How to check whether my user data passing to EC2 instance working or not?

时光毁灭记忆、已成空白 提交于 2019-12-03 06:40:32

问题


While creating new AWS EC2 instance using EC2 command line API, i am passing some user data to new instance.

Now how i came to know whether that user data executed or not ?

How should i check it ?


回答1:


You can verify using following steps

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

You can see all log's of your user data script and it will also create /etc/cloud folder.




回答2:


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 - Instance Settings - Get System log

This should open a modal window with the system logs




回答3:


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




回答4:


Enable logging for your user data

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




回答5:


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



来源:https://stackoverflow.com/questions/15904095/how-to-check-whether-my-user-data-passing-to-ec2-instance-working-or-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!