Serverless-application-model: Custom Domain Name Base Path Mapping

Created on 7 Apr 2017  路  13Comments  路  Source: aws/serverless-application-model

Is there a way to achieve this with in SAM? i tried adding something like this

  PathMapping:
    Type: AWS::ApiGateway::BasePathMapping
    DependsOn: ApiGatewayApi
    Properties:
      BasePath: '(none)'
      DomainName: 'custom-domain'
      RestApiId: !Ref ApiGatewayApi
      Stage: !Sub '${Stage}'

i get an error saying the stage-name is invalid, which appears due to the fact that the deploy/stage resources get created after the AWS::Serverless::Api resource, is there a way to access/depend on those?

areserverless-api typduplicate typquestion

Most helpful comment

Is there a resolution to this issue? I am trying to assign a custom domain to my AWS::Serverless::Api but can't get anything to work.

All 13 comments

There is a DependsOn attribute you can set.

edit: I see that you already set it so disregard what I said if it's not working like you expect :P That is the only way I know of.

ah i see or i guess is there a way to get the logical id of the apigateway::stage resource because thats what we really need to depend on

Doesn't look like there is any attributes available to retrieve via Fn::GetAtt for AWS::Serverless::Api. Might be a good addition :D

i did just notice that in my case the logical id for the stage resource was ApiGatewayApiStage i can test some more but it seems that SAM just gives it that id based off what you call your AWS::Serverless::Api resource

i was able to verify by changing the logical id of of Serverless::api resource and SAM just appends 'Stage' to the end of it during stack creation

Is there a resolution to this issue? I am trying to assign a custom domain to my AWS::Serverless::Api but can't get anything to work.

Doing a custom domain in serverless is currently a big hack. With that said...

A BasePathMapping can be used but this will create a circular dependency upon deletion and the stack will not delete. To fix that include the DependsOn: ServerlessFunction directive. Then when the delete goes forward the failures can be ignored and then deleted manually.

Also the custom domain name should already be created. See http://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-custom-domains.html#how-to-custom-domains-console

  BasePathMapping:
    DependsOn: ServerlessFunction
    Type: AWS::ApiGateway::BasePathMapping
    Properties:
      DomainName: "example.com"
      RestApiId:
        Ref: ServerlessRestApi
      Stage:
        Ref: ServerlessRestApiProdStage

Also, the serverless setup creates underlying stages and api's which are referenced here. Current names of resources can be found in a serverless cloudformation already setup. They are unofficial names and could change anytime.

This works for me, but I hard coded stage name:

ApiGatewayApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: prod
      DefinitionBody:
        swagger: 2.0
        info:
          title: !Ref AWS::StackName
        paths:
          "/ping":
            get:
              x-amazon-apigateway-integration:
                httpMethod: GET
                type: aws_proxy
                uri:
                  Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${NetWorthFunction.Arn}/invocations
              responses: {}

MyMapping:
    Type: 'AWS::ApiGateway::BasePathMapping'
    Properties:
      BasePath: ''
      DomainName: !Ref CustomDomain
      RestApiId: !Ref ApiGatewayApi
      Stage: prod

Thanks to the useful comments here I just managed to get this to work by making the BasePathMapping depend on the Stage that SAM will create. e.g.

  AppApiBasePathMapping:
    Type: AWS::ApiGateway::BasePathMapping
    DependsOn: AppApiv1Stage
    Properties:
      BasePath: foo
      DomainName: !ImportValue ApiGatewayDomainName
      RestApiId: !Ref AppApi
      Stage: v1

  AppApi:
    Type: AWS::Serverless::Api

It would be great to get some steer from the maintainers if this is a stable way to fix this bug. And if they will be fixing it properly any time soon. :smile_cat:

The above workarounds didn't work for me. I just put the mapping in another stack and create it after the API stack.

I got this to work by making the DependsOn point to a method instead of the API. Maybe a method has to exist before the stage is really there?

This will be handled with #248. Marking as duplicate and closing.

RFC for Custom Domain Names #783

Was this page helpful?
0 / 5 - 0 ratings