如何在 AWS SAM Cloud-Formation 中使用 Route53 设置自定义域名

Vol*_*reX 2 yaml amazon-web-services aws-cloudformation serverless aws-sam

原始问题:如何从 SAM Cloud-formation 中的 AWS APIGateway 域名中获取区域域名

编辑:我改变了这个问题,希望能获得更多流量到这个答案,因为它回答了几个问题,而不仅仅是我原来的问题。


当我尝试部署我的堆栈时出现以下错误:

resource DomainName does not support attribute type regionalDomainName in Fn::GetAtt

我的 yml 文件如下所示:

  PublicApi:
    Type: AWS::Serverless::Api
    Properties:
      Name: PublicApi
      ...
      EndpointConfiguration: REGIONAL

  DomainName:
    Type: AWS::ApiGateway::DomainName
    Properties:
      RegionalCertificateArn: "arn:aws:acm:${Region}:XXXXXXXXXXX:certificate/XXXXXXXXXXXXX"
      DomainName: !Sub ${Stage}.${name}
      EndpointConfiguration:
        Types:
          - REGIONAL

  myDNSRecord:
    Type: AWS::Route53::RecordSet
    Properties:
      HostedZoneId : Z1UJRXOUMOOFQ8
      Name: !Sub ${Stage}.${name}
      AliasTarget:
        HostedZoneId: Z1UJRXOUMOOFQ8 
        DNSName: !GetAtt DomainName.regionalDomainName
      Type: A

  UrlMapping:
    Type: AWS::ApiGateway::BasePathMapping
    DependsOn:
      - PublicApi
    Properties:
      DomainName: !Ref DomainName
      RestApiId: !Ref PublicApi
      Stage: !Ref Stage

Run Code Online (Sandbox Code Playgroud)

以下内容来自域名文档

“RegionalDomainName 与此自定义域名的区域终结点关联的域名。您可以通过添加将自定义域名指向此区域域名的 DNS 记录来设置此关联。”

我很困惑我需要做什么才能使这个域名成为区域性的。

我也在不同的迭代中遇到以下错误,我认为这应该解决:

“当 REGIONAL 处于活动状态时,无法为 EDGE 导入证书。”

在这方面的任何帮助将不胜感激!

Vol*_*reX 5

我从几个不同的论坛中找到了针对不同问题的不同解决方案,并最终提出了一个工作模型。我希望这可以帮助将来的某个人。

  PublicApi:
    Type: AWS::Serverless::Api
    Properties:
      Name: PublicApi
      StageName: ApiStage
      ...
      EndpointConfiguration: REGIONAL
Run Code Online (Sandbox Code Playgroud)
  DomainName:
    Type: AWS::ApiGateway::DomainName
    Properties:
      RegionalCertificateArn: "arn:aws:acm:u${Region}:XXXXXXXX:certificate/XXXXXXXX"
      DomainName: stage.example.com
      SecurityPolicy: TLS_1_2
      EndpointConfiguration:
        Types:
          - REGIONAL
  LambdaDNS:
    Type: AWS::Route53::RecordSetGroup
    Properties:
      HostedZoneName:
        Ref: example.com.
      RecordSets:
        - Name:
            Fn::Sub: stage.example.com.
          Type: A
          AliasTarget:
            HostedZoneId: Z1UJRXOUMOOFQ8
            DNSName:
              Fn::GetAtt:
                - DomainName
                - RegionalDomainName
  UrlMapping:
    Type: AWS::ApiGateway::BasePathMapping
    DependsOn:
      - PublicApi
      - PublicApiStage
    Properties:
      DomainName:
        Ref: DomainName
      RestApiId:
        Ref: PublicApi
      Stage: ApiStage
Run Code Online (Sandbox Code Playgroud)

对我来说,关键是 BasePathMapping 中的 DependsOn。