AWS CloudFormation: pathmapping - Invalid stage identifier specified

Viewed 5174

I'm deploying Lambdas (and APIGateway config) through CloudFormation, ultimately using Serverless.

The generated CloudFormation pathmapping section in the cloudformation-template-update-stack.json looks like this:

"pathmapping": {
      "Type": "AWS::ApiGateway::BasePathMapping",
      "Properties": {
        "BasePath": "demolambda",
        "DomainName": "staging-api.<REDACTED>",
        "RestApiId": {
          "Ref": "ApiGatewayRestApi"
        },
        "Stage": "staging"
      }
    }

When this is run I get back the error:

pathmapping - Invalid stage identifier specified.

I'm not really sure what this means. The stack gets uploaded to S3, and everything looks fine, however, no matter how I play around with it I continue to get this message.

Curious if anyone has any thoughts about what causes this or how to fix it?

3 Answers

when I ran the stack, I kept my deployed API stageName same as the BasePathMapping stageName and able to successfully create the Mapping. Additionally I have provided the specific certificate for the Domain while the creation.

ApiGatewayDomainName:
    Type: AWS::ApiGateway::DomainName
    Properties:      
      CertificateArn:
        Fn::ImportValue:  !Sub "mycertificates-UserCertificateArn"             
      DomainName:
          Ref: ApiDomainName

 ApiGatewayBasePathMapping:
    Type: AWS::ApiGateway::BasePathMapping
    Properties:
      DomainName:
        Ref: ApiDomainName
      RestApiId:
        Ref: ApiGatewayRestApi
      Stage: !Sub "${EnvironmentName}"
    DependsOn: ApiGatewayDomainName

First, you need a resource AWS::ApiGateway::Stage. This resource depends on AWS::ApiGateway::Deployment

Then make AWS::ApiGateway::BasePathMapping to depends on AWS::ApiGateway::Stage:

Here is my tested solution:

Resources:
  apiDomainName:
    Type: "AWS::ApiGateway::DomainName"
    Properties:
      CertificateArn: <CertificateArn>
      DomainName: <DomainName>
      SecurityPolicy: 'TLS_1_2'
      EndpointConfiguration:
        Types:
          - EDGE
  apiGateway:
    Type: 'AWS::ApiGateway::RestApi'
...
  apiGatewayDeployment:
    Type: 'AWS::ApiGateway::Deployment'
    Properties:
      RestApiId: !Ref apiGateway
  apiGatewayStage:
    Type: "AWS::ApiGateway::Stage"
    Properties:
      DeploymentId: !Ref apiGatewayDeployment
      RestApiId: !Ref apiGateway
      StageName: <stage>
...
  apiGatewayMapping:
    Type: "AWS::ApiGateway::BasePathMapping"
    DependsOn:
      - apiGatewayStage
    Properties:
      BasePath: <path>
      DomainName: <DomainName>
      RestApiId: !Ref apiGateway
      Stage: <stage>

Both of the current answers are "correct", but not necessarily usable in all instances. The more general answer is to either:

  1. Find or create a step that will always happen after the ApiGatewayDeploy step and make the BasePathMapping depend on that
  2. Use one of several plug-ins that work around this issue, like:

Here's a link to a serverless issue that discusses the cause of this and a lot of good discussion on whys and workarounds: https://github.com/serverless/serverless/issues/2233

Related