aws-sam-cli icon indicating copy to clipboard operation
aws-sam-cli copied to clipboard

Globals are not overriding by --env-vars file

Open solivaf opened this issue 8 years ago • 2 comments

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

solivaf avatar Jan 02 '18 16:01 solivaf

I'm experiencing the same behavior with java: https://github.com/awslabs/aws-sam-local/issues/243

spnce avatar Jan 03 '18 22:01 spnce

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

solivaf avatar Jan 04 '18 00:01 solivaf