Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass environment variables from SAM cli to Lambda function code

Is it possible to specify env variable in SAM so that it is available in Lambda function code (python)? I need to set different value for variable for stage deployments. I am able to set env variable in template file (yml) but not sure how to define it for different environments (dev, prod).

like image 590
Vivek Avatar asked Mar 03 '19 10:03

Vivek


1 Answers

You can set the environment variable through a template parameter:


AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

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

Globals:
  Function:
    Environment:
      Variables:
        STAGE: !Ref Stage

Then in your deployment process, pass in the Stage parameter when creating/updating the stack from the SAM template. For example, if you're doing that via the CLI, use --parameters ParameterKey=Stage,ParameterValue=prod.

like image 164
Milan Cermak Avatar answered Oct 19 '22 23:10

Milan Cermak