Python “如何修复”;模块lambda_函数中的语法错误;

Python “如何修复”;模块lambda_函数中的语法错误;,python,amazon-web-services,lambda,Python,Amazon Web Services,Lambda,我正在尝试运行lambda函数来停止EC2实例。但是我却有错误 尝试添加返回{print'停止您的实例:'+str(实例)}和其他内容。但它不起作用 感谢你帮我指出问题所在。多谢各位 import boto3 # Region your instances are in, e.g. 'us-east-1' region = 'ap-southeast-1' # Instances ID: ex. ['X-XXXXXXXX', 'X-XXXXXXXX'] instances = 'i-02dc

我正在尝试运行lambda函数来停止EC2实例。但是我却有错误

尝试添加返回{print'停止您的实例:'+str(实例)}和其他内容。但它不起作用

感谢你帮我指出问题所在。多谢各位

import boto3

# Region your instances are in, e.g. 'us-east-1'
region = 'ap-southeast-1'

# Instances ID: ex. ['X-XXXXXXXX', 'X-XXXXXXXX']
instances = 'i-02dc8a50ad60d1ab0'

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

保存并运行测试后,它应该会成功运行并终止我的EC2实例

我猜这是因为您正在python3上运行,
print
不再是关键字。这是一个函数。你得叫它

e、 g


如果您这样使用它,它将在两个版本的python上都能工作。

“errorMessage”:“模块'lambda_function'中的语法错误:无效语法(lambda_function.py,第12行)”,第12行:print“stopped your instances:'+str(instances)它运行的是python2还是python3?您应该像使用函数一样使用print,这两种方法都适用。e、 g.
print('stopped your instances:'+str(instances))
print('stopped your instances: ' + str(instances))