Here is a situation I have seen more than once: a scheduled Lambda function creates invoices every night, and one morning a customer has two identical invoices. Nobody deployed anything unusual. The function simply ran twice, and both runs did their job perfectly.
That is not a bug in Lambda. Asynchronous invocations are retried on failure, the same event can arrive more than once even without an error, and sometimes a colleague starts a run manually because it "looks stuck". Each case is rare, but together they are more common than you would hope. In my article on building a DynamoDB lease lock for Lambda I walk through a complete, tested solution in TypeScript. This post explains the core ideas.
First ask whether you need a lock at all
A distributed lock is extra coordination you have to test, monitor and explain.β¦