【问题标题】:AWS-SAM: How to re use a Route53 domain instead of re creating it?AWS-SAM:如何重新使用 Route53 域而不是重新创建它?
【发布时间】:2021-10-28 02:22:34
【问题描述】:

我正在开发一个 HTTP API。我需要使用自定义域。我已经拥有该域,并且还从AWS Certificate manager 生成了证书。我的域 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 一样访问?

【问题讨论】:

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


【解决方案1】:

如果 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

【讨论】:

    猜你喜欢
    • 2018-12-13
    • 2021-10-25
    • 2014-08-14
    • 2021-05-20
    • 1970-01-01
    • 2021-04-11
    • 1970-01-01
    • 1970-01-01
    • 2020-12-16
    相关资源
    最近更新 更多