Hari Pradhan
Hari Pradhan

Reputation: 23

Fail to create a custom domain name for websocket API gateway using AWS Cloudformation

I need to create a custom domain name for a websocket enabled API gateway and seems that CloudFormation for API gateway v2 (which is basically for websocket) doesn’t support it (at least the documentation https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-reference-apigatewayv2.html doesn’t mention this). I tried with AWS::ApiGateway::DomainName which I believe is for Rest API gateway but the stack fails with the error

This custom domain name cannot map to WEBSOCKET protocol Apis.

I see that AWS CLI does support API gateway v2 create domain name command (https://docs.aws.amazon.com/cli/latest/reference/apigatewayv2/create-domain-name.html) but not sure the reason why the CloudFormation wouldn’t support this.

Any workaround or feedback would be greatly appreciated.

Upvotes: 2

Views: 1580

Answers (2)

Mathix420
Mathix420

Reputation: 1297

No more need of lambda, https://stackoverflow.com/a/61834163/9799292

This Cloud Formation template is working just fine

Resources:
    WebSocketDomainName:
      Type: AWS::ApiGatewayV2::DomainName
      Properties:
        DomainName: <domain-name>
        DomainNameConfigurations:
          - EndpointType: 'REGIONAL'
            CertificateArn: <cert-arn>
    WebSocketMapping:
      Type: AWS::ApiGatewayV2::ApiMapping
      Properties:
        ApiId: <app-id>
        DomainName: !Ref WebSocketDomainName
        Stage: <stage-name>

Upvotes: 3

asr9
asr9

Reputation: 2778

Usually the way it works is that new features first appear in CLI or APIs and are added to CFN later on. Why? Because I believe CFN uses the same API internally to create the resource with given properties. Here's a similar thread with same experience for when Aurora Serverless was released. Creating an Aurora serverless cluster from cloudformation

Workaround - create a lambda backed AWS::CloudFormation::CustomResource & call the createDomainName API from the lambda to create your custom domain name. Sometime in the future, it probably will be made available in cloudformation.

Update: No need for lambda custom resource. Better answer here

Upvotes: 1

Related Questions