"errorMessage": "lambda_handler() 接受 0 个位置参数,但给出了 2 个",

0 python amazon-web-services pymysql aws-lambda

此代码在我的机器上运行正常,但在 AWS Lambda 中出现错误:

import pymysql

dbhost = 'database.ap-south-1.rds.amazonaws.com'
dbuser = 'admin'
dbpass = 'admin123'
dbname = 'classicmodels'
connection = pymysql.connect(host=dbhost, user=dbuser, password=dbpass, database=dbname)

def lambda_handler():
  cursor = connection.cursor()
  cursor.execute('SELECT * FROM Persons')
  rows = cursor.fetchall()
  for row in rows:
    print ("{0} {1} {2}".format(row[0], row[1], row[2]))

lambda_handler()
Run Code Online (Sandbox Code Playgroud)
{
"errorMessage": "lambda_handler() takes 0 positional arguments but 2 were given",
"errorType": "TypeError",
"requestId": "fb790715-91f1-4f7a-961f-b83485d23b68",
"stackTrace": ["  File \"/var/runtime/awslambdaric/bootstrap.py\", line 149, in handle_event_request\n    response = request_handler(event, lambda_context)\n"
]
}

Function Logs
START RequestId: fb790715-91f1-4f7a-961f-b83485d23b68 Version: $LATEST
[ERROR] TypeError: lambda_handler() takes 0 positional arguments but 2 were given
Traceback (most recent call last):
File "/var/runtime/awslambdaric/bootstrap.py", line 149, in handle_event_request
response = request_handler(event, lambda_context)END RequestId: fb790715-91f1-4f7a-961f-b83485d23b68
REPORT RequestId: fb790715-91f1-4f7a-961f-b83485d23b68  Duration: 1.07 ms   Billed Duration: 2 ms   Memory Size: 128 MB Max Memory Used: 44 MB
Run Code Online (Sandbox Code Playgroud)

Joh*_*ein 6

不要调用处理程序:

lambda_handler()
Run Code Online (Sandbox Code Playgroud)

调用该函数时,AWS Lambda 服务将调用处理程序。

此外,Lambda 函数应定义为:

def lambda_handler(event, context):
Run Code Online (Sandbox Code Playgroud)

event包含有关触发事件的信息,并将context包含有关 Lambda 部署环境的信息。