amazon-web-servicesaws-lambdaaws-api-gatewayaws-samaws-http-api

AWS API Gateway HttpAPI SAM Custom Domain Name Error


I want to move my existing Rest API to HTTP API, and getting error in custom domain configuration. My SAM file for HTTP API is:

  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"

Error I get in 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)

Because I have an existing custom domain configured in API Gateway. Looks like it tries to create a custom domain name, is there a way to map HTTP API instance to an existing custom domain? For REST API I was using AWS::ApiGateway::BasePathMapping for mapping for this task, but cannot find HTTP API alternative.


Solution

  • You can use AWS::ApiGatewayV2::ApiMapping. It allows you to link an existing custom domain name to AWS::Serverless::HttpApi. You can use ApiMappingKey for your different path mappings (BasePath) as that's what SAM does under the hood.