2
votes

I was looking into the difference between using cfn-init and userdata. I understand from various documentations that with cfn-init, making changes to the stack does not replace the ec2 instance and just updates the configuration changes to the existing ec2 instance.

Can someone please clarify what type of configuration changes is the documentation referring to here?

I am using AWS::CloudFormation::Init in my cloud formation template to install softwares and create new files and services. The stack runs successfully without any issues and all softwares and services are getting installed.

When I tried creating a new file in the AWS::CloudFormation::Init section, and update the stack, the new file does not get created.

Below is the new content added to the CFT template.

        "/tmp/hello.html":
          content:
            Fn::Join:
            - ''
            - - "#!/bin/bash \n"
              - "Checking if this file is created \n"
          mode: '000755'
          owner: root
          group: root

I expect the new file to be created but I don't see it created. Hence, what kind of changes does cfn-init can detect and apply?

1

1 Answers

1
votes

cfn-init effectively changes the User Data field on an Amazon EC2 instance.

A script supplied via User Data is only executed the first time that an instance is booted. (Officially, "once per instance ID").

Therefore, changing the User Data in the CloudFormation template will not impact any running instance. To have it run, a new instance would need to be created.

The AWS::EC2::Instance - AWS CloudFormation documentation says that User Data can be updated with "some interruptions". However, this would not trigger execution of the new script.

You'll need to experiment to find out how to force creation of a new instance when you change the cfn-init configuration. It might create a new instance. If it doesn't, you'll need to figure out a way to force it to happen, such as changing another property that does cause the instance to be replaced.