尝试使用boto3来描述我的所有实例并筛选当前未运行的每个实例。 使用这篇文章作为构建我的过滤器的参考 - http://rob.salmond.ca/filtering-instances-by-name-with-boto3/ 。
当我尝试使用此过滤器按状态过滤实例时 -
filters = [{ 'Name': 'tag:State', 'Values': ['running'] }]查询返回为空(这很有意义,因为状态值嵌套在它自己的字典中。
我的问题是 - 如何使用filters参数访问嵌套标签?
Trying to use boto3 to describe all of my instances and filter every instance that is not currently running. Using this post as a reference for building my filter - http://rob.salmond.ca/filtering-instances-by-name-with-boto3/.
When I try to filter the instances by state using this filter -
filters = [{ 'Name': 'tag:State', 'Values': ['running'] }]The query comes back empty (which makes sense, since the state value is nested inside a dictionary of it's own.
My question is - how do I access a nested tag with the filters parameter?
最满意答案
session = boto3.Session(region_name="us-east-1") ec2 = session.resource('ec2', region) instances = ec2.instances.filter( Filters=[{'Name': 'instance-state-name', 'Values': ['stopped', 'terminated']}]) for instance in instances: print(instance.id, instance.instance_type)希望能帮助到你 !!
session = boto3.Session(region_name="us-east-1") ec2 = session.resource('ec2', region) instances = ec2.instances.filter( Filters=[{'Name': 'instance-state-name', 'Values': ['stopped', 'terminated']}]) for instance in instances: print(instance.id, instance.instance_type)Hope it helps !!
用boto3按状态过滤实例(Filter instances by state with boto3)尝试使用boto3来描述我的所有实例并筛选当前未运行的每个实例。 使用这篇文章作为构建我的过滤器的参考 - http://rob.salmond.ca/filtering-instances-by-name-with-boto3/ 。
当我尝试使用此过滤器按状态过滤实例时 -
filters = [{ 'Name': 'tag:State', 'Values': ['running'] }]查询返回为空(这很有意义,因为状态值嵌套在它自己的字典中。
我的问题是 - 如何使用filters参数访问嵌套标签?
Trying to use boto3 to describe all of my instances and filter every instance that is not currently running. Using this post as a reference for building my filter - http://rob.salmond.ca/filtering-instances-by-name-with-boto3/.
When I try to filter the instances by state using this filter -
filters = [{ 'Name': 'tag:State', 'Values': ['running'] }]The query comes back empty (which makes sense, since the state value is nested inside a dictionary of it's own.
My question is - how do I access a nested tag with the filters parameter?
最满意答案
session = boto3.Session(region_name="us-east-1") ec2 = session.resource('ec2', region) instances = ec2.instances.filter( Filters=[{'Name': 'instance-state-name', 'Values': ['stopped', 'terminated']}]) for instance in instances: print(instance.id, instance.instance_type)希望能帮助到你 !!
session = boto3.Session(region_name="us-east-1") ec2 = session.resource('ec2', region) instances = ec2.instances.filter( Filters=[{'Name': 'instance-state-name', 'Values': ['stopped', 'terminated']}]) for instance in instances: print(instance.id, instance.instance_type)Hope it helps !!
发布评论