用于基于标签启动 EC2 实例的 Lambda 函数



我在 AWS 中创建了一个 python Lambda 函数,用于根据部署到它们的 TAG 启动一些 EC2 实例。它检查实例是否已停止并仅在其上运行。

import boto3
import logging
ec2 = boto3.resource('ec2')
def lambda_handler(event, context):
filters = [{
'Name': 'tag:STARTUP',
'Values': ['YES']
},
{
'Name': 'instance-state-name', 
'Values': ['stopped']
}]
instances = instances.filter(Filters=filters)
stoppedInstances = [instance.id for instance in instances]
if len(stoppedInstances) > 0:
startingUp = instances.filter(instances).start()

当我尝试运行它时,出现以下错误:

START RequestId: XXX Version: $LATEST
filter() takes 1 positional argument but 2 were given: TypeError
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 17, in lambda_handler
startingUp = ec2.instances.filter(instances).start()
TypeError: filter() takes 1 positional argument but 2 were given

虽然我到处看 FILTER 变量都能够处理多个参数,但不知何故我只能使用一个?

我使用的是 Python 3.6 运行时,并且我使用与其他功能相同的角色,该函数可以正常工作以启动服务器(仅基于时间(。

你能指教吗?谢谢!

这完成了@最后一行的技巧!感谢您的评论,为我指明了正确的方向:)

startingUp = ec2.instances.filter(InstanceIds=stoppedInstances).start()

最新更新