Question

if I have a AWS CloudFormation template using UserData block containing script block to be executed, for example:

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

After the instance is created,

  1. I assume that this script block will be saved somewhere to be execute?
  2. If so, where can I find this script on the EC2 instance?
  3. Will AWS remove this temporary script after stack is created successfully?

I could not find they mention in the doc.

Thanks

Was it helpful?

Solution

The user-data for an instance is available for any process on the instance to retrieve at this location:

http://169.254.169.254/latest/user-data

The DNS name "instance-data" resolves to that IP address, so if you trust DNS to be up, you can also use the easier to remember:

http://instance-data/latest/user-data

Here are the Amazon docs:

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AESDG-chapter-instancedata.html

OTHER TIPS

in /var/lib/cloud/data/scripts/ folder

  1. I assume that this script block will be saved somewhere to be execute?

Yes, Cloudfomration Stores the userdata whatever you sent inside "UserData" block.

  1. If so, where can I find this script on the EC2 instance?

You can find Userdata @ /var/lib/cloud/instance/userdata.txt and Userdata logs @ /var/log/cloud-init-output.log

  1. Will AWS remove this temporary script after stack is created successfully? No, AWS won't remove the userdata script after stack is created. You can use it for debugging purpose.

FYI : If you use custom AMI, you can see the original instance userdata and current userdata's also in /var/lib/cloud/instances/*. Looks like AWS never removing the userdata histories.

Hope this helps for basic understanding.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top