简体   繁体   中英

How to use condition for lambda environment variables

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.

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

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: ....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM