尝试使用boto3描述我的所有实例并过滤当前未运行的每个实例。
使用这篇文章作为构建我的filter-http://rob.salmond.ca/filtering-instances-by-name-with-boto3/的引用。
当我尝试使用此过滤器按状态过滤实例时-
filters = [{
'Name': 'tag:State',
'Values': ['running']
}]
查询返回为空(这很有意义,因为状态值嵌套在它自己的字典中。
我的问题是-如何使用滤镜参数访问嵌套标签?
请您参考如下方法:
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)
希望对您有所帮助!




