I have a very simply lambda function written in Python that I deployed as a Docker image in Elastic Container Registry (ECR).
It performs a very simple task and the handler looks like this:
def handler(event, context):
...
__send_email(msg)
handler(None, None)
I am happy with the results, there are no errors in the code and I get my successful e-mail every time it runs.
However, it always triggers twice when I click test.
I tried two things to fix it. First I set the timeout to 5 minutes (it takes about 2.5 seconds to run in most cases). Next I set the asynchronous invocation number of retry attempts to zero. Neither of these to resolved the issue.
But what really confuses me is that I don't see any duplicate calls in the logs. This is all I get when I run it:
START RequestId: 892f1091-92be-4360-be9e-0707c268d127 Version: $LATEST
END RequestId: 892f1091-92be-4360-be9e-0707c268d127
REPORT RequestId: 892f1091-92be-4360-be9e-0707c268d127 Duration: 1180.98 ms Billed Duration: 2416 ms Memory Size: 128 MB Max Memory Used: 46 MB Init Duration: 1234.33 ms
It clearly is not overrunning the five minute timeout. I've read about idempotency, but I'm not seeing how that can be an issue here since all I'm doing is manually clicking test, and I also don't see any log of a duplicate run.
What am I missing and what am I doing wrong?
CodePudding user response:
When an AWS Lambda function is triggered, Amazon calls this function:
def handler(event, context):
You are also directly calling that function:
handler(None, None)
Thus, your handler function is running twice.
If you want to be able to test your function locally, without having this unwanted side effect when it is deployed to AWS, then you need to add a check in your code to detect if it is running in AWS. That would look like the following:
is_lambda = os.environ.get("AWS_EXECUTION_ENV") is not None
if not is_lambda:
handler(None, None)