Home > Net >  AWS API Gateway dynamic StageName for CICD
AWS API Gateway dynamic StageName for CICD

Time:09-28

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?

CodePudding user response:

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

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