I want to get an automated process for AMI creation going, and one piece remaining is automatically cleaning up the instance after image creation.

The instance is booted with a user-data script that does the necessary setup, then kicks off image creation from self using AWS CLI. Then it shuts down. I could go with --no-reboot option and wait there until the image is ready, then terminate, but the docs state that "file system integrity on the created image can't be guaranteed", so I want to avoid using it.

What's the best way to kill the instance from itself after image creation is completed?

2 Answers 2


There are a couple approaches to consider for how to terminate an instance from itself:

  1. Start the EC2 instance with the instance-initiated-shutdown-behavior set to "terminate", then "sudo halt" or equivalent from inside the instance.

  2. Start the EC2 instance with an IAM role that allows it to terminate itself, then invoke the ec2 terminate-instances API from the instance (e.g., using the aws-cli). Get the instance id from the instance metadata.

The first method is quite a bit easier and has less risk of the instance being able to terminate other instances, but you're already calling AWS API from the instance so you're half way to rm /etc/rc2.d/S90halt-after-create-image the second method as well.

Now for the question of how to trigger the termination after the create-image reboot.

You could simply drop the desired halt/terminate command into a startup script like /etc/rc.local and it would get run when the system comes back up. As @AlexB points out in the comments, you need to make sure this doesn't cause new instances with the image to halt, so perhaps test the instance_id.

There's no need to wait for the new AMI creation to complete. It will finish just fine even though your instance is no longer running.

Here's a quick hack that has lots of room for improvement:

# This would be dangerous in a non-EC2 environment or on an instance that
# does not fit the criteria in the original question.
original_instance_id=$(curl -s http://instance-data/latest/meta-data/instance-id)
cat >/etc/rc2.d/S90halt-after-create-image <<EOF
rm \$0
instance_id=\$(curl -s http://instance-data/latest/meta-data/instance-id)
test "\$instance_id" = "$original_instance_id" && sudo halt
chmod +x /etc/rc2.d/S90halt-after-create-image

This code creates a startup script that removes itself and halts the system if certain conditions are true. Could cause general havok if things go wrong. Tested on Ubuntu 12.04. May not work elsewhere.

  • But if I create an /etc/rc.local script right before reboot, wouldn't it become a part of the image? That is, every instance made from that image would auto-reboot infinitely.
    – Alex B
    Apr 27, 2014 at 13:29
  • @AlexB Good catch. You could include a test that only halts if the instance id is the id of the current instance. May 2, 2014 at 3:22
  • Eric, thanks for all your work in this field. I've learned most of my tricks from your many blog posts on the subject.
    – dmourati
    May 2, 2014 at 3:52

Check out packer.io an open source image building tool from HashiCorp.

You should use it because it is a standard tool utilized by companies including Google, Netflix, Target, and others. It is also incorporated into a higher order open source CD tool called Spinnaker.

You can use the AMAZON AMI BUILDER.

  • 2
    Wondering about the reason for downvote. Packer is awesome and solves the stated problem.
    – dmourati
    May 6, 2014 at 6:12
  • I'd guess that the downvote reason is that your 10 words provide no details. What is it? Why should the poster use it? Why should I? Strengths? Weaknesses?
    – Mark
    Mar 9, 2017 at 21:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.