Introducing cfn-init
CloudFormation is a declarative instrument. This means that everything in your template is a declaration of what should be there in CloudFormation’s state. And since you use resource attributes to declare your infrastructure, you must store your configuration in it as well, whether it’s a source code in a Lambda function or UserData
in an EC2 launch template, launch configuration, or instance.
Using UserData
is a very simple process, but it is hard to declare a configuration in it. In the end, you will do a lot of shell programming, which is not as reliable as configuration management should be.
Luckily, CloudFormation has a set of so-called helper scripts—instruments that can get the data from CloudFormation’s metadata or a stack, process it, and perform operations on your resources. One of them is called cfn-init
.
Important note
The cfn-init
instrument (and any other helper script) is only included in Amazon Linux AMIs...