To use local env vars, you must follow the following steps:
- 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
- Populate a JSON file with your vars:
{
"MyFunction": {
"MY_VAR": "Watermelon Man"
}
}
- 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