7.2 Managing Stacks, Drift Detection & Change Sets
Key Takeaways
- Change Sets provide a preview mechanism that evaluates template deltas before execution, exposing planned Actions (Add, Modify, Remove) and Replacement risks (True, False, Conditional).
- Resource replacement (Replacement: True) results in the termination and recreation of the target resource with a new physical ID, creating downtime or data loss risks if unmanaged.
- CloudFormation Drift Detection identifies divergence between stack templates and actual AWS resource configurations caused by out-of-band console or CLI modifications.
- CloudFormation cannot automatically reconcile drift; operators must either revert the out-of-band resource modifications to match the template or update the template to reflect the drifted state.
- Stack termination protection safeguards stacks against accidental deletion calls, while JSON Stack Policies prevent unauthorized modifications, replacements, or deletions of critical stateful resources during stack updates.
7.2 Managing Stacks, Drift Detection & Change Sets
CloudOps Blueprint Focus: Managing production infrastructure requires rigorous change governance and drift remediation. For the AWS Certified CloudOps Engineer – Associate (SOA-C03) exam, you must understand the operational hazards of direct stack updates, execute Change Sets to preview resource replacements, detect and resolve infrastructure drift caused by out-of-band changes, enforce Stack Termination Protection, and author JSON Stack Policies to lock down stateful resources.
Stack Lifecycle Operations: Direct Updates vs. Change Sets
CloudFormation manages collections of related resources through the stack lifecycle. Throughout this lifecycle, stacks transition through standardized status states (CREATE_IN_PROGRESS, CREATE_COMPLETE, UPDATE_IN_PROGRESS, UPDATE_COMPLETE, ROLLBACK_IN_PROGRESS, ROLLBACK_COMPLETE, DELETE_IN_PROGRESS, DELETE_COMPLETE).
When updating an existing stack with a modified template or parameter set, CloudOps engineers can choose between two operational approaches:
Direct Stack Updates
In a direct update (aws cloudformation update-stack), CloudFormation compares the submitted template directly against the currently deployed template and immediately begins executing modifications. While fast, direct updates carry severe operational risks in production environments:
- Unforeseen resource replacements can terminate active database instances or network interfaces.
- Property changes may cause service restarts or unexpected outages without warning.
- Operators cannot review the precise execution sequence before the update begins.
Change Sets Deployment Pattern
To eliminate operational risk, enterprise governance mandates the use of Change Sets (aws cloudformation create-change-set). Change Sets decouple change evaluation from execution:
- CloudFormation calculates the delta between the active stack state and the proposed template.
- CloudFormation generates a detailed, queryable preview report without modifying live resources.
- The CloudOps engineer inspects the planned changes, assesses downtime impact, and either executes (
execute-change-set) or discards (delete-change-set) the Change Set.
Change Sets Workflow & Replacement Impact Analysis
When evaluating a Change Set via the AWS Management Console or describe-change-set CLI command, operators must analyze four key dimensions for every affected resource:
| Change Set Attribute | Values | Operational Meaning & Impact |
|---|---|---|
| Action | Add, Modify, Remove | Indicates whether the resource is newly declared, altered in place, or deleted from the stack. |
| Replacement | True, False, Conditional | Indicates whether the modification requires terminating and recreating the resource with a new physical ID. |
| Scope | Properties, Metadata, CreationPolicy, UpdatePolicy | Identifies which resource attributes or properties triggered the change. |
| Evaluation | Static, Dynamic | Static indicates the change is deterministic; Dynamic indicates the change depends on runtime function resolution. |
The Operational Hazards of Replacement: True
When Replacement is marked as True, CloudFormation cannot update the resource in place because an immutable property was modified (such as changing an EC2 instance's AMI, modifying an RDS DB Subnet Group, or renaming a DynamoDB partition key). CloudFormation executes a replacement workflow:
- CloudFormation provisions a brand-new resource alongside the existing resource.
- CloudFormation updates dependent resources to reference the new physical ID.
- CloudFormation terminates and deletes the original resource.
[!WARNING]
Replacement: Trueresults in a new physical ID, new private/public IP addresses, and new DNS hostnames. For compute instances, all ephemeral storage is lost. For unversioned storage or databases lacking retained snapshots, replacement can trigger catastrophic data loss and extended application downtime.
When Replacement is marked as Conditional, replacement depends on property values evaluated at execution time (for example, whether an RDS engine version update allows in-place upgrade or requires a complete instance replacement).
CloudFormation Drift Detection Architecture & Operational Realities
In modern cloud operations, team members occasionally bypass IaC pipelines and execute out-of-band modifications directly via the AWS Management Console, AWS CLI, or direct SDK calls (e.g., manually opening a security group port during an incident or resizing an EBS volume).
CloudFormation Drift Detection identifies these discrepancies by comparing the live runtime configuration of supported AWS resources against the expected state recorded in the CloudFormation template.
Drift Detection Statuses
Drift detection reports status at both stack and resource levels:
- Stack Drift Status:
IN_SYNC(all resources match),DRIFTED(one or more resources have modified or deleted properties), orNOT_CHECKED. - Resource Drift Status:
IN_SYNC,MODIFIED(specific properties differ from template), orDELETED(the resource was terminated or deleted out-of-band).
Critical Operational Rule: CloudFormation Does Not Auto-Reconcile Drift
CloudFormation Drift Detection is purely diagnostic. CloudFormation cannot automatically reverse out-of-band modifications or roll back live resources to match the template without intervention. There is no automated "synchronize" or "revert drift" button.
To resolve drift, CloudOps engineers must choose one of two remediation paths:
- Revert the Live Resource: Manually reconfigure the live AWS resource (via AWS Console or CLI) to match the template's specified properties, then re-run drift detection to confirm the status is
IN_SYNC. - Update the Template: Modify the CloudFormation template to reflect the drifted out-of-band changes, then execute a standard stack update (via Change Set) so the template and live resource align perfectly.
Safeguarding Infrastructure: Termination Protection & Stack Policies
Production operations require multiple defensive layers to prevent accidental or malicious infrastructure destruction:
Stack Termination Protection
Stack Termination Protection is a boolean attribute configured on a stack (aws cloudformation update-termination-protection --enable-termination-protection). When enabled, CloudFormation rejects all stack deletion calls made via the AWS Management Console, CLI, or API. An administrator must explicitly disable termination protection before the stack can be deleted.
[!IMPORTANT] Termination protection only prevents stack deletion. It does not prevent stack updates, nor does it prevent resource replacement or resource deletion occurring as part of a stack update.
Stack Policies: Enforcing Granular Update Governance
A Stack Policy is a JSON policy document attached directly to a CloudFormation stack that governs which update actions can be executed on specific resources during stack updates.
By default, all update actions are permitted on all resources in a stack. When a Stack Policy is applied, CloudFormation shifts to an explicit default-deny security model: all update actions are denied unless explicitly allowed.
To protect mission-critical stateful infrastructure, engineers attach a stack policy that allows general updates while explicitly denying replacement or deletion on critical databases:
{
"Statement": [
{
"Effect": "Allow",
"Action": "Update:*",
"Principal": "*",
"Resource": "*"
},
{
"Effect": "Deny",
"Action": [
"Update:Replace",
"Update:Delete"
],
"Principal": "*",
"Resource": "LogicalResourceId/ProductionDatabase"
}
]
}
If an update would cause ProductionDatabase to be replaced or deleted, CloudFormation halts the update immediately and rolls back. When an intentional database replacement is required during planned maintenance, an authorized operator can temporarily bypass the restriction by passing an override policy during the update call (--stack-policy-during-update-body).
A CloudOps engineer needs to update an existing CloudFormation stack that hosts a production Amazon DynamoDB table and an Amazon EC2 Auto Scaling group. The engineer modifies the template to adjust table provisioned capacity and EC2 Launch Template specifications. Before applying the changes, the engineer must verify that the update will not cause the DynamoDB table to be replaced. Which operational procedure provides this verification?
An operations engineer runs CloudFormation Drift Detection on an infrastructure stack and discovers that an Amazon EC2 security group has a drift status of DRIFTED because a developer manually opened port 22 via the AWS Management Console. The CloudOps team wants the live security group to match the original CloudFormation template specification. How can the team resolve this drift?
An enterprise deploys its production multi-tier application using CloudFormation. The stack includes an Amazon Aurora database cluster, an Application Load Balancer, and an Auto Scaling group. The security team mandates that accidental stack updates must never delete or replace the Aurora database cluster under any circumstances, while allowing updates to application compute resources. Which mechanism enforces this control?