Home > Net >  How to use condition for lambda environment variables
How to use condition for lambda environment variables

Time:02-11

I am writing a cloudformation template .My use case is to pass different values for aws lambda environment.

   Environment:
    Variables:
      databaseName: lambda-dev
      databaseUser: admin-dev
      databaseName: lambda-prod
      databaseUser: admin-prod

I want to keep a condition in environment variables like if(env==dev) then use these values ,if (env==prod)then use last two .Thank you in advance.Any suggestions please comment .

CodePudding user response:

The way to do this is with a mapping, not with conditions. Something like this:

Parameters:
  Environment:
    Type: String
    Default: dev
    AllowedValues:
      - dev
      - prod
    Description: Enter environment (dev or prod)

Mappings: 
  DatabaseMap: 
    dev: 
      name: "lambda-dev"
      user: "admin-dev"
    prod: 
      name: "lambda-prod"
      user: "admin-prod"

And later, to use the mapping to retrieve the database name associated with the given environment:

!FindInMap
  - DatabaseMap
  - !Ref Environment
  - name

CodePudding user response:

My YAML is not the best, but you want something like this:

Conditions:
  ProductionCondition:
      Fn::Equals:
      - Ref: StageParameter
      - 'production'
Environment:
    Variables:
      databaseName: Fn::If: [ProductionCondition, 'lambda-prod', 'lambda-dev']       databaseUser: admin-dev
      databaseUser: ....
  • Related