Connect aws WebSocket api to custom domain name

Viewed 596

I have created a messaging app using aws WebSocket api and deployed using serverless. The apis are successfully deployed and I am able to test those using wscat. I have other Rest apis in the stack too. I tried mapping my new WebSocket api stack to an existing domain name, but getting the error : Only REGIONAL domain names can be managed through the API Gateway V2 API. For EDGE domain names, please use the API Gateway V1 API. Also note that only REST APIs can be attached to EDGE domain names.

I'm stuck and trying to figure out what changes are to be made.

I went through https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigatewayv2-api.html and https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-custom-domains.html but couldn't figure out.

enter image description here

enter image description here

API configs

1 Answers

You can't mix API Gateway mapping between RestApis and Websocket APIs under a single custom domain. In other words, it could be expressed like we can't use the same domain or subdomain for RestAPI and WebSocket.

Few things that you should be aware of when creating the custom domain for WebSocket mapping

  1. WebSocket doesn't support Edge optimized custom domain endpoint
  2. WebSocket security policy allows only TLS 1.2, not TLS 1.0
  3. Domain certificate can't be referred from another region unless like RestApi custom domain reference

How to create a Custom domain in cloud formation for RestApi and Websocket

WebSocket custom domain

ApiGWCustomDomainName:
    Type: 'AWS::ApiGateway::DomainName'
    Properties:
      RegionalCertificateArn: !Ref RegionalCertificateArn
      DomainName: !Ref DomainName
      EndpointConfiguration:
        Types:
          - REGIONAL
      SecurityPolicy: TLS_1_2
  
  AppApiMapping:
    Type: 'AWS::ApiGatewayV2::ApiMapping'
    Properties:
      ApiMappingKey : !Ref BasePath
      DomainName: !Ref ApiGWCustomDomainName
      ApiId: !Ref  websocketAPI
      Stage : !Ref Stage

Rest API

ApiGWCustomDomainName:
    Type: 'AWS::ApiGateway::DomainName'
    Properties:
      CertificateArn: !Ref CertificateArn
      DomainName: !Ref DomainName
  
  AppApiMapping:
    Type: 'AWS::ApiGateway::BasePathMapping'
    Properties:
      BasePath: !Ref BasePath
      DomainName: !Ref ApiGWCustomDomainName
      RestApiId: !Ref RestApi
      Stage : !Ref ApiStageName
Related