# Idempotency in amazon..aws.ec2

**URL:** <https://forum.ansible.com/t/idempotency-in-amazon-aws-ec2/35118>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [May 5, 2021, 10:21pm UTC](https://forum.ansible.com/t/idempotency-in-amazon-aws-ec2/35118 "2021-05-05T22:21:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dave\_York](https://avatars.discourse-cdn.com/v4/letter/d/77aa72/32.png) [@Dave\_York](https://forum.ansible.com/u/Dave_York)\
**Post date:** [May 5, 2021, 10:21pm UTC](https://forum.ansible.com/t/idempotency-in-amazon-aws-ec2/35118/1 "2021-05-05T22:21:51Z")

</div>

Hi ansible squad!

I’ve been using amazon.aws.ec2 (or community.aws.ec2\_instance - ive been switching back and forth) to ensure there is an ec2 instance created for each {{ inventory\_hostname }} by using tags. It doesn’t seem to work.

Every time I run the playbook with the below play, it just creates a new ec2 instance? How can I make it more idempotent? If the instance already exists, dont create a new one?

Preferably without other plays?

- name: Ensure EC2 instance exists  
amazon.aws.ec2:  
aws\_access\_key: ‘{{ aws\_access\_key }}’  
aws\_secret\_key: ‘{{ aws\_secret\_key }}’  
region: ‘{{ aws\_region }}’  
image: ‘{{ aws\_ec2\_ami }}’  
instance\_type: ‘{{ aws\_instance\_type\_web }}’  
vpc\_subnet\_id: ‘{{ aws\_subnet\_id }}’  
group: ‘SG-{{ environment\_designator }}’  
key\_name: ‘Keypair-{{ environment\_designator }}’  
instance\_tags:  
Mytag.MyEnvironment: “”  
Mytag.MyEnvironment.Environment: ‘{{ environment\_designator }}’  
Mytag.MyEnvironment.Roles: web1  
Mytag.MyEnvironment.Hostname: ‘{{ inventory\_hostname }}’  
wait: yes  
assign\_public\_ip: no  
register: ec2\_instances  
delegate\_to: localhost

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 6, 2021, 6:48am UTC](https://forum.ansible.com/t/idempotency-in-amazon-aws-ec2/35118/2 "2021-05-06T06:48:50Z")

</div>

You should supply an ‘id’ parameter:

[https://docs.ansible.com/ansible/latest/collections/amazon/aws/ec2\_module.html#parameter-id](https://docs.ansible.com/ansible/latest/collections/amazon/aws/ec2_module.html#parameter-id)

---

<div class="post-metadata">

**Author:** ![Dave\_York](https://avatars.discourse-cdn.com/v4/letter/d/77aa72/32.png) [@Dave\_York](https://forum.ansible.com/u/Dave_York)\
**Post date:** [May 6, 2021, 3:17pm UTC](https://forum.ansible.com/t/idempotency-in-amazon-aws-ec2/35118/3 "2021-05-06T15:17:40Z")

</div>

That… makes sense. I should have ctrl-f ‘idempotency’ on the module documentation. I’ll give it a try, thanks for the help!
