无法按标记筛选实例并获取实例列表。请帮助我如何继续
import boto3
ec2 = boto3.resource('ec2')
def lambda_handler(event, context):
# Use the filter() method of the instances collection to retrieve
# all running EC2 instances.
filters = [{'Name':'OS_Name', 'Values':['Rstudio']}]
#filter the instances
instances = ec2.instances.filter(Filters=filters)
#locate all running instances
RunningInstances = [instance.id for instance in instances]
#print the instances for logging purposes
#print RunningInstances
#make sure there are actually instances to shut down.
if len(RunningInstances) > 0:
#perform the shutdown
shuttingDown = ec2.instances.filter(InstanceIds=RunningInstances).stop()
print (shuttingDown)
else:
print ("Nothing to see here")
您需要指定过滤器的类型,因此在本例中它将是tag
filters = [{'Name':'tag:OS_Name', 'Values':['Rstudio']}]
从boto3文件
标记:-分配给资源的标记的键/值组合。使用过滤器名称中的标记键和标记值作为过滤器值。例如,要查找具有键所有者和值TeamA的标记的所有资源,请指定tag:Owner for 筛选器名称和筛选器值的TeamA
相关问题 更多 >
编程相关推荐