amazon-web-services - AWS-SAM:如何重新使用 Route53 域而不是重新创建它?

标签 amazon-web-services aws-cloudformation amazon-route53 aws-sam

我正在开发一个 HTTP API。我需要为此使用自定义域。我已经拥有该域,并且还从 AWS 证书管理器 生成了证书。我的域 DNS 位于 Amazon Route53 中。

现在我尝试将此自定义域附加到我的 HTTP API。我还需要设置基本路径。我正在使用 AWS-SAM 模板,下面是我尝试过的。

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  aws-restapi

  Sample SAM Template for aws-restapi
  
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 5
    VpcConfig:
        SecurityGroupIds:
          - sg-041f24sd125s51e8e
        SubnetIds:
          - subnet-05265b2d


Parameters:
  FirebaseProjectId:
    Type: String
  
  DomainName:
    Type: String
    Default: api.example.com

Resources:

  AuthGatewayHttpApi:
    Type: AWS::Serverless::HttpApi
    Properties:
      Domain:
        DomainName: !Ref DomainName
        CertificateArn: arn:aws:acm:us-east-1:xxxx:certificate/xxxxx-xxxx-xxxx-xxxx-xxxxx
        Route53:
          HostedZoneId: Z096752626aDO8HB8C6
      Auth:
        Authorizers:
          FirebaseAuthorizer:
            IdentitySource: $request.header.Authorization
            JwtConfiguration:
              audience:
                - !Ref FirebaseProjectId
              issuer: !Sub https://securetoken.google.com/${FirebaseProjectId}
        DefaultAuthorizer: FirebaseAuthorizer
  
  AuthFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: aws-restapi/
      Handler: source/testfile.lambdaHandler
      Runtime: nodejs14.x
      Events:
        Gateway:
          Type: HttpApi
          Properties:
            ApiId: !Ref AuthGatewayHttpApi
            Path: /hello
            Method: get

此模板构建良好,但在部署时会引发以下错误。

CREATE_FAILED                      AWS::Route53::RecordSetGroup       RecordSetGroupf015792d8d 

[Tried to create resource record set [name='api.example.com.',type='A'] but it already exists] 

嗯,错误说明了事实,我已经有了域名。我不想再次创建域或证书,我只想在这里使用它们。

我怎样才能完成这个工作?另外,我如何设置 basePath 以便我可以像 api.example.com/products 一样访问?

最佳答案

如果此 DomainName 指向服务器或其他 AWS 服务(例如 AWS Amplify),则您无法重复使用该域名。仅当 API Gateway 已使用该域(或子域)时,您才可以重复使用该域(或子域)。因此,如果您可以删除 DNS 注册器,我建议您使用 AWS SAM 重新创建它或创建不同的子域。如果 API Gateway 已使用子域,则您可以创建 AWS::ApiGatewayV2::ApiMapping 资源。

使用 AWS SAM 创建 DNS 记录

在这种情况下,您可以简单地定义域的 BasePath 属性:

AuthGatewayHttpApi:
    Type: AWS::Serverless::HttpApi
    Properties:
      Domain:
        BasePath:
          - products
        DomainName: !Ref DomainName
        CertificateArn: arn:aws:acm:us-east-1:xxxx:certificate/xxxxx-xxxx-xxxx-xxxx-xxxxx
        Route53:
          HostedZoneId: Z096752626aDO8HB8C6
      Auth:
        Authorizers:
          FirebaseAuthorizer:
            IdentitySource: $request.header.Authorization
            JwtConfiguration:
              audience:
                - !Ref FirebaseProjectId
              issuer: !Sub https://securetoken.google.com/${FirebaseProjectId}
        DefaultAuthorizer: FirebaseAuthorizer

为现有域名创建 ApiMapping

在这种情况下,您无需定义 Domain 属性,只需创建 AWS::ApiGatewayV2::ApiMapping 资源。

AuthGatewayHttpApi:
    Type: AWS::Serverless::HttpApi
    Properties:
      Auth:
        Authorizers:
          FirebaseAuthorizer:
            IdentitySource: $request.header.Authorization
            JwtConfiguration:
              audience:
                - !Ref FirebaseProjectId
              issuer: !Sub https://securetoken.google.com/${FirebaseProjectId}
        DefaultAuthorizer: FirebaseAuthorizer


AuthGatewayProductsMapping: # Creates the mapping for Reporting V1
    Type: AWS::ApiGatewayV2::ApiMapping
    Properties:
      ApiId: !Ref AuthGatewayHttpApi
      ApiMappingKey: products
      DomainName: !Ref DomainName
      Stage: Prod

关于amazon-web-services - AWS-SAM:如何重新使用 Route53 域而不是重新创建它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69046555/

相关文章:

postgresql - EBS 快照与 EC2 上 PostgreSQL 的 WAL-E

ruby - AWS Ruby SDK Cloudformation 不会验证模板

amazon-web-services - 在 Red Hat ami-7.5 上自动化网络接口(interface)相关配置

amazon-web-services - 连接到根域 - AWS Route 53 EC2

amazon-web-services - Route53 域名不工作(godaddy 域)

amazon-web-services - 如何使用 AWS SAM 启用 CORS

amazon-web-services - 备份:Amazon S3或Glacier-很多小文件吗?

git - 使用 AWS CloudFormation 克隆私有(private) GitHub 存储库 AWS Sagemaker NB 实例

amazon-web-services - 我可以对 AWS IAM 用户隐藏一些托管区域吗?

amazon-web-services - 是否可以让 AWS Spot 实例仅在 'price is right' 时启动并运行,如果没有则在标准实例上运行?