A way to do this without having to use aws secrets manager will be using the "Parameters" section in template.yaml with an env.json file which you can omit from git like you would for a regular .env file
Here's a sample template.yaml
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: >
sample aws sam application with env variables
Parameters:
EnvVarOne:
Type: String
Description: Sample environment variable
Default: one
EnvVarTwo:
Type: String
Description: Sample environment variable
Default: two
Globals:
Function:
Timeout: 5
MemorySize: 128
Environment:
Variables:
EnvVarOne: !Ref EnvVarOne
EnvVarTwo: !Ref EnvVarTwo
Then your env.json file would look like this
{
"Parameters": {
"EnvVarOne": "your-env-var-one",
"EnvVarTwo": "your-env-var-two"
}
}
So now when you want to test locally, all you need to do is pass in the --env-vars env.json flag to your commands. Example:
sam local start-api --env-vars env.json
Unfortunately, the --env-vars flag and env.json file doesn't work for production deployment (sam deploy) command. In order to pass in environment variables on deploy, you'll need to use --parameter-overrides with the sam deploy command like this:
sam deploy --parameter-overrides EnvVarOne=your-env-var-one