AWS Builder Center

A race-free "commit confirmed" with DynamoDB conditional writes

In our hackathon project, Deadman, a change to an AWS security group is applied and then reverted on a timer unless a person confirms. I wrote the core logic in Python Lambda functions, and the hard part was making sure confirm and revert can never both win.

STUDENT
The design: one DynamoDB item per change, with a status that only moves along allowed transitions. Every transition is a conditional write, for example "set CONFIRMED only if the status is PENDING and the window has not expired". If two callers race, exactly one write succeeds and the other re-reads the item. A second item per security group acts as a lock, so only one active change can exist at a time.
The order matters: we create the timer before changing the firewall, so a failure to schedule can never leave a change with no safety net. Revert undoes only the rules Deadman itself changed.
I tested with mocks, and also checked every AWS request against the real API models offline with botocore's Stubber, which catches misspelled parameters. But mocks did not catch everything: on the real stack an environment variable name in our template did not match the one in my code, so a Lambda passed an empty function ARN to Scheduler. Lesson: tests on mocks show your code agrees with itself, and only the real cloud shows whether it agrees with your infrastructure.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article