SAM deploy doesn't set environment variables
See original GitHub issueDescription
When using AWS SAM for local development, I can introduce environment variables by setting them in the template with no value, and then defining them in my environment. However, when I go to deploy, the environment variables do not appear to be inserted by sam package
or sam deploy
, and I get the following error on deploy:
Failed to create the changeset: Waiter ChangeSetCreateComplete failed: Waiter encountered a terminal failure state Status: FAILED. Reason: [/Resources/GeneratePlan/Type/Environment/Variables/SECRET_ACCESS_KEY] 'null' values are not allowed in templates
Where SECRET_ACCESS_KEY
is one of my environment variables. I cannot find any documentation detailing how to deploy projects with environment variables, either by having them defined in my environment or providing them in an alternate config.
I don’t want to add the environment variables to my template.yml
directly because this is stored in Git, and I don’t want to edit them manually into the packaged.yml
file each time between the package
and deploy
steps as that’s cumbersome.
I haven’t seen any steps in the documentation or similar issues, so I presume this is either an edge case bug or I am just missing something simple (in which case I might file this as a documentation bug) 😄
Steps to reproduce
The following config is a minimal excerpt from mine:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Globals:
Function:
Environment:
Variables:
SECRET_ACCESS_KEY:
Resources:
AnswerChallenge:
Type: "AWS::Serverless::Function"
Properties:
Runtime: nodejs8.10
Handler: build/functions/answerChallenge.default
CodeUri: ./
Policies: AmazonDynamoDBFullAccess
Events:
GetRequest:
Type: Api
Properties:
Path: /event
Method: get
Observed result
When I run sam start-api
with SECRET_ACCESS_KEY
defined, the lambda works as expected. When I attempt to deploy with sam package
and sam deploy
, I receive an error about undefined variables.
Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
- OS: MacOS
sam --version
: 0.11
Issue Analytics
- State:
- Created 4 years ago
- Reactions:38
- Comments:47 (7 by maintainers)
Yeah the
sam local start-api
also uses the mismatched format in--parameter-overrides
, which is frustrating! Having to use two different config formats between local dev and prod deploy is not great… I had tried the--env-vars
solution but had issues with the different cases expected between uppercase and snake case. This is what I had to do to get some resemblance of consistency:I store my config file as a JSON file, such as
config.json
:And define them as parameters in the
template.yaml
:Then for development I have the following in
package.json
:So I can run
npm run api
to get the api server going with the config file.Then for deployment our CI runs:
Surprising amount of work to get sane environment variables in SAM… I think this needs some developer experience TLC or maybe I’m missing something blatantly obvious 😅
Same issue here.
sam package
/sam deploy
should accept the--env-vars
flag.My current workaround is to use CF parameters, since
sam package
/sam deploy
are aliases forcloudformation package
/cloudformation deploy
.template.yml