Skip to main content
Welcome — this lesson explains CloudFormation init (commonly called cfn-init) and how to use it to bootstrap and manage EC2 instances from CloudFormation templates. cfn-init is a helper script that runs on an EC2 instance during stack creation or updates. It reads the AWS::CloudFormation::Init metadata embedded in your CloudFormation template and performs instance-level configuration tasks such as installing packages, creating files, extracting application sources (from S3 or Git), executing commands, and starting or enabling services. You can optionally pair it with cfn-signal to notify CloudFormation about initialization status and with cfn-hup to detect and apply metadata changes automatically.
A presentation slide titled "CloudFormation Init – Overview" with a cfn-init icon on the left. On the right are two checklist points: "Lets you download files from a remote source" and "Can use cfn-hup to detect metadata changes and to apply those updates automatically."
Typical flow when using cfn-init in a stack:
Ensure the EC2 instance has the CloudFormation helper scripts installed (aws-cfn-bootstrap), and that its instance profile allows access to any remote resources you reference (for example, S3) as well as CloudFormation APIs if you use cfn-hup.
Key AWS::CloudFormation::Init sections Example AWS::CloudFormation::Init metadata (YAML)
Example UserData snippet that invokes cfn-init and then signals CloudFormation
Notes on cfn-hup
  • cfn-hup is a daemon that polls CloudFormation for metadata changes. When it detects changes, it can invoke configured hooks to re-run cfn-init or other commands to apply updates.
  • To use cfn-hup you must:
    • Configure its .conf and .hooks files (these are often created by cfn-init).
    • Ensure the instance role has permission to call CloudFormation APIs.
  • cfn-hup is optional but useful when you want instances to pick up metadata changes without replacing or manually updating instances.
Summary
  • cfn-init automates instance bootstrapping using AWS::CloudFormation::Init metadata in your CloudFormation template.
  • Pair cfn-init with cfn-signal for lifecycle signaling and with cfn-hup for dynamic metadata updates.
  • Verify helper scripts (aws-cfn-bootstrap) are installed on your AMI and that IAM permissions for S3 and CloudFormation are in place.
Links and References

Watch Video