Self-Terminating AWS EC2 Instance?

2019-01-05 01:10发布

Is there a way that Amazon Web Services EC2 instances can be self terminating? Does Amazon have anything that allows an instance to terminate itself ("Hara-Kiri") after running for more than say an hour? I could change the scripts on the running instance to do this itself, but that might fail and I don't want to edit the image, so I would like Amazon to kill the instance.

4条回答
狗以群分
2楼-- · 2019-01-05 01:54

Here is my script for Self-Terminating

$ EC2_INSTANCE_ID="`wget -q -O - http://instance-data/latest/meta-data/instance-id || die \"wget instance-id has failed: $?\"`"
$ echo "ec2-terminate-instances $EC2_INSTANCE_ID" | at now + 55 min || die 'cannot obtain instance-id'

If you want to assign it as Self-Stopping on Self-Terminating, you can do it one time only.

In your EC2 Console go to Instance Settings, change Shutdown Behavior to Stop.
Configure /etc/cloud/cloud.cfg, you may refer to how to run a boot script using cloud-init.
Follow answer from Eric Hammond, put the command in a file and locate it in scripts-per-boot path:

$ echo '#!/bin/sh' > per-boot.sh
$ echo 'echo "halt" | at now + 55 min' >> per-boot.sh
$ echo 'echo per-boot: `date` >> /tmp/per-boot.txt' >> per-boot.sh
$ chmod +x per-boot.sh
$ sudo chown -R root per-boot.sh
$ sudo mv -viu per-boot.sh /var/lib/cloud/scripts/per-boot

Reboot your instance, check if the script is executed:

$ cat /tmp/per-boot.txt 
per-boot: Mon Jul 4 15:35:42 UTC 2016

If so, just in case you forgot to stop your instance, it will assure you that the instance will do itself termination as stopping when it has run for 55 minutes or whatever time you set in the script.

Broadcast message from root@ip-10-0-0-32
        (unknown) at 16:30 ...

The system is going down for halt NOW!

PS: For everyone want to use the Self-Stopping, one thing you should note that not all EC2 types are self recovery on restarting. I recommend to use EC2-VPC/EBS with On/Off Schedule.

查看更多
地球回转人心会变
3楼-- · 2019-01-05 02:00

To have an instance terminate itself do both of these steps:

  1. Start the instance with --instance-initiated-shutdown-behavior terminate or the equivalent on the AWS console or API call.
  2. Run shutdown -h now as root. On Ubuntu, you could set this up to happen in 55 minutes using:

    echo "sudo halt" | at now + 55 minutes
    

I wrote an article a while back on other options to accomplish this same "terminate in an hour" goal:

Automatic Termination of Temporary Instances on Amazon EC2
http://alestic.com/2010/09/ec2-instance-termination

The article was originally written before instance-initiated-shutdown-behavior was available, but you'll find updates and other gems in the comments.

查看更多
欢心
4楼-- · 2019-01-05 02:04

You can do this

ec2-terminate-instances $(curl -s http://169.254.169.254/latest/meta-data/instance-id)

The ec2 will get its current instance id and terminate itself.

查看更多
啃猪蹄的小仙女
5楼-- · 2019-01-05 02:04

Hopefully this will work

instanceId = $(curl http://169.254.169.254/latest/meta-data/instance-id/)

/usr/bin/aws ec2 terminate-instances --instance-ids $instanceId --region us-east-1

Hope this help you !!!

查看更多
登录 后发表回答