【问题标题】:How to fix "Syntax error in module lambda_function"如何修复“模块 lambda_function 中的语法错误”
【发布时间】:2019-09-30 03:20:26
【问题描述】:

我正在尝试运行 lambda 函数来停止 EC2 实例。但相反,我遇到了错误。

试图添加 return{print 'stopped your instances: ' + str(instances)} 和其他东西。但它不起作用。

感谢您在指出问题方面的帮助。谢谢

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 实例

【问题讨论】:

  • "errorMessage": "模块 'lambda_function' 中的语法错误:无效语法(lambda_function.py,第 12 行)",
  • line12: print 'stopped your instances: ' + str(instances)
  • 它运行的是python2还是python3?您应该像使用函数一样使用 print ,它对两者都适用。例如print( 'stopped your instances: ' + str(instances))

标签: python amazon-web-services lambda


【解决方案1】:

我猜这是因为您在 python3 上运行,print 不再是关键字。这是一个功能。你必须调用它。

例如

print('stopped your instances: ' + str(instances))

如果你像这样使用它,它可以在两个版本的 python 上工作。

【讨论】:

    猜你喜欢
    • 2021-06-28
    • 1970-01-01
    • 2022-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 2020-10-30
    相关资源
    最近更新 更多