Search code examples
amazon-web-servicesaws-cloudformationgitlab-ciaws-api-gatewayaws-sam

AWS API Gateway dynamic StageName for CICD


I'm trying to deploy an AWS API Gateway using Gitlab CICD pipelines. Since I have different environments, I want the StageName property of the API to be set dynamically i.e., StageName should be "dev" when deployed to dev stack, "prod" when deployed to prod stack. Below is the YAML script I used.

Mappings:
  StackEnv:
    dev-stack:
      envm: "dev"
    qa-stack:
      envm: "qa"
Resources:  
  TestingAPI:
    Type: AWS::Serverless::Api
    Properties:
      StageName: [!FindInMap [StackEnv, !Ref AWS::StackName, envm]]

It looks fine from above but when I try to deploy the API, I'm getting the error mentioned below.

[InvalidResourceException('TestingAPI', "Type of property 'StageName' is invalid.")] 

I can't seem to understand why it should fail. If this approach won't work, how can I pass the respective environment values to the StageName property without breaking the pipeline?


Solution

  • You are wrapping the results of the !FindInMap function in [] making it a list. Remove the brackets:

          StageName: !FindInMap [StackEnv, !Ref AWS::StackName, envm]