如何检查传递到 EC2实例的用户数据是否正常工作

在使用 EC2命令行 API 创建新的 AWS EC2实例时,我将一些用户数据传递给新实例。

如何知道该用户数据是否已执行?

100272 次浏览

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

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

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.

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

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


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"

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.