Picture a lambda function which simply spits out a color "red", "blue" etc all day via an api gateway, imagine it being hit 1000s of times an hour.
The current value, "green" say, is changed by other business processes. Imagine it being changed once or twice a day.
This would be a trivial lambda function and the ways I know how to do it are
-
Using dynamoDB (or any of the DB) have a trivial table that has one value, being "current color". The function would simply get the value from the DB and return the text, say "yellow".
-
Have a text file on an S3 bucket. It would just have one line, one word, say "yellow". The function would simply read the text from the text file and return the text, say "yellow".
(The other business processes that occasionally change the color, would simply write to either the DB or the plain text file.)
In fact is there a simpler way to do this??
-
is there some sort of "setting" system for this purpose where a value can be "known immediately by all the running instances of your lambda"
-
or some system essentially like keeping a value live in memory, for lambda functions?
Is there a faster / cheaper way to have "some live value" known to one of your lambda functions?
2
Answers
It would generally depend on your full architecture.
If the lambda is invoked by another process, in other words, it reacts to an external trigger, you can pass the "color" to the event that triggers the lambda and read it from the event JSON.
Suppose the lambda is triggered on schedule or by an unrelated process unaware of the "color". In that case, the AWS SSM Parameter Store is a fitting and inexpensive option compared to DynamoDB: pricing. This is especially true if the parameter isn’t sensitive and doesn’t need to be encrypted.
If it’s simple (e.g. color=red/blue/green) and changes infrequently (e.g. twice per day) then you might consider simply making it an environment variable for the Lambda function. That way you don’t have any external storage and you don’t need to suffer the costs associated with DynamoDB or Parameter Store queries or storage and you don’t need to worry about AWS service API throttling.
Simply update the environment variable value and re-deploy the Lambda function as needed. The Lambda service will dispose of all existing warm Lambda execution environments and trigger a cold start on the next invocation of the Lambda function.