3

I have written a lambda function which does some processing.

There is 1 environment variable which is set by default.

Is there a way I can change it after every run ?

12
  • 2
    You can change your environment variable by hand or programmatically with a script, but storing dynamic data is not what environment variables are for, you usually use some sort of database for that. What is your exact use case for wanting to change it at every run ? Commented Oct 17, 2019 at 6:30
  • 1
    @giuseppedeponte But is it a good idea to use database for single variable ? Eg. Let's say i am maintaining a counter (i =0 ) how many times my lambda function run. In my nodejs lambda script i want to do i++ so that my Env variable increment by 1. Any link to how i can change AWS lambda environment variable ? Commented Oct 17, 2019 at 8:11
  • 1
    Some sort of database... Could be just a file, like a JSON or CSV or just text file. Commented Oct 17, 2019 at 8:18
  • 1
    In your example ‘using a counter to check how mamy times a lamda is run’, you can just have a normal counter in your code but outside of the lambda handler. That allows you to keep all sorts of state (eg: in-process cache, etc) between lambda invocations on the same lambda instance. If you need to preserve state over different lambda instances, use a DB. Commented Oct 17, 2019 at 9:26
  • 1
    you can store parameter, it will accessible over different lamda function and you can also deal with store parameter as an ENV and it will also work accrod different lamda. storing outside in the event hanlder may work but it will lost once you update the lamda function. also you will not bother with DB Commented Oct 17, 2019 at 9:44

2 Answers 2

3

If you are not happy with DB, as suggested in comment then you can use AWS store parameter which will answer your this query.

way to modify environment variable in AWS Lambda?

You can consume Your environment variable from the store parameter, also it will keep the state across different lambda function, Variable outside handle may work as suggested @Jan, but what if you update the lambda function?

So, for example, the flow will be

If store-paramter == true;
 #do the job,after job done
 #update store-paramter value
 store-paramter=false
else
 #play with with value

once you generate secret in secretsmanager , AWS will popup with complete code in a different language just copy the code and paste it in lambda that sample but you should assign permission to lambda.

Also, you explore a handy npm package aws-param-store.

BTW application should not update ENV, but to deal with your use case you can follow.

You can check this article too from scratch how to set and consume secret in lambda.

Sign up to request clarification or add additional context in comments.

Comments

1

Environment variables are made to pass settings to your Lambda. They aren't the right tool for keeping your state.

DynamoDB is a serverless database made exactly for that purpose. It is easy to create a table that will store just one value.

Within free tier you can get 25GBs and opportunity to make up to 25 write/read requests a second.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.