Reputation: 47
I am writing a Cloud Formation template for creating Lambda function and the "Environment" param can have any number of variables. ( not known before hand) How can I make this as dynamic rather than specifying static key value pairs.
Upvotes: 1
Views: 1743
Reputation: 2060
You can't have dynamic environment variables for Lambda as samtoddler
already pointed out. However, there's a simple alternative:
Use environment variables to specify a path to your config, e.g. the name of a parameter store value from Systems Manager.
In your Lambda function, read the environment variable and retrieve the actual value from parameter store. This can be a JSON string or whatever configuration settings you need that you can serialize into a string. Optimization: only retrieve the value when your Lambda function is invoked the first time and cache it outside of your handler function.
If you need to change your configuration, simply update the value in parameter store. Hint: If you need to distribute this new value as fast as possible to your Lambda function, just create a new version of your Lambda function and use this new version. It ensures that new instances are created the next time your Lambda function is called for this new version and then, your new parameter store values are used.
There are a few more details with a very similar scenario in this blog post by AWS: https://aws.amazon.com/blogs/compute/sharing-secrets-with-aws-lambda-using-aws-systems-manager-parameter-store/
Upvotes: 1
Reputation: 9625
You can't dynamically change them.
Using AWS Lambda environment variables
When you publish a version, the environment variables are locked for that version along with other version-specific configuration.
Upvotes: 0