Comments (20)
If it helps anyone, I found out that by setting empty environment variables in the template.yml, the env vars from your JSON environment variables file will get loaded.
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: test
Resources:
TestFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./
Handler: app.handler
Runtime: nodejs12.x
MemorySize: 2048
Environment:
Variables:
TEST_ENV: ""
Events:
Api:
Type: Api
Properties:
Path: /
Method: get
{
"TestFunction": {
"TEST_ENV": "foo"
}
}
With this, TEST_ENV gets loaded, and the value is "foo".
sam local start-api -n env.json
from aws-sam-cli.
Actually this is already in the readme.. but probably not too explicit (https://github.com/awslabs/aws-sam-cli/blob/develop/docs/advanced_usage.rst#environment-variable-file).
@sashokbg would you be interested in sending a PR?
Closing the issue because this is not a bug.
from aws-sam-cli.
@1nfility thank you for the above! AWS really needs to handle this better or add this info to their docs. Was racking my brain trying to figure out why env vars weren't working for same local invoke...
from aws-sam-cli.
FWIW - This seems to work for me only when my env file is in the project root directory, which is also where I'm running the command sam local start-api --env-vars=.env.json
It also would not work until I set blank Environment Variables in the template.yml.
It would be nice to be able to use ENV variables only for local development, without having to set them on the function. 🤷♂
from aws-sam-cli.
I'm encountering this issue with 0.2.8. Can anyone repro?
from aws-sam-cli.
Encountering it in 0.5.0 version too :-/
from aws-sam-cli.
@david-christensen Thanks a lot for you post. That workaround worked for me as well!
from aws-sam-cli.
That's weird. Are you using the same env-vars.json as the blog post?
from aws-sam-cli.
Docker inspect confirms on OSX Docker that no envs are passed to container. Triple checked env.json and template.yaml file match.
If passed as sam commnad line it appears on docker environment and of course works also for OS.getenv in lambda function.
{
"VoteSpacesTabs": {
"TABLE_NAME": "spaces-tabs-votes",
"BUCKET_NAME": "mybucket"
}
}
AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
VotesTable:
Type: "AWS::Serverless::SimpleTable"
VoteSpacesTabs:
Type: "AWS::Serverless::Function"
Properties:
Runtime: python3.6
Handler: lambda_function.lambda_handler
Policies: AmazonDynamoDBFullAccess
Timeout: 10
Environment:
Variables:
TABLE_NAME: !Ref VotesTable
Events:
Vote:
Type: Api
Properties:
Path: /
Method: post
from aws-sam-cli.
I believe this is fixed in the develop branch, but just hasn't been released.
Pull request: #29
Commit: PaulMaddox@5f6bb38
from aws-sam-cli.
Okay, this makes sense. I will release new build to NPM today.
from aws-sam-cli.
Duplicate of #28
from aws-sam-cli.
Same problem here with 0.6.0
from aws-sam-cli.
Re-opening because people are hitting this bug again
from aws-sam-cli.
Looking at the fix for original bug (#28 - Must use function logicalId to get env vars from EnvVars.json), I don't think you guys are hitting the same bug. We have an integration test for this issue that is passing:
Test: https://github.com/awslabs/aws-sam-cli/blob/develop/tests/integration/local/invoke/test_integrations_cli.py#L68-L77
SAM Template used in the test: https://github.com/awslabs/aws-sam-cli/blob/develop/tests/integration/testdata/invoke/template.yml#L45-L53
Env Vars file used in test: https://github.com/awslabs/aws-sam-cli/blob/develop/tests/integration/testdata/invoke/vars.json
Can you @sashokbg or others post a sample to repro the issue?
NOTE: SAM CLI will pass env vars that are defined in the function. ie. If your function has defined variable Foo
, but in your env-var.json you are passing value for variable Bar
, then your container will not get the value for Bar
.
from aws-sam-cli.
Hello @sanathkr
I just did a test and it worked !
What I missed was that I needed to declare the environment variable in my function.
It does not just pass any env variable it finds.
Do you think we should probably update the readme ?
Thank you for your help !
Best regards
Alex
from aws-sam-cli.
No more docs?
from aws-sam-cli.
It's not working. Is there any FULL example?
from aws-sam-cli.
This should help @scorphus, https://github.com/awslabs/aws-sam-cli/blob/develop/tests/integration/testdata/invoke/vars.json
from aws-sam-cli.
@1nfility thank you for that. you really saved my life man. That one really works.
from aws-sam-cli.
Related Issues (20)
- SAM build still fails for go with `--use-container` HOT 7
- posted prematurely ... more testing required HOT 1
- Bug: Windows deploys broken because CRLF not converted to LF for bash/shell scripts HOT 3
- Bug: sam build - FileNotFoundError, file is part of aws-sdk node module HOT 3
- Bug: sam init - AttributeError HOT 2
- Provide proper release artifacts HOT 4
- Deploy command freezes when tag containing space is provided in samconfig file HOT 2
- Bug: Unable to debug typescript sam app via VScode breakpoints HOT 2
- Concurrent requests to `sam local start-api` cause crash in _clean_decompressed_paths function and 502 in browser HOT 4
- Unable to upload artifact using SAM CLI Version 1.109.0 HOT 15
- An HTTP Client raised an unhandled exception: sequence item 0: expected str instance, bytes found HOT 4
- Is FullAccess Role secure? HOT 3
- Bug: sam local invoke - KeyError HOT 1
- Bug: sam local invoke - ReadTimeout HOT 2
- Prevent Image Build/Rebuild for sam local start-api ? HOT 1
- Bug: sam local start-api --warm-containers doesn't work on docker HOT 8
- Bug: Cannot push image to public Repository on ECR HOT 3
- Bug: Cannot resolve http://host.docker.internal when using --warm-containers HOT 3
- Bug: sam local invoke throws exception: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: HOT 10
- Mistakingly created HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aws-sam-cli.