AWS Boto3-如何使用多个过滤器并遍历标签名称/值?

问题描述

当前,我正在使用boto3两次调用AWS ec2,以获取以标签名org-production-*org-non-production-*开头的subnetID。如何在python中结合这两个功能,仍然能够访问SubnetID的all_prod_subnets和all_non_prod_subnets?我想避免重复代码仅对aws ec2进行一次调用,获取所有子网并对其进行迭代,然后根据标签名称过滤响应。

def get_all_production_subnets_from_accounts():
    subnet = vpc_client.describe_subnets(
        Filters=[{'Name': 'tag:Name','Values': ['org-production-*']}])['Subnets']
    if len(subnet) > 0:
        # print([s['SubnetId'] for s in subnet])
        all_prod_subnets =  [ s['SubnetId'] for s in subnet ]
        print("[DEBUG]Queried Subnet ID's of Production are: {}".format(all_prod_subnets))
        return all_prod_subnets
    else:
        return None

def get_all_nonproduction_subnets_from_acccounts():
    subnet = vpc_client.describe_subnets(
        Filters=[{'Name': 'tag:Name','Values': ['org-non-production-*']}])['Subnets']
    if len(subnet) > 0:
        # print([s['SubnetId'] for s in subnet])
        all_non_prod_subnets =  [ s['SubnetId'] for s in subnet ]
        print("[DEBUG]Queried Subnet ID's of Non-Production are: {}".format(all_non_prod_subnets))
        return all_non_prod_subnets
    else:
        return None

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)