We can use AWS::CloudFormation::Init to execute commands and upload files after starting an instance. But does anybody know what are the internals of this operation (from Amazon's side)?
When we pass a template in, at what point are the files or commands transmitted to the VM? Is this is a Xen feature (through special pipe), or via the network?
"Resources": {
"MyInstance": {
"Type": "AWS::EC2::Instance",
"Metadata" : {
"AWS::CloudFormation::Init" : {
"config" : {
"packages" : {
:
},
"sources" : {
:
},
"commands" : {
:
},
"files" : {
:
},
"services" : {
:
},
"users" : {
:
},
"groups" : {
:
}
}
}
},
"Properties": {
:
}
}
}
CloudFormation creates a bucket for each region in which you upload a template file. The buckets are accessible to anyone with Amazon Simple Storage Service (Amazon S3) permissions in your AWS account. If a bucket created by CloudFormation is already present, the template is added to that bucket.
cfn-init supports all metadata types for Linux systems. It supports metadata types for Windows with conditions that are described in the sections that follow. For an example of using AWS::CloudFormation::Init and the cfn-init helper script, see Deploying applications on Amazon EC2 with AWS CloudFormation.
cfn-init: Use to retrieve and interpret resource metadata, install packages, create files, and start services.
An associated AWS::CloudFormation::WaitCondition resource checks the URL for the required number of success signals or for a failure signal. Important.
Creating a AWS::CloudFormation::Init resource as metadata to an EC2 instance does not cause the instance to do anything by itself.
For the instance to actually perform all the operations specified in that resource, it must run the cfn-init
command line tool. On Amazon EC2 AMIs that command is already installed at /opt/aws/bin/cfn-init
. The command takes several options, including the name of the AWS::CloudFormation::Init resource, the name of the EC2 server resource, and the region you are running in. You also need to provide AWS security credentials.
If you'd like this to run automatically when you create a new instance (I sure did) you'll have to use the EC2 instance's UserData to create a shell script that the instance will run on first boot, and put the cfn-init
command in it.
I've written about this specific issue in my blog recently.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With