palaho
palaho

Reputation: 93

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 !

Upvotes: 8

Views: 17688

Answers (3)

Jason
Jason

Reputation: 159

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'
                }
            }
        )

Upvotes: 15

Karan B
Karan B

Reputation: 21

AWS Lambda just executes the piece of code with given set of inputs. Once executed, it returns the output and that's all. If you want to preserve the output for your next call, then you probably need to store that in DB or Queue as Dan said. I personally use SQS in conjunction with SNS that sends me notifications about current state. You can even store the end result like success or failure in SQS which you can use for next trigger. Just throwing the options here, rest all depends on your requirements.

Upvotes: 2

erik258
erik258

Reputation: 16305

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.

Upvotes: 7

Related Questions