Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access an AWS Lambda environment variable from Python

Using the new environment variable support in AWS Lambda, I've added an env var via the webui for my function.

How do I access this from Python? I tried:

import os  MY_ENV_VAR = os.environ['MY_ENV_VAR'] 

but my function stopped working (if I hard code the relevant value for MY_ENV_VAR it works fine).

like image 741
keybits Avatar asked Dec 02 '16 17:12

keybits


People also ask

How do you view an environment variable in Python?

Read Environment Variables in Python: The os module will require to import to read the environment variables. The os. environ object is used in Python to access the environment variable. The coder can set and get the value of any environment variable by using this object.

How do I use Python code in AWS Lambda?

In AWS main page, under “Find services”, search for lambda and select it. In Lambda's main page, select Author from scratch and give a name to the function. Under Runtime, select the Python version you need to run your script. Click on “Create function”.


2 Answers

AWS Lambda environment variables can be defined using the AWS Console, CLI, or SDKs. This is how you would define an AWS Lambda that uses an LD_LIBRARY_PATH environment variable using AWS CLI:

aws lambda create-function \   --region us-east-1   --function-name myTestFunction   --zip-file fileb://path/package.zip   --role role-arn   --environment Variables={LD_LIBRARY_PATH=/usr/bin/test/lib64}   --handler index.handler   --runtime nodejs4.3   --profile default 

Once created, environment variables can be read using the support your language provides for accessing the environment, e.g. using process.env for Node.js. When using Python, you would need to import the os library, like in the following example:

... import os ... print("environment variable: " + os.environ['variable']) 

Resource Link:

AWS Lambda Now Supports Environment Variables



Assuming you have created the .env file along-side your settings module.

. ├── .env └── settings.py 

Add the following code to your settings.py

# settings.py from os.path import join, dirname from dotenv import load_dotenv  dotenv_path = join(dirname(__file__), '.env') load_dotenv(dotenv_path) 

Alternatively, you can use find_dotenv() method that will try to find a .env file by (a) guessing where to start using file or the working directory -- allowing this to work in non-file contexts such as IPython notebooks and the REPL, and then (b) walking up the directory tree looking for the specified file -- called .env by default.

from dotenv import load_dotenv, find_dotenv load_dotenv(find_dotenv()) 

Now, you can access the variables either from system environment variable or loaded from .env file.

Resource Link:

https://github.com/theskumar/python-dotenv



gepoggio answered in this post: https://github.com/serverless/serverless/issues/577#issuecomment-192781002

A workaround is to use python-dotenv: https://github.com/theskumar/python-dotenv

import os import dotenv  dotenv.load_dotenv(os.path.join(here, "../.env")) dotenv.load_dotenv(os.path.join(here, "../../.env")) 

It tries to load it twice because when ran locally it's in project/.env and when running un Lambda the .env is located in project/component/.env

like image 187
SkyWalker Avatar answered Sep 23 '22 09:09

SkyWalker


Both

import os os.getenv('MY_ENV_VAR') 

And

os.environ['MY_ENV_VAR'] 

are feasible solutions, just make sure in the lambda GUI that the ENV variables are actually there.

like image 36
Nicolò Gasparini Avatar answered Sep 22 '22 09:09

Nicolò Gasparini