Protecting Resources and Bootstrapping Instances with CloudFormation
This lesson explains the CloudFormation mechanisms that prevent accidental data loss and configure instances reliably during stack creation.
Learning outcomes
After completing this lesson, you will be able to:
- Protect resources with deletion policies, stack policies and termination protection.
- Bootstrap instances with helper scripts and creation policies.
- Diagnose a stack that times out waiting for signals.
Protecting stacks and resources
DeletionPolicy on a resource controls what happens when it is removed from the template or the stack is deleted: Delete (the default for most resources), Retain (keep the resource, unmanaged) or Snapshot (for EBS, RDS, ElastiCache, Redshift and others). UpdateReplacePolicy does the same for the old resource when an update replaces it. Termination protection prevents a whole stack being deleted. A stack policy is a JSON document that denies update actions such as Update:Replace or Update:Delete on chosen logical resources during stack updates; it can be overridden temporarily for a single update. Rollback triggers monitor CloudWatch alarms during and after an update and roll the stack back if one fires.
| Mechanism | Protects against |
|---|---|
| DeletionPolicy: Retain or Snapshot | Data loss when a resource or stack is deleted |
| UpdateReplacePolicy | Data loss when an update replaces a resource |
| Termination protection | Deleting the whole stack |
| Stack policy | Updates that replace or delete critical resources |
| Rollback triggers | Updates that degrade the application (alarm fires) |
Protection features
Bootstrapping EC2 with helper scripts
Instances can be configured with user data, but CloudFormation offers more structured tools. The AWS::CloudFormation::Init metadata on an instance or launch template declares packages, files, commands, users and services; the cfn-init helper script, run from user data, applies it. cfn-hup watches for metadata changes and reapplies them on update. cfn-signal reports success or failure back to CloudFormation. A CreationPolicy on the instance or Auto Scaling group makes CloudFormation wait for a number of success signals within a timeout before marking the resource CREATE_COMPLETE; if signals do not arrive, the resource fails and the stack rolls back. An UpdatePolicy on an Auto Scaling group controls rolling or replacing updates when the launch template changes.
UserData:
Fn::Base64: !Sub |
#!/bin/bash
/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource WebLaunchTemplate --region ${AWS::Region}
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource WebASG --region ${AWS::Region}
# on the Auto Scaling group
CreationPolicy:
ResourceSignal: {Count: 2, Timeout: PT15M}The following diagram shows the helper scripts in order.

Helper scripts
A stack that times out
A stack creating an Auto Scaling group with a CreationPolicy fails after 15 minutes with Received 0 SUCCESS signal(s) out of the 2 required. The instances are running. The user data log (/var/log/cloud-init-output.log) shows that cfn-signal could not reach the CloudFormation endpoint, because the instances are in private subnets whose NAT gateway route was created by the same stack after the group. Adding DependsOn on the route to the Auto Scaling group, or a CloudFormation interface endpoint, lets the signal through, and the stack completes.
Which attribute keeps a database when its stack is deleted?
Show answer
DeletionPolicy set to Retain or Snapshot.
Exam Tips
Remember for the exam
- DeletionPolicy Retain or Snapshot protects data on deletion; UpdateReplacePolicy on replacement.
- Stack policies block updates to critical resources; termination protection blocks stack deletion.
- cfn-init applies CloudFormation::Init; cfn-signal with a CreationPolicy confirms success.
- No signals received: check network access to CloudFormation and cfn-init errors.
- Rollback triggers roll back updates when alarms fire.