简体   繁体   English

在 boto3 中添加多个过滤器

[英]Adding multiple filters in boto3

Hi I have a requirement to fetch ec2 instance details with tags as follows您好,我需要使用如下标签获取 ec2 实例详细信息

prod = monitor产品 = 监视器

test = monitor测试 = 监控

The objective is to list instances with these tags only.目的是仅列出具有这些标签的实例。 I was able to add one filter but not sure how to use multiple filters in ec2.instances.filter(Filters我能够添加一个过滤器,但不确定如何在 ec2.instances.filter(Filters

from collections import defaultdict

import boto3

# Connect to EC2
ec2 = boto3.resource('ec2')

# Get information for all running instances
running_instances = ec2.instances.filter(Filters=[{
    'Name': 'instance-state-name',
    'Values': ['running'] ,
    'Name': 'tag:prod',
    'Values': ['monitor']}])

ec2info = defaultdict()
for instance in running_instances:
    for tag in instance.tags:
        if 'Name'in tag['Key']:
            name = tag['Value']
            
                    
    # Add instance info to a dictionary         
    ec2info[instance.id] = {
        'Name': name,
        'Type': instance.instance_type,
        'State': instance.state['Name'],
        'Private IP': instance.private_ip_address,
        'Public IP': instance.public_ip_address,
        'Launch Time': instance.launch_time
        }

attributes = ['Name', 'Type', 'State', 'Private IP', 'Public IP', 'Launch Time']
for instance_id, instance in ec2info.items():
    for key in attributes:
        print("{0}: {1}".format(key, instance[key]))
    print("------")

Your syntax does not quite seem correct.您的语法似乎不太正确。 You should be supplying a list of dictionaries .您应该提供一个字典列表 You should be able to duplicate tags, too:您也应该能够复制标签:

Filters=[
    {'Name': 'instance-state-name', 'Values': ['running']},
    {'Name': 'tag:prod', 'Values': ['monitor']},
    {'Name': 'tag:test', 'Values': ['monitor']},
]

This should return instances with both of those tags .这应该返回带有这两个标签的实例。

If you are wanting instances with either of the tags, then I don't think you can filter it in a single call.如果您想要带有任何一个标签的实例,那么我认为您不能在一次调用中对其进行过滤。 Instead, use ec2.instances.all() , then loop through the returned instances using Python code and apply your logic.相反,使用ec2.instances.all() ,然后使用 Python 代码循环返回的实例并应用您的逻辑。

Try this;尝试这个;

for example;例如;

response = ce.get_cost_and_usage(
            Granularity='MONTHLY',
            TimePeriod={
                'Start': start_date,
                'End': end_date
            },
            GroupBy=[
                {
                    'Type': 'DIMENSION',
                    'Key': 'SERVICE'
                },
            ],
            Filter=
                {
                    "Dimensions": { "Key": "LINKED_ACCOUNT", "Values": [awslinkedaccount[0]] }, 
                    "Dimensions": { "Key": "RECORD_TYPE", "Values": ["Usage"] },
                },


            Metrics=[
                'BLENDED_COST',
            ],
        )


print(response)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM