The Python Oracle

Can we set the environment variable of AWS python lambda on invocation

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Drifting Through My Dreams

--

Chapters
00:00 Question
01:07 Accepted answer (Score 3)
02:14 Answer 2 (Score 0)
02:29 Thank you

--

Full question
https://stackoverflow.com/questions/6410...

Accepted answer links:
[update-function-configuration]: https://awscli.amazonaws.com/v2/document...
[AWS Systems Manager Parameter Store]: https://docs.aws.amazon.com/systems-mana...
[AWS Lambda execution context]: https://docs.aws.amazon.com/lambda/lates...

Answer 2 links:
https://docs.aws.amazon.com/systems-mana...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #amazonwebservices #awslambda #environmentvariables

#avk47



ACCEPTED ANSWER

Score 3


Sadly you can't set the environment variables during invocation, as they are part of lambda's configuration. Thus you can use update-function-configuration API call to set their values in unpublished function only. If the function is published (i.e. it has a version) then the environment variables are immutable.

As a workaround you can store variable SERVICE_UP externally, e.g. in AWS Systems Manager Parameter Store to persist its value between invocations.

The other workaround, if your function is unpublished, would be to use AWS SDK in the function to call update-function-configuration so that the function updates its own environment variables.

For short term storage of the SERVICE_UP value, you could use AWS Lambda execution context which persists between function executions for some time. But its not permanent.




ANSWER 2

Score 0


You can use AWS param store and keep this flag there.

See https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html