How to check User Data status while launching the instance in aws

梦想的初衷 提交于 2020-07-16 16:51:31

问题


I am trying to launch aws instance with User Data. My User Data is a server installation process and i have to check whether the user data scripts are executed properly. Is there any option to check if the status of User data is completed ? I need to know the status since from that launched instance i am taking another image. As off now, i explicitly used time.sleep(90) for my process completion.

Note: I am using Boto library.

Any solution on this would be greatly appreciated!


回答1:


UPDATE

What I landed up doing was creating a marker file at the end of the user-data run. I had the node controller spawn one ssh session per ec2 node and run a simple busy-wait loop as a command on the other end, so it only returns when the file is created. I then just wait() for all the ssh sessions to exit or until the wait timeout occurs.

It's ugly, but it works. It's very frustrating that EC2 doesn't provide better facilities for signalling status from within instances.

Labels

One possible approach is to have the instance's user-data script add an additional label to the instance when it completes. You can poll the instance with update or do a describe-instances with a filter that includes only nodes with the tag you use to specify that user data has been updated.

This requires that you include a limited API key and secret in your user-data scripts when you send them. Don't use your regular api key and secret, make one with very limited IAM rights. Additionally the user-data script will probably want to delete its self when it's done.

SNS / SQS

I've also considered using the Simple Notification Service and/or SQS for this, but it seems like overkill.

Like setting tags it requires that the instance have its own EC2 credentials.

SNS is push-only, so you have to have an endpoint reachable by EC2. That's a pain. SQS is pull, but doesn't have message routing, so you need one queue per set of nodes you're bringing up. You have to pass the unique queue name into the instance or have the instance use EC2 credentials to query it from a tag, then have the instance use that particular queue.

So, yeah, a pain.

Console

Getting console output won't work, EC2 stops updating it shortly after the instance transitions to the 'running' state.

There doesn't appear to be any way, instance- or client-side, to force an update.

Marker file

When the cloud-init script finishes it can touch a marker file somewhere shell-accessible to the normal user. This is a bit annoying, as it requires ssh'ing into every node and then polling for the creation of the marker file. The pain of polling can be somewhat reduced by use of a loop like:

while ! test -e 'cloud-init-complete'
do
    inotifywait -qq -t 2 -e create -e moved_to . ||true
done

after the installation of the inotify-tools package. If you don't burn inotify-tools into your AMIs you'll want to replace inotifywait with a simple sleep and accept the extra latency, or do:

while ! test -e 'cloud-init-complete'
do
    if test -x /usr/bin/inotifywait; then
        inotifywait -qq -t 2 -e create -e moved_to . ||true
    else
        sleep 2
    fi
done

This still requires an ssh connection to each server, though, and that's a pain to monitor and poll.

Something smarter?

My dream solution is being able to send an additional request to the EC2 metadata service to set a special instance tag or custom extra status field.




回答2:


InstaceStatusOk Waiter waits until UserData script has completed:

waiter = client.get_waiter('instance_status_ok')
waiter.wait(InstanceIds=['i-12345'])

https://boto3.readthedocs.io/en/latest/reference/services/ec2.html#EC2.Waiter.InstanceStatusOk




回答3:


You use EC2 API Tools and log User Data output or just check ec2-get-console-output. Refer to this article: http://alestic.com/2010/12/ec2-user-data-output

Sorry, not sure about Boto

In your user-data script you log result to log-file. Once Instance is started, you check the file.




回答4:


If you're using Amazon Linux, you can use a chkconfig script in /etc/init.d with a Required-Start directive:

#!/bin/bash
# chkconfig:   345 95 95
# description: Description

### BEGIN INIT INFO
# Provides: userdatainit
# Description: Wait for user data
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Required-Start: cloud-init
# Required-Stop:
### END INIT INFO

case "$1" in
  start)
    echo "Do work here, will happen -after- UserData script/config."
    ;;
  stop)
    ;;
  *)
    echo "Usage: $0 {start|stop}"
    exit 1
    ;;
esac

exit 0

If you have saved this as a file like /etc/init.d/afteruserdata, then register it like so:

chown root:root /etc/init.d/afteruserdata
chmod 755 /etc/init.d/afteruserdata
chkconfig --add afteruserdata
chkconfig --level 345 afteruserdata on 

Then you can create the AMI and your code will run after the UserData script.

Caveat: cloud-init only does its thing the first time you boot an instance from the AMI.



来源:https://stackoverflow.com/questions/11245356/how-to-check-user-data-status-while-launching-the-instance-in-aws

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