user6826691
user6826691

Reputation: 2011

How to update user data in cloud formation?

I am new to cloud formation ! How do we update the user data in cloud formation, does the update stack also update anything that is changed inside the UserData? If not how do we update the User Data changes ? Thanks !

"UserData": {
    "Fn::Base64": {
        "Fn::Join": [
            "",
            [
                "#!/bin/bash\n",
                "apt-get update\n",
                "apt-get -y upgrade\n",
            ]
        ]
    }
}

Upvotes: 3

Views: 7132

Answers (3)

Usman Mutawakil
Usman Mutawakil

Reputation: 5259

To update anything in your cloudformation template you run an update-stack command with the new or update cloudformation template as an input parameter. AWS will then only alter the components you have changed. So if any line in the userData section has been altered when you run update-stack AWS will run through the entire userData section once again.

THis also means your EC2 instances will be destroyed and replaced by new instances if your change requires updating userData.

Update: You can update your EC2 instances user data without destroying if you're EC2 instance is using an EBS drive and you turn it off first. But this is an anti-pattern in my opinion. The EC2 instances is just a VM. You should keep it immutable. If something needs to change rebuild the instance.

Upvotes: 2

vishal
vishal

Reputation: 1874

"Fn::Join": [
        "/n",
        [
            "#!/bin/bash",
            "apt-get update",
            "apt-get -y upgrade",
        ]

actually this is the correct way. Don't know whether it is just a typo from your side

Upvotes: 0

Abhinav
Abhinav

Reputation: 2876

Userdata can be updated, but it would require stopping and starting of your EBS backed instance :

http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html#cfn-ec2-instance-userdata

Upvotes: 1

Related Questions