Lambda has quickly become a core service in the AWS lineup. A utility knife for developers, Lambda can be as simple or complex as needed. It is the glue between AWS resources, a simple event-based function, or the workhorse for a full serverless application. However, with great power comes great responsibility.  One of those responsibilities is to understand Lambda’s at least once execution policy.

One often overlooked requirement for Lambda functions is that they must be idempotent.  Every invocation has a chance of running multiple times, regardless of successful or failed executions.

Invocations occur at least once in response to an event and functions must be idempotent to handle this.

For workloads that always produce the same artifacts in the same location, there may be no reason to worry about this requirement. However, for workloads that create new artifacts or resources on each execution, this becomes problematic. Multiple create statements or API calls with no correlation mechanism will result in an equal number of artifacts and resources upon completion. If those outputs also incur cost, not accounting for this “at least once” approach will go straight to the bottom line.

We first encountered this phenomenon when working with Spotinst to create spotinst-lambda, a project based on lambda-formation. The integration allows CloudFormation to manage Spotinst Elastigroups as it would any other AWS resource. Create, Update and Delete actions from CloudFormation invoke Lambda functions that communicate with the Spotinst API, ultimately returning status back to CloudFormation. Spotinst Elastigroups show up alongside all other AWS resources with the same look and feel as the rest of our AWS environment.

At scale, we noticed random successful executions would create multiple Elastigroups. This would double or triple the infrastructure needed for a CloudFormation Stack. First, we exhaustively tested the code to ensure it was not part of the lambda-formation stack. Spotinst also confirmed from their side that the API was acting correctly. Finally, we isolated Lambda as the root cause. Spotinst quickly updated their API to correlate duplicate requests, which ultimately fixed the problem.

One extremely valuable resource in our debugging endeavors was the Serverless Forum Slack channel .  Many helpful hands were willing to dig into architecture, logs and code as we figured out edge cases in the #debug-buffet channel.

Although the solution to correlate API requests was not difficult to implement, we wanted to dig deeper into how often multiple executions were occurring. We created a test project that received CloudFormation events and sent API calls normally intended for Spotinst to AWS API Gateway. The API Gateway called mocked services to return successful results for Create and Delete actions. All invokes were then tracked with unique short IDs and atomic counters in DynamoDB.

With the mock service in place, we scripted CloudFormation to continually run five concurrent stack creations for an entire month. After a successful creation, the script would also delete the stack so that we stayed under any soft limits and kept the stack list as clean as possible.

The following is an example of a successful Lambda function running twice. Notice that the eventId is the same for both events. Without the function reporting itself to DynamoDB, the only way to differentiate the executions is by the ingestionTime and timestamp fields.

"events": [

"ingestionTime": 1487052414069,

"timestamp": 1487052414054,

"message": "2017-02-14T06:06:54.054Z\tc87ea13a-f27b-11e6-9ccd-bdcd7bdb14b3\tCREATE SUCCESSFUL:mock-elastigroup-HketRflYl\n",

"eventId": "33162376982678112638063343232696884515265773774940209152",

"logStreamName": "2017/02/14/[$LATEST]e1e5e62b2b90474cac7a7e84501e06d7"



"ingestionTime": 1487053014052,

"timestamp": 1487053014045,

"message": "2017-02-14T06:16:54.045Z\t2e1c509e-f27d-11e6-bbbd-eb040bc485ea\tMULTIPLE CREATE: mock-elastigroup-HketRflYl 2\n",

"eventId": "33162390362924525049650453271189028336502854056219901952",

"logStreamName": "2017/02/14/[$LATEST]e1e5e62b2b90474cac7a7e84501e06d7"



Dashboard of all create function executions, with count on the y-axis and 24-hour time intervals on the x-axis.

Over the course of the month, 491,699 create functions were executed resulting in 112 multiple invokes. Of the 112, only three caused triple executions, and none occurred four or more times. While a 0.02% multiple execution rate is extremely low, it becomes instantly noticeable when the result is two or three times more infrastructure and spend than originally intended.

Going forward, know that Lambda will eventually execute functions more than once. Whether you use correlation IDs, atomic counters or another de-duplication pattern, you could be creating multiple rows in a database, multiple resources or twice as many artifacts as intended. The documentation calls this behavior out, but I’ve rarely seen it accounted for in the many examples I’ve run across to date.

Kevin is a member of the CTO Architecture Team, which is responsible for researching and evaluating innovative technologies and processes. He has a strong, extensive background in DevOps specifically in developing tools for Operations Support Systems (OSS) within the communications and IT infrastructure industry.

Before arriving at Sungard Availability Services, Kevin spent 13 years at AT&T (by way of AT&T’s acquisition of USi) rising through the engineering ranks to become a Senior Technical Architect for the Hosting Division’s Tools Team.

Kevin holds a B.A. in Economics from the University of Maryland and a Masters in Technology Management from University of Maryland University College.


  • Alex

    Alex Alex

    Reply Author

    Just a simple correction. You mentioned that the eventId s of were the same, but they were not in the snapshot. The first 6 digits are but the rest of the id is different. Is this the wrong snapshot?

    Posted on

    • Kevin McGrath

      Great catch! I need to see if I still have the logs sitting around from this test. It appears something did get lost in translation. I had multiple examples before writing the blog and may have fat fingered a copy.

      If I can I’ll dig back in an double check. Thank you for the comment.

      Posted on