Lambda关闭脚本不起作用

0

【以下的问题经过翻译处理】 我几年前使用Python 2.7设置了一个lambda脚本,它工作正常,当EC2实例退役或添加时,我能够根据需要删除或添加实例ID。AWS已经无法编辑使用Python 2.7创建的脚本。我正在尝试创建一个Python 3.8的脚本。在测试脚本时,我遇到了一个非常通用的错误:“There is an error in your JSON event. Correct it before saving.”

使用Python 2.7创建的脚本如下:

import boto3
region = 'us-east-1'

instances = [ID列表]

def lambda_handler(event, context):
    ec2 = boto3.client('ec2', region_name=region)
    ec2.stop_instances(InstanceIds=instances)
    print 'stopped your instances: ' + str(instances)

使用Python 3.8创建的脚本如下:

import boto3
region = 'us-east-1'
instances = [ID列表]
ec2 = boto3.client('ec2', region_name=region)

def lambda_handler(event, context):
    ec2.stop_instances(InstanceIds=instances)
    print('stopped your instances: ' + str(instances))

感觉语法中缺少了一些简单的部分,但是我却找不到到底是什么。

profile picture
EXPERTE
gefragt vor 5 Monaten21 Aufrufe
1 Antwort
0

【以下的回答经过翻译处理】 你的脚本看起来是正确的。

我刚刚测试了你的脚本,它可以正常工作。

看起来是其他问题,也许是你的测试事件本身有问题?

我使用了默认的测试事件:

{
  "key1": "value1",
  "key2": "value2",
  "key3": "value3"
}

另请参阅:https://aws.amazon.com/premiumsupport/knowledge-center/start-stop-lambda-eventbridge/

profile picture
EXPERTE
beantwortet vor 5 Monaten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen