Certopolis

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::Init applies itself: the instance must run cfn-init, usually from user data.
  • Believing cfn-signal installs or configures anything: it only reports success or failure to CloudFormation.
  • Believing cfn-hup performs the first configuration: it reacts to later metadata changes through its hooks.
  • Believing cfn-signal finds the stack's Region by itself: --region defaults to us-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.

Written from these sources

Try 3 questions on this

Pick an answer to see why each option is right or wrong.

  1. A DevOps engineer writes an AWS CloudFormation template for an Amazon EC2 instance. The instance resource's Metadata section holds an AWS::CloudFormation::Init block that lists packages to install, configuration files to write, and services to start. After the stack is created, none of those packages or files exist on the instance, because the user data script only updates the aws-cfn-bootstrap package. The instance must apply the metadata when it first boots. What should a DevOps engineer do to meet these requirements?

    Right. In the game that’s Renown for your Configuration Management and IaC building.

    Not that one: B is right. In the game your Configuration Management and IaC building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    It is normally called from user data with the stack name, the resource's logical ID and the Region, so adding that call makes the instance apply its metadata on first boot.

    Written from docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/cfn-init.html

  2. A company's AWS CloudFormation template creates an Auto Scaling group of five Amazon EC2 instances in the eu-west-1 Region. Each instance's user data installs the application, which takes about 25 minutes, and then runs a local health check. The stack must not report the group as complete until at least four instances have passed the health check. If that has not happened within 40 minutes, the stack must fail. Which combination of steps will meet these requirements? (Select THREE.)

    Right. In the game that’s Renown for your Configuration Management and IaC building.

    Not that one: A,C,D is right. In the game your Configuration Management and IaC building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    The signals come from cfn-signal in user data after the health check passes, and because the helper sends to us-east-1 unless told otherwise, each call must name the stack's own Region.

    Written from docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-attribute-creationpolicy.html

  3. A DevOps team inherited an AWS CloudFormation template for an Auto Scaling group of 40 Amazon EC2 instances. The group's UpdatePolicy attribute contains an AutoScalingRollingUpdate policy with MaxBatchSize set to 4 and an AutoScalingReplacingUpdate policy with WillReplace set to true. The account's EC2 quota cannot hold a second group of 40 instances, and the last launch template update failed while it launched new instances. Future launch template updates must replace instances four at a time inside the existing group. What should a DevOps engineer do to meet these requirements?

    Right. In the game that’s Renown for your Configuration Management and IaC building.

    Not that one: D is right. In the game your Configuration Management and IaC building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    Setting WillReplace to false leaves the rolling update in charge, and it replaces instances inside the existing group in batches of MaxBatchSize, four at a time.

    Written from docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-attribute-updatepolicy.html

Ask for something