Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set or modify an AWS Lambda environment variable with Python boto3

i want to set or modify an environment variable in my lambda script. I need to save a value for the next call of my script. For exemple i create an environment variable with the aws lambda console and don't set value. After that i try this :

import boto3
import os

if os.environ['ENV_VAR']:
   print(os.environ['ENV_VAR'])
os.environ['ENV_VAR'] = "new value"

In this case my value will never print. I tried with :

os.putenv()

but it's the same result. Do you know why this environment variable is not set ?

Thank you !

like image 257
palaho Avatar asked Dec 23 '16 15:12

palaho


People also ask

Can you use Boto3 in Lambda?

It can do anything from providing web pages and processing data streams to using APIs and connecting with other AWS and non-AWS services. To accomplish our aim by Working with Boto3 Lambda (AWS), doing some data wrangling, and saving the metrics and charts on report files on an S3 bucket.

Can you set environment variables in Python?

With python code, environment variables can be set and manipulated. Setting the environment variable with code makes it more secure and it does not affect the running python script.

How do I change the runtime in Python Lambda?

To change the runtime, you create a new container image. When you use a . zip file archive for the deployment package, you choose a runtime when you create the function. To change the runtime, you can update your function's configuration.


2 Answers

Consider using the boto3 lambda command, update_function_configuration to update the environment variable.

response = client.update_function_configuration(
            FunctionName='test-env-var',
            Environment={
                'Variables': {
                    'env_var': 'hello'
                }
            }
        )
like image 193
Jason Avatar answered Sep 25 '22 08:09

Jason


I need to save a value for the next call of my script.

That's not how environment variables work, nor is it how lambda works. Environment variables cannot be set in a child process for the parent - a process can only set environment variables in its own and child process environments.

This may be confusing to you if you set environment variables at the shell, but in that case, the shell is the long running process setting and getting your environment variables, not the programs it calls.

Consider this example:

from os import environ
print environ['A']
environ['A'] = "Set from python"
print environ['A']

This will only set env A for itself. If you run it several times, the initial value of A is always the shell's value, never the value python sets.

$ export A="set from bash"
$ python t.py
set from bash
Set from python
$ python t.py
set from bash
Set from python

Further, even if that wasn't the case, it wouldn't work reliably with aws lambda. Lambda runs your code on whatever compute resources are available at the time; it will typically cache runtimes for frequently executed functions, so in these cases data could be written to the filesystem to preserve it. But if the next invocation wasn't run in that runtime, your data would be lost.

For your needs, you want to preserve your data outside the lambda. Some obvious options are: write to s3, write to dynamo, or, write to sqs. The next invocation would read from that location, achieving the desired result.

like image 32
Daniel Farrell Avatar answered Sep 22 '22 08:09

Daniel Farrell