AWS SAM配置HTTP API时自定义域名已存在的映射问题咨询
解决方案
你遇到的报错是因为SAM模板中AWS::Serverless::HttpApi的Domain配置块默认会触发创建新的自定义域名逻辑,无法直接复用已有域名。HTTP API场景下对应REST API的AWS::ApiGateway::BasePathMapping的资源是AWS::ApiGatewayV2::ApiMapping,操作步骤如下:
- 移除
resHttpApiGateway资源下的全部Domain配置块,不再让SAM尝试自动创建域名和映射 - 新增独立的
AWS::ApiGatewayV2::ApiMapping资源,关联已有自定义域名和你的HTTP API实例
以下是修改后的完整配置示例:
# 修改后的HTTP API配置 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 # 新增的API映射资源,复用已有自定义域名 resHttpApiDomainMapping: Type: AWS::ApiGatewayV2::ApiMapping Properties: DomainName: !If - condIsLocal - !Sub "dev.${paramBaseDomain}" - !Sub "${paramEnvironment}.${paramBaseDomain}" ApiId: !Ref resHttpApiGateway Stage: !Ref resHttpApiGateway.Stage ApiMappingKey: "company-api" # 对应原配置中的BasePath
注意事项
- 确保你使用的已有自定义域名的终端节点类型为REGIONAL,和原配置要求匹配
- 同账号下的已有域名无需额外权限配置,跨账号域名需要提前配置好跨账号访问权限
内容的提问来源于stack exchange,提问作者0bj3ct
相关产品推荐
相关产品推荐

