# Role to Create/Tear Down EC2 Instances

**URL:** <https://forum.ansible.com/t/role-to-create-tear-down-ec2-instances/26626>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [February 1, 2018, 7:58pm UTC](https://forum.ansible.com/t/role-to-create-tear-down-ec2-instances/26626 "2018-02-01T19:58:34Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Justin2](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@Justin2](https://forum.ansible.com/u/Justin2)\
**Post date:** [February 1, 2018, 7:58pm UTC](https://forum.ansible.com/t/role-to-create-tear-down-ec2-instances/26626/1 "2018-02-01T19:58:34Z")

</div>

I have a few questions on handling the creation/tear down of an ec2 instance.

I have a role ( its a task atm, but Ill be migrating it over’)

`

- name: dallascowboys | Deployments  
include\_tasks:  
tasks/customer-instance.yml  
vars:  
customer\_name: dallascowboys  
customer\_ip: ‘10.99.100.10’  
customer\_instance\_count: 1  
customer\_eip\_state: present  
customer\_eni\_state: present  
tags:
- dallascowboys  
`

`

- name: Customer | Configure ENI  
ec2\_eni:  
state: “{{ customer\_eni\_state }}”  
description: ‘{{ customer\_name }} ENI’  
private\_ip\_address: ‘{{ customer\_ip }}’  
region: “{{ vpc\_region }}”  
security\_groups: “{{ secgrp\_appliance\_id }}”  
subnet\_id: “{{ public\_subnet\_id }}”  
register: customer\_eni

- name: Customer | Configure EIP  
ec2\_eip:  
state: “{{ customer\_eip\_state }}”  
region: “{{ vpc\_region }}”  
in\_vpc: true  
device\_id: “{{ customer\_eni.interface.id }}”

- name: Customer | Configure EC2 Instance  
ec2:  
key\_name: “{{ aws\_mfacloud\_key }}”  
instance\_type: t2.medium  
image: “{{ mfa\_ami }}”  
region: “{{ vpc\_region }}”  
instance\_profile\_name: ‘{{ role\_appliance\_name }}’  
network\_interfaces: “{{ customer\_eni.interface.id }}”  
user\_data: “{{ lookup(‘file’,‘windows-remoting’) }}”  
instance\_tags:  
Name: ec2-cloudmfa-{{ customer\_name }}-prd  
Customer: “{{ customer\_name }}”  
Environment: prd  
Patch Group: cloudmfa  
Ansible: true  
AMI: True  
count\_tag:  
Name: ec2-cloudmfa-{{ customer\_name }}-prd  
exact\_count: “{{ customer\_instance\_count }}”  
register: instance

- name: debug instance  
debug:  
var: instance

- name: Customer | Grab Password  
ec2\_win\_password:  
instance\_id: “{{ instance.tagged\_instances[0].id }}”  
region: “{{ vpc\_region }}”  
key\_file: “~/.ssh/{{ aws\_mfacloud\_key }}.pem”  
wait: yes  
wait\_timeout: 300  
register: winpass  
`

This works OK to spin an instance up, my issue is when I want to destroy the instance for some reason. I first set count to 0, which causes the job to fail because the ec2\_win\_password module. So I run it again, with eip absent and this works, but again fails on the ec2\_win\_password. I then run it a third time with eni absent, and it fails with the following error.

> fatal: [localhost]: FAILED! =\> {“changed”: false, “failed”: true, “msg”: “state is absent but all of the following are missing: eni\_id”}

My issues are. Is there a better way to create these instances? Its going to be ~20 or so instances, that are configured the same, but will end up with different software on them. So I cant just say “spin up 20 instances”. Is there a better way to have ec2\_win\_password only execute when I have created, or started an instance? As opposed to turned off/terminated an instance? I assume im missing something obvious about the tear down of the EIP and ENI as well.

Sorry for the vagueness here, I know im probably missing some info that would be helpful so please just ask.
