AWS Certified DevOps Engineer – Professional
cfn-init vs cfn-signal vs cfn-hup and CreationPolicy (DOP-C02)
cfn-init reads the AWS::CloudFormation::Init metadata and installs packages, writes files and starts services. cfn-signal tells CloudFormation whether an instance is ready, and works with a CreationPolicy. cfn-hup is a daemon that detects metadata changes and runs actions you define. This split comes from the CloudFormation Template Reference.
What does each helper script do?
cfn-init fetches and parses the metadata under the AWS::CloudFormation::Init key of a resource, then installs packages, writes files to disk, and enables or disables and starts or stops services. It is typically run from an EC2 instance's or launch template's user data, with --stack, --resource and --region. Nothing in the metadata is applied unless cfn-init runs.
cfn-signal reports to CloudFormation whether instances were created or updated successfully. It is used with a CreationPolicy, or with an UpdatePolicy that sets WaitOnResourceSignals on an Auto Scaling group. With -e it takes a process exit code to decide success or failure. Its --region option defaults to us-east-1, so a stack in another Region must pass its own Region.
cfn-hup is a daemon that checks resource metadata for changes, every 15 minutes by default, and runs the actions in its hooks files. A hook has triggers (post.add, post.update or post.remove), a path to the metadata, and an action. A common action is to run cfn-init again, so that a stack update changes the configuration of running instances. cfn-hup.conf names the stack.
How does a CreationPolicy wait for instances?
A CreationPolicy keeps a resource from reaching create complete until CloudFormation receives the required number of success signals or the timeout passes. It applies only when the resource is created. It is supported on AWS::AutoScaling::AutoScalingGroup, AWS::EC2::Instance, AWS::CloudFormation::WaitCondition and AWS::AppStream::Fleet.
ResourceSignal has a Count, which defaults to 1, and a Timeout in ISO 8601 duration format such as PT40M, with a maximum of 12 hours. If a failure signal arrives or too few signals arrive before the timeout, the resource creation fails and the stack rolls back.
For an Auto Scaling group, AutoScalingCreationPolicy adds MinSuccessfulInstancesPercent, the share of instances that must signal success. It defaults to 100.
Which UpdatePolicy applies to an Auto Scaling group?
AutoScalingRollingUpdate replaces instances inside the existing group in batches. MaxBatchSize sets how many are replaced at once and defaults to 1. MinInstancesInService sets how many stay in service. With WaitOnResourceSignals set to true, PauseTime becomes the timeout for the signals from new instances.
AutoScalingReplacingUpdate with WillReplace set to true creates a whole new group and keeps the old one until the new one is complete, so CloudFormation can roll back to the old group. If both policies are on the group, WillReplace: true gives the replacing update precedence. AWS says to pair WillReplace with a matching CreationPolicy.
AutoScalingInstanceRefresh runs an Auto Scaling instance refresh instead. It cannot be combined with AutoScalingRollingUpdate on the same group.
Where the exam catches people
- Believing metadata in
AWS::CloudFormation::Initapplies itself: the instance must runcfn-init, usually from user data. - Believing
cfn-signalinstalls or configures anything: it only reports success or failure to CloudFormation. - Believing
cfn-hupperforms the first configuration: it reacts to later metadata changes through its hooks. - Believing
cfn-signalfinds the stack's Region by itself:--regiondefaults tous-east-1. - Believing a rolling update runs when both update policies are present with
WillReplace: true: the replacing update takes precedence and builds a second group.