具有无服务器的 WebSockets API 的自定义域名

tps*_*idt 6 amazon-web-services websocket aws-api-gateway serverless-framework

我正在使用无服务器管理应用程序的 REST API,并希望使用同一区域中的 WebSockets API 扩展此设置。一切都应该使用相同的证书,但不同的子域来处理。

起初,我创建了一个新的自定义域sls create_domain --stage=...。然后我尝试将它添加到新的 WebSockets 堆栈中,但以这个错误结束:

错误:无法为...找到 CloudFormation 资源

我在 Github 上发现 CloudFormation 现在似乎不支持它,因此 Serverless 不支持它。

所以我尝试在 UI 中手动将我的舞台附加到自定义域名:

REST API 和 HTTP API 在同一个域名上的混合只能通过 API Gateway 的 V2 DomainName 接口来完成。目前,WebSocket API 只能与其他 WebSocket API 一起附加到域名。这也必须通过 API Gateway 的 V2 DomainName 接口发生。

出现了更多混淆,因为在这种情况下它甚至不是相同的域名。新域名sockets.<DOMAIN>.com和现有域名是api.<DOMAIN>.com. 还是不同的子域落入“同一个域名”?

尽管如此,我还是尝试通过 apigatewayv2 CLI 再次创建自定义域:

aws apigatewayv2 create-domain-name --domain-name <DOMAIN> --domain-name-configurations file://domain-configuration.json --region eu-west-1
Run Code Online (Sandbox Code Playgroud)

域配置.json:

[
{
    "ApiGatewayDomainName": "<DOMAIN>",
    "CertificateArn": "arn:aws:acm:us-east-1:<ACCOUNT_ID>:certificate/<CERT_ID>",
    "CertificateName": "<DOMAIN>",
    "DomainNameStatus": "AVAILABLE",
    "EndpointType": "EDGE",
    "SecurityPolicy": "TLS_1_2"
}
Run Code Online (Sandbox Code Playgroud)

]

但这会导致以下错误:

调用 CreateDomainName 操作时发生错误 (BadRequestException):无效证书 ARN:arn:aws:acm:us-east-1:924441585974:certificate/b88f0a3f-1393-4a16-a876-9830852b5207。证书必须在“eu-west-1”中。

我目前的状态是 API Gateway 只允许自定义证书位于 us-east-1,所以这个错误让我更加困惑。

简介: 我完全不知道如何将自定义域名附加到我的 WebSocket API 阶段。我对正确方向的每一个提示感到高兴!

tps*_*idt 8

找到了一个带有自定义 CloudFormation 资源模板的解决方案:

resources:
  Resources:
    WebSocketDomainName:
      Type: AWS::ApiGatewayV2::DomainName
      Properties:
        DomainName: <domain-name>
        DomainNameConfigurations:
          - EndpointType: 'REGIONAL'
            CertificateArn: <cert-arn>
    WebSocketMapping:
      Type: AWS::ApiGatewayV2::ApiMapping
      Properties:
        ApiId: <api-id>
        DomainName: !Ref WebSocketDomainName
        Stage: <stage-name>
    DNSRecord:
      Type: AWS::Route53::RecordSet
      Properties:
        HostedZoneName: <hosted-zone-name>.
        TTL: '900'
        ResourceRecords:
          - !GetAtt [ WebSocketDomainName, RegionalDomainName ]
        Name: <domain-name>
        Type: CNAME
Run Code Online (Sandbox Code Playgroud)

编辑:现在使用serverless-domain-manager

custom:
  customDomain:
    rest:
      domainName: rest.serverless.foo.com
      stage: ci
      basePath: api
      certificateName: '*.foo.com'
      createRoute53Record: true
      endpointType: 'regional'
      securityPolicy: tls_1_2
    http:
      domainName: http.serverless.foo.com
      stage: ci
      basePath: api
      certificateName: '*.foo.com'
      createRoute53Record: true
      endpointType: 'regional'
      securityPolicy: tls_1_2
    websocket:
      domainName: ws.serverless.foo.com
      stage: ci
      basePath: api
      certificateName: '*.foo.com'
      createRoute53Record: true
      endpointType: 'regional'
      securityPolicy: tls_1_2
Run Code Online (Sandbox Code Playgroud)