How to check whether my user data passing to EC2 instance is working

Amazon Web-ServicesAmazon Ec2User Data

Amazon Web-Services Problem Overview


While creating a new AWS EC2 instance using the EC2 command line API, I passed some user data to the new instance.

How can I know whether that user data executed or not?

Amazon Web-Services Solutions


Solution 1 - Amazon Web-Services

You can verify using the following steps:

  1. SSH on launch EC2 instance.
  2. Check the log of your user data script in:
    • /var/log/cloud-init.log and
    • /var/log/cloud-init-output.log

You can see all logs of your user data script, and it will also create the /etc/cloud folder.

Solution 2 - Amazon Web-Services

Just for reference, you can check if the user data executed by taking a look at the system log from the EC2 console. Right click on your instance -

In the new interface: Monitor and Troubleshoot > Get System Log

enter image description here

In the old interface: Instance Settings > Get System log

enter image description here

This should open a modal window with the system logs

enter image description here

Solution 3 - Amazon Web-Services

It might also be useful for you to see what the userdata looks like when it's being executed during the bootstrapping of the instance. This is especially true if you are passing in environmental variables or flags from the CloudFormation template. You can see how the UserData is being executed in two different ways:


1. From within the instance:

# Get instance ID
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)

# Print user data 
sudo cat /var/lib/cloud/instances/$INSTANCE_ID/user-data.txt

2. From outside the instance

Note: this will only work if you have configured the UserData shell in such a way that it will output the commands it runs. For bash, you can do this like as follows:

"#!/bin/bash\n",
"set -x\n",

Right click on the EC2 instance from the EC2 console -> Monitor and Troubleshoot -> Get system log. Download the log file and look for something a section that looks like this:

ip-172-31-76-56 login: 2021/10/25 17:13:47Z: Amazon SSM Agent v3.0.529.0 is running
2021/10/25 17:13:47Z: OsProductName: Ubuntu
2021/10/25 17:13:47Z: OsVersion: 20.04
[   45.636562] cloud-init[856]: Cloud-init v. 21.2-3...
[   47.749983] cloud-init[896]: + echo hello world

this is what you would see if the UserData was configured like this:

"#!/bin/bash\n",
"set -x\n",
"echo hello world"

Solution 4 - Amazon Web-Services

Debugging user data scripts on Amazon EC2 is a bit awkward indeed, as there is usually no way to actively hook into the process, so one ideally would like to gain Real time access to user-data script output as summarized in Eric Hammond's article Logging user-data Script Output on EC2 Instances:

> The recent Ubuntu AMIs still send user-data script to the console > output, so you can view it remotely, but it is no longer available in > syslog on the instance. The console output is only updated a few > minutes after the instance boots, reboots, or terminates, which forces > you to wait to see the output of the user-data script as well as not > capturing output that might come out after the snapshot.

Depending on your setup you might want to ship the logs to a remote logging facility like Loggly right away, but getting this installed early enough can obviously be kind of a chicken/egg problem (though it works great if the AMI happens to be configured like so already).

Solution 5 - Amazon Web-Services

Have your user data create a file in your ec2's /tmp directory to see if it works:

bob.txt:

#!/bin/sh
echo 'Woot!' > /home/ec2-user/user-script-output.txt

Then launch with:

ec2-run-instances -f bob.txt -t t1.micro -g ServerPolicy ami-05cf5c6d -v

Solution 6 - Amazon Web-Services

Enable logging for your user data

Eric Hammond, in "Logging user-data Script Output on EC2 Instances (2010, Hammond)", suggests:

exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1

Take care to put a space between the two > > characters at the beginning of the statement.

Here’s a complete user-data script as an example:

#!/bin/bash -ex
exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1
echo BEGIN
date '+%Y-%m-%d %H:%M:%S'
echo END

Solution 7 - Amazon Web-Services

Put this in userdata

touch /tmp/file2.txt

Once the instance is up you can check whether the file is created or not. Based on this you can tell if the userdata is executed or not.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPravinView Question on Stackoverflow
Solution 1 - Amazon Web-ServicesRavi PrajapatiView Answer on Stackoverflow
Solution 2 - Amazon Web-ServicesShankar ARULView Answer on Stackoverflow
Solution 3 - Amazon Web-ServicesPaoloView Answer on Stackoverflow
Solution 4 - Amazon Web-ServicesSteffen OpelView Answer on Stackoverflow
Solution 5 - Amazon Web-ServicesMauvis LedfordView Answer on Stackoverflow
Solution 6 - Amazon Web-Servicesutkarsh-devopsView Answer on Stackoverflow
Solution 7 - Amazon Web-Servicesyogesh thotaView Answer on Stackoverflow