Aws-sam-cli: Environment Variable Template vs File

Created on 26 Sep 2017  Â·  10Comments  Â·  Source: aws/aws-sam-cli

I'm attempting to load environment variables from a file based on the instructions in the README. Unfortunately, unless I have the variables defined in the template the contents of the file are never recognized.

I would expect that the file approach would work regardless of whether the template has the environment configuration set.

Most helpful comment

@sanathkr - I feel like we should have an option to have a separate file for environment variables so we can choose whether or not to commit the file. As of right now we _have_ to commit the env variables.

All 10 comments

I'm seeing a similar problem. I attempted to load the environment variables a separate file per the docs, but wasn't able to get it to work. I also attempted to load the environment variables by prefixing them before the command, and didn't have any luck either.

UPDATE: I was able to get the environment variables to pull in from the host once I defined empty environment variables in the template.yaml that matched the names of my host environment variables.

UPDATE: I was able to get the environment variables to pull in from the host once I defined empty environment variables in the template.yaml that matched the names of my host environment variables.

@MoMenne can you show an example of how you got it to work, please?


edit: if I create a API event for the same function, the env-vars file works as expected 😕

@danilopopeye Sorry for not getting back to you sooner. He's an example of what I did
```AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Description: |
Sends a text message as a reminder

Resources:
# Place your project resources here (AWS Lambda functions, API Gateway) etc

SendText:
Type: AWS::Serverless::Function
Properties:
Timeout: 10
Runtime: nodejs6.10
Handler: index.handler
Environment:
Variables:
AUTH_TOKEN:
ACCOUNT_SID:
I then setACCOUNT_TOKENandACCOUNT_SID``` env variables and it seemed to pull those two env vars in correctly.

I then set ACCOUNT_TOKEN and ACCOUNT_SID env variables

@MoMenne you mean SHELL env variables or using the env-vars.json file?

Sorry about that. Right. I set environment variables in the shell.
export ACCOUNT_TOKEN=xxxxxxx

On Fri, Oct 6, 2017 at 2:54 PM, Danilo Sousa notifications@github.com
wrote:

I then set ACCOUNT_TOKEN and ACCOUNT_SID env variables

@MoMenne https://github.com/momenne you mean SHELL env variables or
using the env-vars.json file?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/awslabs/aws-sam-local/issues/139#issuecomment-334854248,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AANTMA6rbrhMLzlqDc6XorajTvlme_xLks5spoVlgaJpZM4PjjS5
.

--

Mike Menne
Technology Director

4811 Delmar Blvd, St. Louis, MO 63108
LaunchCode.org https://www.launchcode.org/ | Facebook
https://www.facebook.com/launchcodestl/ | Twitter
https://twitter.com/Launch_Code

[email protected]
314-252-8921 <314-477-9816>

This is expected behavior where SAM Local only parses environment variables that are defined in the SAM template. This is exactly how it will work when you push the Lambda to cloud. So as long as you define the env-var in SAM template it should show up in your function

@sanathkr - I feel like we should have an option to have a separate file for environment variables so we can choose whether or not to commit the file. As of right now we _have_ to commit the env variables.

I found this from google when looking for a reference to using environment variables. According to the docs about sam local environment variables:

The Environment key defines the variables and the default values. One can use the --env-vars cli argument to define a json file to override the values.

@frob - the link to those docs is broken

Was this page helpful?
0 / 5 - 0 ratings

Related issues

asyba picture asyba  Â·  3Comments

dschu-lab picture dschu-lab  Â·  3Comments

drumadrian picture drumadrian  Â·  3Comments

red8888 picture red8888  Â·  3Comments

Caian picture Caian  Â·  3Comments