Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set environment variables according to stage in cloud formation template

The environment variables in the lambda handler has to be set via lambda handler according to the stage. The values for schema, endpoint are different for different stage. How can this be done via yml template. I am new to this, so don't know how this will be done.

Parameters:
   Stage: {Type: String, Default: ''}
Resources:
   LambdaHandler:
   Type: AWS::Serverless::Function
   Properties:
       Environment:
          Variables:
          ......
          ......

How to continue further?

like image 665
Nitika Avatar asked Oct 27 '25 08:10

Nitika


1 Answers

template.yaml:

Parameters:
  Environment:
    AllowedValues:
      - dev
      - prod
    Type: String

Resources:
  myLambda:
    Type: AWS::Lambda::Function
    Properties:
      Environment:
        Variables:
          stage: !Ref Environment

Your shell:

$ aws cloudformation deploy --parameter-overrides Environment=dev

Say you want a variable conditional on the environment:

Parameters:
  Environment:
    AllowedValues:
      - dev
      - prod
    Type: String

Mappings:
  Environments:
    dev:
      LogLevel: "DEBUG"
    prod:
      LogLevel: "ERROR"

Resources:
  myLambda:
    Type: AWS::Lambda::Function
    Properties:
      Environment:
        Variables:
          LOG_LEVEL: !FindInMap [Environments, !Ref Environment, LogLevel]
like image 129
Neil McGuigan Avatar answered Oct 29 '25 23:10

Neil McGuigan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!