Skip to content

Instantly share code, notes, and snippets.

@AnalyzePlatypus
Created May 19, 2020 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnalyzePlatypus/9e6ee7550363fe5bea38db99f719ad49 to your computer and use it in GitHub Desktop.
Save AnalyzePlatypus/9e6ee7550363fe5bea38db99f719ad49 to your computer and use it in GitHub Desktop.

AWS Lambda SAM cli: Using local env vars

To use local env vars, you must follow the following steps:

  1. Declare all env vars in your template.yml, like so:
Resources:
  MyFunction:
    Type: AWS::Serverless::Function 
      Environment:
        Variables:
          MY_VAR: !Ref MY_VAR # !Ref means to read this from the SAM's own environment
  1. Populate a JSON file with your vars:
{
  "MyFunction": {
    "MY_VAR": "Watermelon Man"
  }
}
  1. Pass the --env-vars flag when invoking the function:
sam local invoke MyFunction --env-vars .env.json

# Or
sam local start-api --port 5000 --env-vars .env.json
# And then in a new tab
curl 127.0.0.1:5000/myFunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment