# Retrieving vpc\_subnet\_id by resource tags

**URL:** <https://forum.ansible.com/t/retrieving-vpc-subnet-id-by-resource-tags/16782>\
**Category:** Ansible Project\
**Created:** [November 14, 2014, 3:47am UTC](https://forum.ansible.com/t/retrieving-vpc-subnet-id-by-resource-tags/16782 "2014-11-14T03:47:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lorin\_Hochstein](https://avatars.discourse-cdn.com/v4/letter/l/5f9b8f/32.png) [@Lorin\_Hochstein](https://forum.ansible.com/u/Lorin_Hochstein)\
**Post date:** [November 14, 2014, 3:47am UTC](https://forum.ansible.com/t/retrieving-vpc-subnet-id-by-resource-tags/16782/1 "2014-11-14T03:47:03Z")

</div>

Let’s say I create an ec2 vpc like this:

tasks:

- name: create a vpc  
ec2\_vpc:  
internet\_gateway: True  
resource\_tags: { env: production }  
cidr\_block: 10.0.0.0/16  
subnets:
- cidr: 10.0.0.0/24  
resource\_tags:  
env: production  
tier: web
- cidr: 10.0.1.0/24  
resource\_tags:  
env: production  
tier: db  
route\_tables:
- subnets:
- 10.0.0.0/24
- 10.0.1.0/24  
routes:
- dest: 0.0.0.0/0  
gw: igw

register: vpc

Later in the play, I want to create an instance inside of subnet 10.0.0.0/24 (web tier):

ec2:  
image: “{{ ubuntu\_image.ami }}”  
instance\_type: “{{ instance\_type }}”  
assign\_public\_ip: True  
key\_name: mykey  
group: [web, ssh]  
instance\_tags: { type: web, env: production }  
exact\_count: “{{ count }}”  
count\_tag: { type: web }  
vpc\_subnet\_id: “{{ vpc.subnets[0].id}}”

Is vpc.subnets[0] guaranteed to correspond to the first subnet that was created (10.0.0.0/24)? If not, is there a way I can specify “the vpc subnet that corresponds to resource tag {env: production, tier: web}?”

Lorin

---

<div class="post-metadata">

**Author:** ![vaida.dan](https://avatars.discourse-cdn.com/v4/letter/v/5daacb/32.png) [@vaida.dan](https://forum.ansible.com/u/vaida.dan)\
**Post date:** [December 29, 2014, 3:19pm UTC](https://forum.ansible.com/t/retrieving-vpc-subnet-id-by-resource-tags/16782/2 "2014-12-29T15:19:36Z")

</div>

Hi Lorin,

It looks like your question is similar to mine: [https://groups.google.com/d/msg/ansible-project/Xy3kt0Mr8YY/eB6TqMN0Y3EJ](https://groups.google.com/d/msg/ansible-project/Xy3kt0Mr8YY/eB6TqMN0Y3EJ)  
Didn’t had the time to explore potential fixes yet though…  
Perhaps you have some updates from your side.

Cheers,

Dan.
