Sub topic 3
Reading Failures and Rollback States
Assume you can read YAML, use the AWS CLI, and understand basic AWS resources. CloudFormation events are the main evidence when an operation fails.
Reading Failures and Rollback States
Run:
aws cloudformation describe-stack-events --stack-name demoRead the newest events first. Find the first resource with CREATE_FAILED, UPDATE_FAILED, or DELETE_FAILED. Its status reason usually identifies the cause, such as a missing permission, invalid property, or resource name conflict.
| Status | Meaning |
|---|---|
| `UPDATE_ROLLBACK_COMPLETE` | The update failed, but CloudFormation restored the previous state. |
During rollback, do not edit resources manually unless necessary. Fix the cause, then retry the update. If rollback itself fails, resolve the reported problem and continue rollback before making another update.
Protecting Data
DeletionPolicy controls what happens when a resource is removed from a template or its stack is deleted. UpdateReplacePolicy controls the old resource when a property change requires replacement.
Resources:
DataBucket:
Type: AWS::S3::Bucket
DeletionPolicy: Retain
UpdateReplacePolicy: RetainRetain protects data but can leave resources and charges behind. Use Snapshot where the resource type supports snapshots.
Drift and Shared Values
Drift occurs when someone changes a managed resource outside CloudFormation. Start detection with detect-stack-drift, then inspect the result with describe-stack-resource-drifts. Reconcile intentional changes in the template; otherwise restore the template’s desired state.
Outputs can export stable values, and another stack can read them with Fn::ImportValue. This creates a dependency: the exporting stack cannot be deleted while another stack imports its value.
Composing and Governing Stacks
A nested stack uses AWS::CloudFormation::Stack and a child template stored in Amazon S3. It divides a large deployment into reusable units, while the parent coordinates parameters and dependencies.
Stack policies can deny updates to critical resources. Termination protection blocks accidental stack deletion, but it does not prevent resource updates or direct deletion outside CloudFormation.
Existing resources can be brought under management through resource import. The template must describe the resource, and an import operation requires its identifiers and a successful validation.
Transforms process a template before deployment. AWS::Serverless expands serverless resources into ordinary CloudFormation resources. Macros provide custom transformations and should be versioned and reviewed.
Custom resources call a Lambda function or another service during stack operations. They are useful for unsupported APIs, but failures, timeouts, and non-idempotent behavior can block a stack.
StackSets deploy one template across multiple accounts and Regions. Use them for organization-wide baselines, with suitable permissions and cautious rollout settings.
Durable Design
Keep stacks focused, name exports deliberately, protect stateful resources, use parameters sparingly, review change sets, and test rollback paths. Prefer nested stacks or separate stacks when teams or lifecycles differ.
Exercise
Why might an update leave an old resource after replacement?
Show answer
UpdateReplacePolicy: Retain preserves the old resource when CloudFormation replaces it.