【问题标题】:Lambda function to start EC2 instance based on tag基于标签启动 EC2 实例的 Lambda 函数
【发布时间】:2018-01-09 19:41:51
【问题描述】:

我在 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 运行时,并且我使用的角色与其他可以正常启动服务器的函数相同(仅基于时间)。

你能建议吗?谢谢!

【问题讨论】:

标签: python amazon-web-services amazon-ec2 aws-lambda


【解决方案1】:

这成功了@最后一行!感谢您的 cmets 为我指明了正确的方向 :)

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

【讨论】:

    猜你喜欢
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-26
    • 2021-05-08
    • 2020-12-18
    • 1970-01-01
    相关资源
    最近更新 更多