# Handling Failure during AMI creation

**URL:** <https://forum.ansible.com/t/handling-failure-during-ami-creation/17037>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [December 12, 2014, 4:00am UTC](https://forum.ansible.com/t/handling-failure-during-ami-creation/17037 "2014-12-12T04:00:24Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Navid\_Paya](https://avatars.discourse-cdn.com/v4/letter/n/9fc29f/32.png) [@Navid\_Paya](https://forum.ansible.com/u/Navid_Paya)\
**Post date:** [December 12, 2014, 4:00am UTC](https://forum.ansible.com/t/handling-failure-during-ami-creation/17037/1 "2014-12-12T04:00:24Z")

</div>

Hi guys  
I have a playbook that:

- Creates an EC2 instance
- Provisions the instance using a few roles
- Creates an AMI out of that instance
- Destroys the instance after the AMI is created successfully  
Now the problem is if there’s a failure somewhere after the instance creation, let’s say npm is having issues which is quite normal or some ubuntu repository is having issues, and I run the playbook again, it will spin up a new instance. How can I handle this? Preferably a way to use the same instance if the failure happens before AMI creation part or at least automatically delete the instance if I hit an error before exiting the playbook successfully.

Here’s my current playbook:

`
