无法通过标签过滤实例并获取实例列表。请帮助我进一步进行操作。
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")
答案 0 :(得分:0)
您需要指定过滤器的类型,因此在这种情况下将是标签。 filters = [{'Name':'tag:OS_Name', 'Values':['Rstudio']}]
来自boto3文档
tag:-分配给资源的标签的键/值组合。使用过滤器名称中的标记键和标记值作为过滤器值。例如,要查找所有带有标签的键,该标签的标签具有所有者Owner和值TeamA,请指定tag:Owner作为过滤器名称,并指定TeamA作为过滤器值。