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

问题描述 投票:1回答:1

我正在用Serverless管理一个应用程序的REST API,并希望在同一地区用WebSockets API扩展这个设置。一切都应该用相同的证书来处理,但不同的子域。

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

错误,找不到CloudFormation的资源..: Failed to find CloudFormation resources for ....

我在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

domain-configuration.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"
}

]

但这导致了以下错误。

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

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

小结: 我完全卡在了如何让自定义域名附加到我的WebSocket API阶段。我很高兴每一个正确方向的提示!

amazon-web-services websocket aws-api-gateway serverless-framework
1个回答
1
投票

找到了一个自定义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: <app-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
© www.soinside.com 2019 - 2024. All rights reserved.