141

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?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Pravin
  • 2,871
  • 5
  • 26
  • 29
  • Depends on the AMI, if it supports cloud init, it will be executed. If not, it will be available via metadata requests and you need to handle it from there. – datasage Apr 09 '13 at 14:33
  • @datasage : i am using cloud supported AMI. – Pravin Apr 09 '13 at 19:14
  • It's very important to show what you attempted in your question, along with its results. See "[ask]" and [mre] and their linked pages. – the Tin Man Feb 25 '22 at 22:48

7 Answers7

230

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Ravi Prajapati
  • 2,536
  • 1
  • 11
  • 9
81

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

Shankar ARUL
  • 12,642
  • 11
  • 68
  • 69
  • 3
    Amazon must have changed it again. I do not see a log. I see only the script. – falsePockets Jan 22 '19 at 03:16
  • 7
    @falsePockets, nah, it works. note that he wrote you should click on "Get System Log", not on "View/Change User Data" (which for some reason is highlighted in the screenshot). – EagleBeak May 23 '19 at 14:43
  • 1
    'Get System Log' seems to no longer be an option provided by AWS, at least through this menu path. – Meadowlark Bradsher Oct 20 '20 at 19:54
  • 3
    Check this out: `Right-click on the instance > Monitoring and troubleshoot > Get system log` – Verma Aman Nov 04 '20 at 15:25
  • 1
    Additionally you can ssh into the instance and view `/var/log/cloud-init.log`. There you should be able to find a line like `Writing to /var/lib/cloud/instances/i-064ea6f3ea0d3xxxx/user-data.txt - wb: [600] 370 bytes` where you can review the user data being used. – Federico Jan 11 '21 at 00:43
18

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"
Paolo
  • 21,270
  • 6
  • 38
  • 69
4

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).

Steffen Opel
  • 63,899
  • 11
  • 192
  • 211
  • Can i pass S3cms and s3fs commands as a part of user data to E2 instance ? – Pravin Apr 21 '13 at 11:54
  • @Pravin - Sure, user data scripts are ordinary shell scripts, i.e. you can basically do whatever you can do on the command line resp. in a local shell script. Of course you need to ensure that your requirements like `s3fs` are installed on the EC2 instance, i.e. either provided by the AMI already or provisioned by yourself from the user data script before using them in turn. – Steffen Opel Apr 21 '13 at 13:20
  • :- I hava installed s3cmd on AMI and using this preconfigured AMI, i am creating new instances. For this i am passing user data as Creating a new diretory on new instance and downloading file from S3. But this script creates only directory, not downloads file from S3. Just look following llink http://stackoverflow.com/questions/16130952/passing-s3cmd-commands-as-user-data-to-ec2 – Pravin Apr 22 '13 at 02:17
  • @Pravin - I've posted [an answer](http://stackoverflow.com/a/16142934/45773) over there. – Steffen Opel Apr 22 '13 at 08:51
2

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

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
utkarsh-devops
  • 567
  • 7
  • 12
0

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
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Mauvis Ledford
  • 40,827
  • 17
  • 81
  • 86
  • can i pass s3cmd and s3fs commands as user data to ec2 instance ? – Pravin Apr 21 '13 at 11:54
  • Your scripts are executed on the remote machine on bootup (early on in the phase) so as long as it's preinstalled and loaded. – Mauvis Ledford Apr 22 '13 at 06:35
  • can you launch from the EC2 online dashboard and have the script run? This is what I am having difficulty understanding. I thought you just put the bash script in the user-data text field.... – chris Frisina Sep 10 '18 at 15:46
0

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303