Score:0

AWS CDK add route 53 alias record the most simple way

us flag

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.

)

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.