AWS lambda functions and internal reusable containers
Some time has happened from the last post, but today I have some information that is not relevant to anyone and that's the main purpose of this blog.
Okay this is the deal. I was following one of the amazon web services posts on how-to-receive-alerts-when-specific-apis-are-called-by-using-aws-cloudtrail-amazon-sns-and-aws-lambda. Translated into "my grandmother" language: being alerted when some AWS events are happening.
The flow on how the services work together is shown on this diagram:
But why am I writing about this? Well I've faced an issue while building this setup, actually opened a support case with amazon and since I'm not an expert with AWS lambda, they clarified to me some of the lambda nature that cause my function not to work properly.
The expectation: Receive an email once an event happened on AWS coming from route53 each time a bucket object was created
The issue: The email was sent only for the first time the lambda function was invoked, but it didn't send an email on second time and any other subsecuent time
The problem: AWS Lambda reuse their containers to run the code, and the environment variables as well. The cloudtrail.js code use a FILTER_CONFIG global variable, and the logic of the code says that it will run some code if FILTER_CONFIG is empty. But since this global variable is kept on the container, none of the following code is being executed.
The solution: Set the FILTER_CONFIG as empty just at the beginning of the init function.
After doing this change the lambda function worked just like a charm.