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

7

158

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?

Johore answered 9/4, 2013 at 14:4 Comment(3)
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.Atwitter
@Atwitter : i am using cloud supported AMI.Johore
It's very important to show what you attempted in your question, along with its results. See "How to Ask" and minimal reproducible example and their linked pages.Unalterable
H
265

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.

Hausa answered 8/9, 2015 at 14:47 Comment(5)
This should be the correct answer. That's the most reliable way to do so.Smilacaceous
Good answer. I had to use sudo to access the cloud-init-output.log.Tonneson
Knowing these paths makes life so much easier, thanks!Porterfield
What about on Windows?Anomalism
I found /var/log/cloud-init-output.log contains the ouputMartins
C
92

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

Calan answered 19/1, 2017 at 19:13 Comment(5)
Amazon must have changed it again. I do not see a log. I see only the script.Cabral
@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).Tomfool
'Get System Log' seems to no longer be an option provided by AWS, at least through this menu path.Negate
Check this out: Right-click on the instance > Monitoring and troubleshoot > Get system logSherborn
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.Baghdad
M
22

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"
Misinform answered 22/7, 2021 at 9:25 Comment(0)
R
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).

Ruiz answered 9/4, 2013 at 14:35 Comment(4)
Can i pass S3cms and s3fs commands as a part of user data to E2 instance ?Johore
@Johore - 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.Ruiz
:- 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 #16131452Johore
@Johore - I've posted an answer over there.Ruiz
C
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

Chief answered 6/3, 2019 at 8:7 Comment(3)
Unless your name is Eric Hammond, which I doubt by reading your username, you should cite your sources: alestic.com/2010/12/ec2-user-data-outputSchramm
/var/log/user-data.log doesn't exist on my amazon linux ec2Dickdicken
Please read "Link-only answers".Unalterable
A
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
Apophthegm answered 9/4, 2013 at 19:26 Comment(3)
can i pass s3cmd and s3fs commands as user data to ec2 instance ?Johore
Your scripts are executed on the remote machine on bootup (early on in the phase) so as long as it's preinstalled and loaded.Apophthegm
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....Animalize
U
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.

Unchristian answered 24/2, 2022 at 11:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.