73

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

keybits
  • 4,383
  • 2
  • 20
  • 18
  • 2
    Are you sure that you're env var is actually set in the process that's running the script and not just in another process where you have a terminal open? – Charles D Pantoga Dec 02 '16 at 18:01
  • Can you give us some more insight? I just tried it with a little script like this: import os def lambda_handler(event, context): my_env_var = os.environ['MY_ENV_VAR'] print my_env_var print str(type(my_env_var)) and works fine returning a string type variable. If you tell me a bit more maybe I can help. – Daniel Cortés Jan 23 '17 at 17:51
  • @DanielCortés Thanks for the comment. I'm not setting `MY_ENV_VAR = os.environ['MY_ENV_VAR']` from within the `lambda_handler` function. Might that be the problem? Can you try setting `MY_ENV_VAR` at the module level and see if that works? – keybits Jan 28 '17 at 10:53
  • 1
    @keybits Just tried that and still worked. I'll write an answer to include my code and screenshots of the lambda configuration, maybe something there might help you. – Daniel Cortés Jan 31 '17 at 15:04
  • Did any answer solve the problem? – Efren Jul 05 '18 at 07:41

4 Answers4

72

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

SkyWalker
  • 28,384
  • 14
  • 74
  • 132
  • 1
    The point of using environment variables here is usually to separate sensitive information, such as credentials for remote service access, from the code which gets stored in revision control for anyone to see. It also allows runtime composition of these values so prod can have different settings to test, for example. Stuffing them into a file along with the code is only slightly more useful than hard coding. – Dave Nov 26 '19 at 01:30
19

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.

Nicolò Gasparini
  • 2,228
  • 2
  • 24
  • 53
12

I used this code; it includes both cases, setting the variable from the handler and setting it from outside the handler.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Trying new lambda stuff"""
import os
import configparser

class BqEnv(object):
    """Env and self variables settings"""
    def __init__(self, service_account, configfile=None):
        config = self.parseconfig(configfile)
        self.env = config
        self.service_account = service_account

    @staticmethod
    def parseconfig(configfile):
        """Connection and conf parser"""
        config = configparser.ConfigParser()
        config.read(configfile)
        env = config.get('BigQuery', 'env')
        return env

    def variable_tests(self):
        """Trying conf as a lambda variable"""
        my_env_var = os.environ['MY_ENV_VAR']
        print my_env_var
        print self.env
        return True

def lambda_handler(event, context):
    """Trying env variables."""
    print event
    configfile = os.environ['CONFIG_FILE']
    print configfile
    print type(str(configfile))
    bqm = BqEnv('some-json.json', configfile)
    bqm.variable_tests()
    return True

I tried this with a demo config file that has this:

[BigQuery]
env = prod

And the setting on lambda was the following: env variables

Hope this can help!

Daniel Cortés
  • 486
  • 7
  • 13
1
os.environ["variable_name"]

In the configuration section of AWS lambda, make sure you declare the variable with the same name that you're trying to access here. For this example, it should be variable_name