# referencing the instance id of an EC2 resource just created

**URL:** <https://forum.ansible.com/t/referencing-the-instance-id-of-an-ec2-resource-just-created/24775>\
**Category:** Ansible Project\
**Created:** [June 20, 2017, 9:18pm UTC](https://forum.ansible.com/t/referencing-the-instance-id-of-an-ec2-resource-just-created/24775 "2017-06-20T21:18:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bill\_L](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Bill\_L](https://forum.ansible.com/u/Bill_L)\
**Post date:** [June 20, 2017, 9:18pm UTC](https://forum.ansible.com/t/referencing-the-instance-id-of-an-ec2-resource-just-created/24775/1 "2017-06-20T21:18:28Z")

</div>

Hello,  
I am having a difficult time referencing the instance id of an EC2 resource that I have just created. After I create it, I would immediately like to terminate it. My code is below:

Thank you,  
Bill

---

<div class="post-metadata">

**Author:** ![Michael\_Bushey](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@Michael\_Bushey](https://forum.ansible.com/u/Michael_Bushey)\
**Post date:** [June 21, 2017, 1:21am UTC](https://forum.ansible.com/t/referencing-the-instance-id-of-an-ec2-resource-just-created/24775/2 "2017-06-21T01:21:59Z")

</div>

I don’t think this works in Ansible. The ec2 functionality seems very marginal and defective. I split the playbooks into two parts. The first spins up the instance. Then I go to the aws console to assign the correct IAM role to the instance as I can’t figure out how to do this in Ansible. Then I run

ssh {IP or name} “test -e /usr/bin/python || (apt -qqy update && apt install -qqy python-minimal)”

to install python 2.x so Ansible can run commands on the machine. Then I can run the second playbook to configure the server.

---

<div class="post-metadata">

**Author:** ![Bill\_L](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Bill\_L](https://forum.ansible.com/u/Bill_L)\
**Post date:** [June 21, 2017, 1:38pm UTC](https://forum.ansible.com/t/referencing-the-instance-id-of-an-ec2-resource-just-created/24775/3 "2017-06-21T13:38:13Z")

</div>

Hmm. That seems rather complex. Do you happen to know what the prescribed method of doing this is? Does Ansible recommend using add\_hosts module?
