AWS Builder Center
Weekend Agent Challenge: CloudFormation Drift Sync Agent

Weekend Agent Challenge: CloudFormation Drift Sync Agent

Infrastructure changes made outside CloudFormation can quickly create configuration drift. My AI-powered agent identifies these changes and automatically synchronizes the CloudFormation templates.

CloudFormation Drift Sync Agent - Infrastructure that corrects itself

An autonomous AI agent that detects AWS configuration drift and keeps your CloudFormation template in sync — automatically.




Vision & What the Agent Does

CloudFormation is supposed to be the single source of truth for your AWS infrastructure. In reality, people make quick changes straight from the AWS Console or CLI - flip an S3 bucket's versioning, tweak an SNS topic's display name, widen a security group - to test something or fix an urgent issue. Over time those out-of-band edits pile up and the live environment stops matching the template. That gap is called configuration drift, and it's how outages and security holes quietly sneak in.
Most tools stop at detecting drift and emailing you a report. My agent goes further: it keeps the CloudFormation template in sync with the live environment automatically.

From a user's point of view:

  • What triggers it (no button clicks): The agent runs unattended in two ways. Any change made in the Console or CLI is recorded by CloudTrail and delivered to the agent through EventBridge in near real-time. As a safety net, an EventBridge Scheduler runs a reconciliation sweep every 6 hours to catch anything missed.
  • What it does on its own: It detects the change, checks whether the resource is actually managed by CloudFormation, uses an AI model to explain what changed and why it matters, and generates the corrected CloudFormation template that matches reality - accurately, for almost any resource type.
  • How it reports back: It emails the owner. Low-risk (cosmetic) changes are auto-applied and synced into the template with a confirmation email. Higher-risk changes are held and emailed with a plain-language summary plus a diff and one-click Approve / Reject links - nothing touches the live stack until a human approves.
The result: your infrastructure-as-code stays honest, with a human in the loop exactly where it matters.

Two Things I Tested Live

I deployed a small demo stack — an S3 bucket, an SNS topic, and an SQS queue — then made real changes and watched the agent handle them.


Demo 1 — A cosmetic change from the CLI → auto-applied

  • The SNS topic started with its display name as demo-original, matching the template.

  • I then changed it from the AWS CLI with aws sns set-topic-attributes … DisplayName=changed-by-me — a change made completely outside CloudFormation.

  • Within moments the agent detected the drift, classified it as cosmetic (low risk), generated the fix, and ran UpdateStack on its own. I got an "AUTO-APPLIED" email — no approval needed.

  • The stack's template updated automatically to the new display name, back in sync with reality.


Demo 2 — A risky change from the Console → email approval

  • 1. The S3 bucket started with versioning Suspended, matching the template.

  • I enabled versioning from the AWS Console — a higher-risk change.

  • The agent detected it, classified it high risk, and emailed me an "AWAITING APPROVAL" message with a clean summary, a before/after diff (Status: Suspended → Enabled), and Approve / Reject links.

  • Clicking Approve opened a confirmation page, ran UpdateStack, and synced the template to Enabled. (A link with a tampered token is correctly rejected — only the real emailed link works.)

  • The template now shows Status: Enabled, matching the live bucket — this time with a human in the loop.


How I Built It

I built it as a small, serverless, event-driven system on AWS, one focused stage at a time. Each drift event flows through a pipeline: provenance check → AI analysis → template generation → risk-based routing → notification.
Key decisions and challenges I hit and solved:
  • Making it truly autonomous. Early on, drift was recorded but nothing acted on it. I wired detection to automatically trigger the processing pipeline so it runs end-to-end with no human.
  • Catching Console changes. My first Console test did nothing. The cause: the S3 Console calls AWS from the browser, so CloudTrail logged a browser user-agent, and my origin classifier only recognized aws-cli/console.amazonaws.com. I fixed it to treat browser user-agents as Console origin — now Console and CLI changes are both caught.
  • Accurate fixes for any service. Guessing property names from CloudTrail was unreliable. I integrated CloudFormation's own native drift detection (DetectStackResourceDrift), which returns the live properties in CloudFormation's exact schema — so the generated template is correct for a wide range of resource types without hand-coding each one.
  • Preserving template references. The generator was flattening short-form intrinsics like !Ref and !GetAtt into plain strings. I fixed the parser to keep them as valid long-form (Ref: / Fn::GetAtt:), so references never break on UpdateStack.
  • One-click approvals. I added secure Approve/Reject links to the email, guarded by a per-change secret token, backed by a Lambda Function URL.
  • Real-world limits. DynamoDB's 400 KB item limit (solved by offloading large templates to S3), duplicate suppression by change fingerprint, and clean, readable email summaries (stripping CloudTrail noise) all needed care.

AWS Services Used

  • AWS Lambda — the agent's compute: detector, scheduled sweep, processor, and approval-callback functions.
  • Amazon EventBridge — an event rule forwards CloudTrail changes to the detector, and EventBridge Scheduler runs the 6-hourly reconciliation sweep.
  • AWS CloudTrail — records the Console/CLI changes that signal drift.
  • Amazon Bedrock (Nova) — the AI that explains each change and classifies its risk.
  • Amazon DynamoDB — single-table store for drift events and audit history.
  • Amazon S3 — stores proposed/applied templates and offloaded large payloads.
  • Amazon SES — sends notifications and the one-click approval emails.
  • AWS IAM — least-privilege roles plus a dedicated CloudFormation service role for approved updates.
  • AWS SAM / CloudFormation — the whole agent is defined and deployed as infrastructure-as-code.

Architecture flow:



What I Learned

  • Autonomy is just good wiring. The agent only ran on its own once each step reliably handed off to the next — no human in the middle.
  • Test the whole path, not just the code. Small things broke the flow (a trail must exist; Console changes look different from CLI). I learned to check CloudTrail → EventBridge → Lambda end-to-end.
  • Use what AWS already gives you. CloudFormation's built-in drift detection gave me accurate fixes across many services — no need to hand-code each one.
  • Safety comes first. Since the agent can change live infrastructure, the human approval step (with a secure one-click link) is the most important part.
  • AI is only one step. Bedrock explains the change and rates the risk, but the real work is the pipeline around it.
  • Serverless keeps it simple and cheap. Small Lambda functions, EventBridge Scheduler, a Lambda approval link, and one SAM template to deploy it all.

Link to Repo

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