Coder Social home page Coder Social logo

Comments (20)

1nfility avatar 1nfility commented on May 12, 2024 33

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.

sanathkr avatar sanathkr commented on May 12, 2024 7

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.

kenjim83 avatar kenjim83 commented on May 12, 2024 7

@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.

david-christensen avatar david-christensen commented on May 12, 2024 5

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.

nibab avatar nibab commented on May 12, 2024 4

I'm encountering this issue with 0.2.8. Can anyone repro?

from aws-sam-cli.

stephen84s avatar stephen84s commented on May 12, 2024 2

Encountering it in 0.5.0 version too :-/

from aws-sam-cli.

mrvladis avatar mrvladis commented on May 12, 2024 2

@david-christensen Thanks a lot for you post. That workaround worked for me as well!

from aws-sam-cli.

sanathkr avatar sanathkr commented on May 12, 2024

That's weird. Are you using the same env-vars.json as the blog post?

from aws-sam-cli.

heikkis avatar heikkis commented on May 12, 2024

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.

PaulMaddox avatar PaulMaddox commented on May 12, 2024

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.

sanathkr avatar sanathkr commented on May 12, 2024

Okay, this makes sense. I will release new build to NPM today.

from aws-sam-cli.

sanathkr avatar sanathkr commented on May 12, 2024

Duplicate of #28

from aws-sam-cli.

sashokbg avatar sashokbg commented on May 12, 2024

Same problem here with 0.6.0

from aws-sam-cli.

sanathkr avatar sanathkr commented on May 12, 2024

Re-opening because people are hitting this bug again

from aws-sam-cli.

sanathkr avatar sanathkr commented on May 12, 2024

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.

sashokbg avatar sashokbg commented on May 12, 2024

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.

scorphus avatar scorphus commented on May 12, 2024

No more docs?

from aws-sam-cli.

scorphus avatar scorphus commented on May 12, 2024

It's not working. Is there any FULL example?

from aws-sam-cli.

lukeshay avatar lukeshay commented on May 12, 2024

This should help @scorphus, https://github.com/awslabs/aws-sam-cli/blob/develop/tests/integration/testdata/invoke/vars.json

from aws-sam-cli.

9oelM avatar 9oelM commented on May 12, 2024

@1nfility thank you for that. you really saved my life man. That one really works.

from aws-sam-cli.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.