Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: sam local does not load environment variables through .env #6015

Closed
stevemao opened this issue Sep 30, 2023 · 3 comments
Closed

Bug: sam local does not load environment variables through .env #6015

stevemao opened this issue Sep 30, 2023 · 3 comments
Labels
stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at.

Comments

@stevemao
Copy link

stevemao commented Sep 30, 2023

Description:

when I run sam local start-api -t ./cdk.out/Stack.template.json --log-file logfile.txt, i see a warning

* Tip: There are .env or .flaskenv files present. Do "pip install python-dotenv" to use them.

So I want sam to load my environment variables from .env

Steps to reproduce:

run pip install python-dotenv

run sam local start-api -t ./cdk.out/Stack.template.json --log-file logfile.txt

do a sample request to this API.

Observed result:

.env environment variables are not loaded

Expected result:

.env environment variables are loaded

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically. If you
used sam build before running local commands, you will need to re-run sam build for the changes to be picked up. You only need to restart SAM CLI if you update your AWS SAM template
 * Tip: There are .env or .flaskenv files present. Do "pip install python-dotenv" to use them.
2023-09-30 15:58:14 WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:3000
2023-09-30 15:58:14 Press CTRL+C to quit
Invoking index.botInstalled (nodejs18.x)
Local image is up-to-date
Using local image: public.ecr.aws/lambda/nodejs:18-rapid-x86_64.
  1. OS: mac
  2. sam --version: SAM CLI, version 1.97.0
  3. AWS region: sydney AU
# Paste the output of `sam --info` here
{
  "version": "1.97.0",
  "system": {
    "python": "3.11.5",
    "os": "macOS-12.6.9-x86_64-i386-64bit"
  },
  "additional_dependencies": {
    "docker_engine": "24.0.6",
    "aws_cdk": "2.95.1 (build ae455d8)",
    "terraform": "1.5.6"
  },
  "available_beta_feature_env_vars": [
    "SAM_CLI_BETA_FEATURES",
    "SAM_CLI_BETA_BUILD_PERFORMANCE",
    "SAM_CLI_BETA_TERRAFORM_SUPPORT",
    "SAM_CLI_BETA_RUST_CARGO_LAMBDA"
  ]
}

Add --debug flag to command you are running

@stevemao stevemao added the stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. label Sep 30, 2023
@jfuss
Copy link
Contributor

jfuss commented Oct 2, 2023

@stevemao Thanks for the report. Not sure where "* Tip: There are .env or .flaskenv files present. Do "pip install python-dotenv" to use them." is coming from, maybe flask. Regardless, SAM CLI does not read of use .env files. You can however use the template to define the Environment Variables for your Lambda function and use --env-vars to set them. We do not set any env vars in the Lambda function unless they are defined in the template. More details can be found here: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-using-invoke.html#serverless-sam-cli-using-invoke-environment-file

Closing as it is a duplicate of #1355

@jfuss jfuss closed this as completed Oct 2, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Oct 2, 2023

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@stevemao
Copy link
Author

stevemao commented Oct 3, 2023

@jfuss thanks for the response. As an end-user, I only know this sentence comes from aws-sam-cli. You should suppress this message when your internal lib/package prints it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at.
Projects
None yet
Development

No branches or pull requests

2 participants