From 064ed63843f008b37a0acfed487145a5f09770e1 Mon Sep 17 00:00:00 2001 From: Adam Chainz Date: Tue, 24 May 2016 16:00:59 +0100 Subject: [PATCH] Fix ec2 inventory for potentially inconsistent data (#12642) --- contrib/inventory/ec2.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/contrib/inventory/ec2.py b/contrib/inventory/ec2.py index 6a0b1100242..bf3ba28ea06 100755 --- a/contrib/inventory/ec2.py +++ b/contrib/inventory/ec2.py @@ -521,8 +521,20 @@ class Ec2Inventory(object): else: reservations = conn.get_all_instances() + # Pull the tags back in a second step + # AWS are on record as saying that the tags fetched in the first `get_all_instances` request are not + # reliable and may be missing, and the only way to guarantee they are there is by calling `get_all_tags` + instance_ids = [] + for reservation in reservations: + instance_ids.extend([instance.id for instance in reservation.instances]) + tags = conn.get_all_tags(filters={'resource-type': 'instance', 'resource-id': instance_ids}) + tags_by_instance_id = defaultdict(dict) + for tag in tags: + tags_by_instance_id[tag.res_id][tag.name] = tag.value + for reservation in reservations: for instance in reservation.instances: + instance.tags = tags_by_instance_id[instance.id] self.add_instance(instance, region) except boto.exception.BotoServerError as e: