- Declaring AWS::CloudFormation::Init metadata in your template, and
- Invoking cfn-init from the instance UserData during boot.
Make sure the EC2 instance has an IAM instance profile that gives it permission to read stack metadata (this is typically an Instance Profile containing an IAM role you created earlier).
How cfn-init works (high level)
- The template embeds configuration instructions under the resource’s
Metadata→AWS::CloudFormation::Initsection. - On instance boot, a UserData script calls
/opt/aws/bin/cfn-init(or equivalent) to fetch the metadata from the stack and apply the configuration (install packages, write files, and start/enable services). - cfn-init uses the instance’s IAM role (Instance Profile) to retrieve stack metadata, so the instance must have the appropriate permissions.
1) cfn-init metadata (packages and services)
TheAWS::CloudFormation::Init metadata describes how to configure the instance. The fragment below installs the httpd package using yum and ensures the httpd service is enabled and running using sysvinit:
packagesinstructs cfn-init to install OS packages (here usingyumfor Amazon Linux).servicestells cfn-init which service manager to interact with. Usesysvinitfor older AMIs orsystemdfor Amazon Linux 2 and most modern distributions.
2) Properties: instance type, AMI, security group, and instance profile
The EC2 Instance resource needs standard properties:InstanceType, ImageId, SecurityGroupIds, and IamInstanceProfile. Many templates use a region-based mapping to choose the ImageId.
Example parameters and mapping:
3) Security group
Create a security group that allows SSH (22) and HTTP (80) access and attach it to the instance viaSecurityGroupIds:
4) UserData: invoke cfn-init
UserData should invoke cfn-init on instance boot to apply theAWS::CloudFormation::Init configuration. Encode the script with Fn::Base64 and use Fn::Sub to allow CloudFormation pseudo-parameters to be expanded:
- The typical path for
cfn-initon Amazon Linux is/opt/aws/bin/cfn-init. Ensure the AMI includes theaws-cfn-bootstrappackage or otherwise providescfn-init. - Use the
-vflag for more verbose output when troubleshooting. - Wrap the script with
Fn::Subwhen embedding pseudo-parameters such as${AWS::StackName}so substitutions happen before the Base64 encoding.
Confirm the AMI you use contains
cfn-init (often provided by the aws-cfn-bootstrap package). Also choose the correct service manager key in metadata (sysvinit vs systemd) to match your AMI. Without the correct AMI and permissions, cfn-init will not be able to apply the configuration.Full minimal template (combines the pieces)
This example ties together Parameters, Mappings, the security group, and the EC2 instance withAWS::CloudFormation::Init metadata and UserData that runs cfn-init.
Template components at a glance
| Template section | Purpose | Example/key items |
|---|---|---|
| Parameters | Inputs to the template | MyVPC, MyInstanceType, MyCFNInstanceProfile |
| Mappings | Region-specific AMI selection | RegionMap → AMI |
| Resources — Security Group | Network access rules | MySecurityGroup (SSH, HTTP) |
| Resources — EC2 Instance | Instance configuration & metadata | MyInstance with Metadata → AWS::CloudFormation::Init |
| UserData | Boot-time invocation of cfn-init | Fn::Base64 + Fn::Sub script calling /opt/aws/bin/cfn-init |
Final checklist
- Verify the
MyCFNInstanceProfileInstance Profile exists and grants the instance permission to read CloudFormation stack metadata. - Confirm the chosen AMI includes
cfn-init(or installaws-cfn-bootstrap) and uses the expected service manager (sysvinitvssystemd). - Use
-vwith cfn-init for verbose logs while debugging. - Ensure your security group allows the inbound traffic necessary for testing (SSH/HTTP in this example).
cfn-init at boot. cfn-init will read the embedded AWS::CloudFormation::Init instructions to install httpd and ensure the service is enabled and running.
Links and references
- AWS CloudFormation User Guide — AWS::CloudFormation::Init
- cfn-init and aws-cfn-bootstrap (GitHub / docs)
- Amazon Linux AMI information
- CloudFormation documentation