2

我想将现有的 Rest API 移动到 HTTP API,并在自定义域配置中出错。我的 HTTP API SAM 文件是:

  resHttpApiGateway:
    Type: AWS::Serverless::HttpApi
    Properties:
      StageName: !Sub "${paramEnvironment}"
      CorsConfiguration:
        AllowOrigins:
          - "*"
        AllowHeaders:
          - Content-Type
          - X-Amz-Date
          - Authorization
          - X-Api-Key
          - correlation-object
        AllowMethods:
          - GET
          - POST
          - PUT
          - DELETE
      Auth:
        Authorizers:
          authTokenAuthorizer:
            AuthorizerPayloadFormatVersion: 1.0
            FunctionArn: !Ref paramLambdaAuthorizer
            Identity:
              Headers:
                - Authorization
              ReauthorizeEvery: 0
      Domain:
        EndpointConfiguration: REGIONAL
        DomainName: !If
          - condIsLocal
          - !Sub "dev.${paramBaseDomain}"
          - !Sub "${paramEnvironment}.${paramBaseDomain}"
        CertificateArn: arn:aws:acm:xxx
        Route53:
          HostedZoneId: xxx
        BasePath: "company-api"

我在 Cloudformation 中遇到的错误:

The domain name you provided already exists. (Service: AmazonApiGatewayV2; Status Code: 400; Error Code: BadRequestException; Request ID: ddba8c65-63de-4be4-9316-afd777e8b63f; Proxy: null)

因为我在 API Gateway 中配置了一个现有的自定义域。看起来它试图创建一个自定义域名,有没有办法将 HTTP API 实例映射到现有的自定义域?对于 REST API,我使用AWS::ApiGateway::BasePathMapping进行此任务的映射,但找不到 HTTP API 替代方案。

4

2 回答 2

1

我认为没有办法做到这一点,因为这是来自 API Gateway 的旧 REST API 和较新的 HTTP API 之间的一个非常彻底的变化(内部)。

在旧的 REST API 中,自定义域名始终使用 CloudFront 分配配置(即使在 CloudFront UI 中不可见),而在 CloudFront 中,不可能将多个分配与同一个 CNAME 相关联。如果我没记错的话,这甚至是真正的跨账户。

尽管我自己没有这方面的经验,但可能有一种使用通配符域的解决方法。

根据 API 的运行位置以及 API 的 SLA 要求,简单地设置一些停机时间以确保此更改顺利进行可能会更容易。在停机的情况下,不要忘记事先减少 Route53 DNS 条目的 TTL 值,以便您的新记录更快地传播到所有 DNS 缓存。

(编辑)事实证明这是可能的,只要您不使用 AWS SAM 创建映射。以下是此来源的简短示例

[...]
Resources:
 [...]

  CustomDomainName: # Creates the domain name only
    Type: AWS::ApiGatewayV2::DomainName
    Properties: 
      DomainName: !Ref DomainName
      DomainNameConfigurations: 
        - EndpointType: REGIONAL
          CertificateArn: !If [CreateCert, !Ref GeneratedCert, !Ref CertArn]

  HttpApiMapping: # Create a mapping to an HTTP API (V2)
    Type: AWS::ApiGatewayV2::ApiMapping
    Properties: 
      ApiId: !Ref HttpApiGateway
      ApiMappingKey: http
      DomainName: !Ref CustomDomainName
      Stage: !Ref HttpApiGatewayApiGatewayDefaultStage ## = {LogicalName} + ApiGateway + {StageName} + Stage

  RestApiMapping: #Create a maooing to a REST API (V1)
    Type: AWS::ApiGatewayV2::ApiMapping
    Properties: 
      ApiId: !Ref RestApiGateway
      ApiMappingKey: rest
      DomainName: !Ref CustomDomainName
      Stage: !Ref RestApiGatewayProdStage ## = {Logicalname} + {StageName} + Stage

  HttpApiGateway: # Creates a HTTP API endpoint under the customm domian
    Type: AWS::Serverless::HttpApi

  RestApiGateway: # Creates a Rest API endpoint under the customm domian
    Type: AWS::Serverless::Api
    Properties:
      StageName: Prod
  [...]

  RecordSet: # Creates a record set in the hosted zone for custom domain to point at the APICustomDomain alias
    Type: AWS::Route53::RecordSet
    Properties:
      Name: !Ref DomainName
      HostedZoneId: !If [CreateZone, !Ref GeneratedZone, !Ref ZoneId]
      AliasTarget: 
        DNSName: !GetAtt CustomDomainName.RegionalDomainName
        HostedZoneId: !GetAtt CustomDomainName.RegionalHostedZoneId
      Type: A
于 2021-08-10T06:50:00.820 回答
1

您可以使用AWS::ApiGatewayV2::ApiMapping. 它允许您将现有的自定义域名链接到AWS::Serverless::HttpApi. 您可以使用ApiMappingKey不同的路径映射 ( BasePath),因为这就是 SAM幕后所做的。

于 2021-08-16T08:47:15.857 回答