somashekhar
somashekhar

Reputation: 37

display instance ids based on instance tags aws

need instance ids if particular tag['key'] is not tagged i am trying to list instance ids based on some tags for ex :: if tag:name=env and value = 'Not tagged'(like it filters in console) ()

import boto3
client=boto3.client('ec2',region_name='us-east-1')
filters=[{'Name':'env', 'Values':['Not tagges']}]
response=client.describe_instances(Filters=filters)
print(response)

Upvotes: 0

Views: 598

Answers (2)

r0ck
r0ck

Reputation: 192

Parameters Filters (list) --
The filters.

tag-key - The key of a tag assigned to the resource.
Use this filter to find all resources that have a tag with a specific key, regardless of the tag value.


if the problem still presists...
Buzz me...
r0ck

Upvotes: 0

marcincuber
marcincuber

Reputation: 3791

Your filters should be as follows:

filters = 
  [{
    'Name':'tag:env', 
    'Values': ['not tagged']
  }]

response=client.describe_instances(Filters=filters)

and then for example you can further filter and select only instance IDs + Hypervisor:

for r in response['Reservations']:
    for i in r['Instances']:
        print(i['InstanceId'], i['Hypervisor'])

Upvotes: 2

Related Questions