Globals are not overriding by --env-vars file
The template file below defines a funciton called FooBarFunction with globals. dev_template.yaml
AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Globals:
Function:
Environment:
Variables:
FOO: "B"
Resources:
FooBarFunction:
Type: "AWS::Serverless::Function"
Properties:
Runtime: python3.6
Handler: foo.handler
This is the content inside my environment file which is passed by --env-vars (-n).
env.json
{
"FooBarFunction": {
"FOO": "BAR"
}
}
And this is my function:
import os
FOO = os.getenv("FOO")
def handler(event, context):
print(FOO)
These files (template and environment) are in the same folder directory and when I run the command sam local invoke -n env.json --template dev_template.yaml -e foo.json FooBarFunction I got None as print(FOO) from my handler function
I'm experiencing the same behavior with java: https://github.com/awslabs/aws-sam-local/issues/243
Hmmm, looking into the code, seems to be a limitation of cloudformation library which doesn't provide the Globals section in its struct definition. I still believe that doesn't make senses but I guess this should be clear in documentation, on Globals Section.
As workaround I fill the environment inside Function hierarchy and works perfectly