I have created an API Gateway for websockets with CDK.
For easier remembering of the URL, I want to give the sadjl342r.execute-api.amazonaws an ALIAS in Route53
Something like "my-ws-api.mydomain.tld"
This is the way I created the API gateway
const websocketEventsLambda: IFunction = this.createWebsocketsLambda(props.stage);
this.apiGatewayWebSockets = new CfnApi(this, apiGatewayId, {
name: apiGatewayId,
protocolType: "WEBSOCKET",
routeSelectionExpression: "$request.body.topic",
apiKeySelectionExpression: "$request.header.x-api-key"
});
const connectIntegration = new CfnIntegration(
this,
"websockets-connect-lambda-integration-" + props.stage,
{
apiId: this.apiGatewayWebSockets.ref,
connectionType: "INTERNET",
integrationType: "AWS_PROXY",
integrationMethod: "POST",
integrationUri:
"arn:aws:apigateway:eu-central-1:lambda:path/2015-03-31/functions/arn:aws:lambda" +
":<REGION>:<ACCOUNT_ID>:function:" + websocketEventsLambda.functionName +
"/invocations",
passthroughBehavior: "WHEN_NO_MATCH",
payloadFormatVersion: "1.0",
}
);
const connectRoute = new CfnRoute(
this,
"apigateway-websockets-connect-route-" + props.stage,
{
apiId: this.apiGatewayWebSockets.ref,
routeKey: "$connect",
authorizationType: "NONE",
target: "integrations/" + connectIntegration.ref
}
);
const deployment = new CfnDeployment(
this,
`apigatewayv2-websockets-deployment-` + props.stage,
{
apiId: this.apiGatewayWebSockets.ref
}
);
new CfnStage(this, `apigateway-stage-${props.stage}`, {
apiId: this.apiGatewayWebSockets.ref,
autoDeploy: true,
deploymentId: deployment.ref,
stageName: props.stage
});
deployment.addDependsOn(connectRoute);
deployment.addDependsOn(disconnectRoute);
deployment.addDependsOn(subscribeRoute);
deployment.addDependsOn(defaultRoute);
this.webSocketsApiURL =
deployment.apiId +
".execute-api." +
this.region +
".amazonaws.com/" + props.stage;
Now I try to create an ARecord within the same scope (I used this answer as a starting point: https://stackoverflow.com/a/56599567/15013406)
const domainName: string = "ws-api-" + props.stage + "." + DOMAIN;
new route53.ARecord(this, "AliasRecord", {
recordName: domainName,
target: route53.RecordTarget.fromAlias({
bind() {
return {
dnsName: webSocketsApiURL,
hostedZoneId: idontknowwhattoenterhere
};
}
}),
zone: hostedZone
});
I don't know what to enter for the hostedZoneId in the last code snippet. Note that this answer says that this must be some other zone than the zone of my own domain that should point to the API Gateway
See https://stackoverflow.com/a/56599567/15013406:
Note: the hostedZoneId for your alias record is not the same as the hosted zone id of your own zone.
)