-2

I am currently setting up a script that will generate a new EC2 for clients that would like to test our service for a period of seven days. I am using the AWS CLI tools to spin up the new instances as needed and am looking for a way to automatically terminate said instances after seven days. Is this something that AWS supports? I have been looking everywhere for a solution.

The code I use to startup the new instances is:

aws ec2 run-instances --image-id ami-whatever --count 1 --instance-type t2.micro --key-name genericKey \
     --security-groups launch-wizard --block-device-mappings \
     "[{\"DeviceName\":\"/dev/sdf\",\"Ebs\":{\"SnapshotId\":\"snap-whatever\"}}]"

Is there any way that I can alter this to make the instance terminate after a seven day period?

They are all ubuntu Trusty EC2s.

Thanks!

Downwithopp
  • 73
  • 1
  • 8
  • 1
    Possible duplicate of [Self-Terminating AWS EC2 Instance?](http://stackoverflow.com/questions/10541363/self-terminating-aws-ec2-instance) – AStopher Mar 08 '16 at 20:43
  • Are you looking for a AWS CLI solution for this? – helloV Mar 08 '16 at 20:52
  • @cybermonkey I don't feel like sshing into each individual and running a command (which was the top answer on the other thread) is the same as forcing the EC2 to terminate after seven days during the creation process. – Downwithopp Mar 08 '16 at 20:53
  • @helloV yes, I want to be able to alter the code above to make this happen, I am just not sure if that is possible. – Downwithopp Mar 08 '16 at 20:53
  • @Downwithopp Then simply use a bash script on boot and deploy instances using an image that you'd capture from the test instance. – AStopher Mar 08 '16 at 20:55
  • @cybermonkey that is still not answering the actual question, just offering a workaround. – Downwithopp Mar 08 '16 at 20:57
  • @Downwithopp No, it *is* answering the question, I don't see how you consider that a workaround? – AStopher Mar 08 '16 at 20:58
  • @cybermonkey "Is there any way that I can alter this to make the instance terminate after a seven day period?" – Downwithopp Mar 08 '16 at 20:59
  • 2
    @Downwithopp if you absolutely have to do this as part of the run-instances command, then you will have to pass a user-data script into that command. – Mark B Mar 08 '16 at 21:01
  • @Downwithopp It seems that the only issue is that you aren't aware of the powerful array of tools at your disposal. It's not possible to do what you ask directly from the commandline, the best way is the way I described. – AStopher Mar 08 '16 at 21:06

1 Answers1

2

You could do something like the following:

  1. Set the instance shutdown behavior to "terminate" when you create the instance
  2. Run a cron script, perhaps hourly, that checks the system uptime (or the EC2 instance's metadata), and initiates a shutdown command when the uptime is greater than 7 days.

Another option would be:

  1. Set a tag on these servers to distinguish them from servers you don't want to shutdown.
  2. Schedule a Lambda function to run periodically.
  3. Have the Lambda function query for the metadata of all your EC2 instances that have the specified tag, and issue a terminate command for servers with a launch time was greater than 7 days ago.
Mark B
  • 183,023
  • 24
  • 297
  • 295