Lesson 3 of 5 AWS CloudFormation

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.

MechanismProtects against
DeletionPolicy: Retain or SnapshotData loss when a resource or stack is deleted
UpdateReplacePolicyData loss when an update replaces a resource
Termination protectionDeleting the whole stack
Stack policyUpdates that replace or delete critical resources
Rollback triggersUpdates that degrade the application (alarm fires)
Match the pairs

Protection features

Exam skills 3.1.2

Match each protection need with the CloudFormation feature that provides it.

Log in to try this activity Interactive activities are free with an account.

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.

Bootstrapping with helper scripts. User data runs cfn-init, which applies the AWS::CloudFormation::Init metadata of packages, files, commands and services. cfn-signal then reports success or failure, and a CreationPolicy makes CloudFormation wait for the signals within a timeout. cfn-hup reapplies the metadata when it changes on update.
The timeout is illustrative. A stack that times out on create usually means cfn-signal never ran.
Match the pairs

Helper scripts

Exam skills 3.1.2 · 3.1.3

Match each helper script or attribute with its role.

Log in to try this activity Interactive activities are free with an account.
Worked example

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.

Check yourself

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.