sashoalm
sashoalm

Reputation: 79685

Does cfn-init need to be called from UserData?

It seems that AWS::CloudFormation::Init has a config section which has files and commands subsections. But there is also the Base64-encoded UserData in Properties which calls cfn-init in a sample script that I have:

"Fn::Base64": {
    "Fn::Join": [
        "",
        [
            "<script>\n",
            "cfn-init.exe -v -s ", {
                "Ref": "AWS::StackId"
            },
            " -r MyInstance",
            " --region ", {
                "Ref": "AWS::Region"
            },
            "\n",
..................

My question is - do you need to manually call cfn-init from UserData in order to execute the instructions in AWS::CloudFormation::Init? If so, why is that? Why not start it automatically so the config section always gets executed if present?

Upvotes: 1

Views: 1242

Answers (1)

Neil Bostrom
Neil Bostrom

Reputation: 2349

cfn-init.exe is the local component on the EC2 instances that communicates back to the CloudFormation stack to execute the commands inside the AWS::CloudFormation::Init section.

I don't know, but I believe the reason it doesn't put the actual script inside the User Data is that it doesn't want to modify the User Data in case you have your own scripts in there. Also, the commands to execute could be dynamic from the parameters passed into your CloudFormation script. The cfn-init.exe tool can read these values and make smart choices about how to run the AWS::CloudFormation::Init commands.

More information on what cfn-init.exe does here: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-init.html

Upvotes: 2

Related Questions