AWS Builder Center

Never Deploy Blind: A ChangeSet Preview and Approval Gate for LLM-Generated CloudFormation

A template that passes validation can still delete or replace live resources. Here is how I preview every LLM-generated CloudFormation template as a ChangeSet, flag risky changes, and require approval before anything executes.

My AI-powered AWS Deployment Assistant already validates, tests, and cost-checks every template an LLM generates. But all of those checks look at the template itself. None of them answer the question a user really cares about right before deploying: what will this do to my account?
A template can be valid, secure, and cheap, and still delete a database or replace a running instance. So the last layer in my pipeline is a preview and an approval gate.

The idea

CloudFormation already has a feature for this: ChangeSets. Instead of deploying a template, you ask CloudFormation what it would do, and it returns a list of changes. The assistant never deploys directly. It always goes through four steps:
  1. Create a ChangeSet.
  1. Summarize it in plain English.
  1. Flag risky changes and require explicit approval for them.
  1. Execute the ChangeSet only if approved. Otherwise delete it.

Step 1: create the ChangeSet

A new stack needs  ChangeSetType="CREATE" . An existing stack needs  "UPDATE" .
import uuid
import boto3
from botocore.exceptions import ClientError, WaiterError
cfn = boto3.client("cloudformation")
def stack_exists(name: str) -> bool:
try:
cfn.describe_stacks(StackName=name)
return True
except ClientError as e:
if "does not exist" in str(e):
return False
raise
def create_preview(stack: str, template_body: str) -> str:
cs_name = f"preview-{uuid.uuid4().hex[:8]}"
cfn.create_change_set(
StackName=stack,
TemplateBody=template_body,
ChangeSetName=cs_name,
ChangeSetType="UPDATE" if stack_exists(stack) else "CREATE",
)
try:
cfn.get_waiter("change_set_create_complete").wait(
StackName=stack, ChangeSetName=cs_name
)
except WaiterError:
pass # a FAILED status is inspected in the next step
return cs_name

Step 2: read the changes

 describe_change_set  returns each change with an action ( Add ,  Modify ,  Remove ) and, for modifications, whether the resource will be replaced. Replacement means CloudFormation creates a new resource and deletes the old one, which is the dangerous case for anything stateful.
def get_changes(stack: str, cs_name: str):
changes, token = [], None
while True:
kwargs = {"StackName": stack, "ChangeSetName": cs_name}
if token:
kwargs["NextToken"] = token
resp = cfn.describe_change_set(**kwargs)
if resp["Status"] == "FAILED":
return resp.get("StatusReason", "unknown reason"), []
changes += [c["ResourceChange"] for c in resp.get("Changes", [])]
token = resp.get("NextToken")
if not token:
return None, changes
If the ChangeSet fails because the template contains no changes, the status reason says so. I treat that as "nothing to deploy", not as an error.

Step 3: classify and summarize

def classify(change: dict) -> str:
action = change["Action"]
replacement = change.get("Replacement", "False")
if action == "Remove":
return "DANGEROUS"
if action == "Modify" and replacement in ("True", "Conditional"):
return "DANGEROUS"
return "SAFE"
def summarize(changes: list) -> str:
lines = []
for c in changes:
flag = "!!" if classify(c) == "DANGEROUS" else "ok"
extra = ""
if c["Action"] == "Modify":
extra = f" (replacement: {c.get('Replacement', 'False')})"
lines.append(
f"[{flag}] {c['Action']:7} {c['LogicalResourceId']} "
f"({c['ResourceType']}){extra}"
)
return "\n".join(lines)
This is a sample of what the user sees (illustrative):
[ok] Add AppBucket (AWS::S3::Bucket)
[ok] Modify AppFunction (AWS::Lambda::Function) (replacement: False)
[!!] Modify AppDatabase (AWS::RDS::DBInstance) (replacement: True)
[!!] Remove OldQueue (AWS::SQS::Queue)
Two lines are marked as dangerous: a database that will be replaced and a queue that will be deleted. A user who only skims the template would likely miss both.

Step 4: the approval gate

Safe-only changes can proceed after a normal confirmation. Anything dangerous needs an explicit, separate confirmation.
def deploy_with_gate(stack: str, template_body: str, ask) -> str:
cs_name = create_preview(stack, template_body)
reason, changes = get_changes(stack, cs_name)
if reason:
cleanup(stack, cs_name)
return f"No deployment: {reason}"
print(summarize(changes))
dangerous = [c for c in changes if classify(c) == "DANGEROUS"]
if dangerous:
approved = ask(
f"{len(dangerous)} change(s) will delete or replace resources. "
"Type the stack name to confirm: ",
expect=stack,
)
else:
approved = ask("Deploy these changes? (yes/no): ", expect="yes")
if not approved:
cleanup(stack, cs_name)
return "Cancelled. Nothing was deployed."
cfn.execute_change_set(StackName=stack, ChangeSetName=cs_name)
return "Deployment started."
For dangerous changes I require the user to type the stack name instead of just "yes". It's a small piece of friction, but it stops reflex approvals.

Cleaning up rejected previews

Deleting the ChangeSet is not always enough. A brand-new stack that was previewed with  CREATE  sits in  REVIEW_IN_PROGRESS  until it is deployed or deleted, so rejecting it should remove the stack too.
def cleanup(stack: str, cs_name: str):
try:
cfn.delete_change_set(StackName=stack, ChangeSetName=cs_name)
except ClientError:
pass
status = cfn.describe_stacks(StackName=stack)["Stacks"][0]["StackStatus"]
if status == "REVIEW_IN_PROGRESS":
cfn.delete_stack(StackName=stack)

IAM capabilities

If the template creates IAM resources, CloudFormation requires  Capabilities=["CAPABILITY_IAM"]  (or  CAPABILITY_NAMED_IAM ). I don't pass these automatically. If the preview fails for a missing capability, the assistant tells the user that the template creates IAM resources and asks them to opt in. An LLM-generated template should never get IAM permissions silently.

Design decisions

  • Never deploy directly. Every deployment goes through a ChangeSet, even for brand-new stacks.
  • Treat replacement as dangerous. A "Modify" that replaces a resource can behave like a delete plus a create.
  • Make dangerous approvals different. Typing the stack name is harder to do on autopilot than clicking yes.
  • Preview what will change, not what the template says. The ChangeSet is computed against the real stack, so it catches things a static check cannot.

Limitations

  •  Replacement: Conditional  means CloudFormation can't know in advance. I treat it as dangerous to be safe, which means some false alarms.
  • A ChangeSet shows what CloudFormation will do to resources, not what the resources will do at runtime.
  • Some changes can still fail during execution even though the ChangeSet was created successfully.
  • It's one layer. Validation, security checks, and cost checks still run before this step.

Conclusion

Validation answers "is this template well-formed?" A ChangeSet answers "what will happen if I deploy it?" For LLM-generated infrastructure, I want a human to see that second answer, in plain English, before anything runs. The gate is small, but it turns a silent deletion into a visible decision.
*This is part of my series on building a safe AI-powered AWS Deployment Assistant. See my earlier posts on guardrails, testing, cost checks,
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