简体   繁体   中英

How do you set an environment variable within a c# AWS lambda function?

I have a lambda function that sends out a message to AWS SNS when certain conditions appear in a SQL database. This lambda function fires every 5 minutes to check for this condition.

It works fine, and sends the appropriate message. However, once it has sent the message I don't want it to send it every 5 minutes until it is resolved. I would rather it only send out the message every 5 hours after that. Mainly because if I'm away on the weekend I don't want to be bombarded with an email or text every 5 minutes when I'm away from my computer.

One possible solution was to have an environment variable that stores the last time a message was sent, LastTimeMessageSent. Getting the value of the environment variable is easy and works fine

Environment.GetEnvironmentVariable("LastTimeMessageSent");

However, when I do this

Environment.SetEnvironmentVariable("LastTimeMessageSent", DateTime.UtcNow.ToString());

It doesn't work. No exception or error message. I can't seem to find anywhere in the documentation or the web that describes how to do this or explains why this wouldn't work.

How do I set the environment variable? If I'm not able to do that inside the lambda what is the correct way to accomplish what I need to accomplish?

AWS Lambda functions run in a temporary container environment. They can also run in parallel across many host systems.

While a Lambda function can access environment variables, these are specifically set when the container is created for the Lambda function. Once the function has finished running, the environment might be thrown away . (If a function is invoked often enough, the environment might be kept for future executions.)

Thus, using environment variables is not a way to persist data.

Some other options are:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM