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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With