How can I filter AWS instances using Tag and Value using boto3
?
import boto3
ec2 = boto3.resource('ec2')
client = boto3.client('ec2')
response = client.describe_tags(
Filters=[{'Key': 'Owner', 'Value': 'user@example.com'}])
print(response)
How can I filter AWS instances using Tag and Value using boto3
?
import boto3
ec2 = boto3.resource('ec2')
client = boto3.client('ec2')
response = client.describe_tags(
Filters=[{'Key': 'Owner', 'Value': 'user@example.com'}])
print(response)
You are using the wrong API. Use describe_instances
import boto3
client = boto3.client('ec2')
custom_filter = [{
'Name':'tag:Owner',
'Values': ['user@example.com']}]
response = client.describe_instances(Filters=custom_filter)
boto3.client.describe_tags() is universal, but it is tedious to use. because you need to nest and specify the services, tag key name and tag values to filter . i.e.
client = boto3.client('ec2')
filters =[
{'Name': 'resource-type', 'Values': ['instance']},
{'Name': 'Key', 'Values': ['Owner']},
{'Name': 'Values', 'Values' : ['user@example.com']}
]
response = client.describe_instances(Filters=filters)
As @helloV suggested, it is much easier to use describe_instances(). describe_tags is there to allow user to create function to traverse all the services tags.