aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.59k stars 3.88k forks source link

apigatewayv2: Custom domain configuration for WebSocket API #23805

Open tmokmss opened 1 year ago

tmokmss commented 1 year ago

Describe the feature

HTTP API construct has defaultDomainMapping property to set a custom domain for the API. We'd like to have an equivalent property for WebSocket API.

Use Case

Set a custom domain for WebSocket API.

Proposed Solution

We can add defaultDomainMapping option and create CfnMapping resouce like the below:

      new CfnApiMapping(this, 'Mapping', {
        apiId: api.apiId,
        domainName: domainName.name,
        stage: stage.stageName,
      });

We may additionally need to create a CfnStage because currently it is not created inside WebSocketApi. In HTTP API we have createDefaultStage option for this purpose.

Other Information

Currently we can set a custom domain by the code like below:

    const api = new WebSocketApi(this, 'Api', {
      connectRouteOptions: {
        authorizer,
        integration: new WebSocketLambdaIntegration('ConnectIntegration', websocketHandler),
      },
      disconnectRouteOptions: {
        integration: new WebSocketLambdaIntegration('DisconnectIntegration', websocketHandler),
      },
      defaultRouteOptions: {
        integration: new WebSocketLambdaIntegration('DefaultIntegration', websocketHandler),
      },
    });

    const stage = new WebSocketStage(this, `Stage`, {
      webSocketApi: api,
      stageName: this.defaultStageName,
      autoDeploy: true,
    });

    const dnsRecordName = 'foo';
    const customDomainName = `${dnsRecordName}.${props.hostedZone.zoneName}`;

    const domainName = new DomainName(this, 'DomainName', {
      domainName: customDomainName,
      certificate: new Certificate(this, 'Certificate', {
        domainName: customDomainName,
        validation: CertificateValidation.fromDns(props.hostedZone),
      }),
    });

    new CfnApiMapping(this, 'Mapping', {
      apiId: api.apiId,
      domainName: domainName.name,
      stage: stage.stageName,
    });

    new ARecord(this, 'DnsRecord', {
      recordName: dnsRecordName,
      zone: props.hostedZone,
      target: RecordTarget.fromAlias(
        new ApiGatewayv2DomainProperties(domainName.regionalDomainName, domainName.regionalHostedZoneId),
      ),
    });

Acknowledgements

CDK version used

2.61.1

Environment details (OS name and version, etc.)

macOS

pahud commented 1 year ago

Agree. @tmokmss are you planning to submit a PR for this?

tmokmss commented 1 year ago

@pahud Not anytime soon. It's only a nice-to-have issue for me.