每当我部署 Lambda 函数并尝试测试它时,它都会失败并生成以下错误
此 Lambda 函数将请求正文中的 JSON 对象插入到 Postgres 表中。
def lambda_handler(event, context):
data = json.loads(event["body"])
initialId = data["initials_id"]
ageId = data["age_id"]
roleId = data["role_id"]
gradeId = data["grade_id"]
symptomaticId = data["symptomatic_id"]
maskedId = data["masked_id"]
testedId = data["tested_id"]
testDateId = data["test_date_id"]
studentTotalContacts = data["student_total_contacts"]
studentUnmaskedContacts = data["student_unmasked_contacts"]
studentsTested = data["students_tested"]
studentsPositive = data["students_positive"]
studentsPositiveAfterUmContact = data["students_positive_after_um_contact"]
teacherContacts = data["teacher_contacts"]
teacherUnmaskedContacts = data["teacher_unmasked_contacts"]
teacherTested = data["teachers_tested"]
teachersPositive = data["teachers_positive"]
teachersPositveAfterUmContact = data["teachers_positive_after_um_contact"]
query_sql=f"""
INSERT INTO wustl.contact_tracker_submissions (initials_id, age_id, role_id, grade_id, symptomatic_id, masked_id,
tested_id, test_date_id, student_total_contacts, student_unmasked_contacts,
students_tested, students_positive, students_positive_after_um_contact,
teacher_contacts, teacher_unmasked_contacts, teachers_tested, teachers_positive,
teachers_positive_after_um_contact )
VALUES({initialId}, {ageId}, {roleId}, {gradeId}, {symptomaticId}, {maskedId}, {testedId}, {testDateId}, {studentTotalContacts},
{studentUnmaskedContacts}, {studentsTested}, {studentsPositive}, {studentsPositiveAfterUmContact}, {teacherContacts},
{teacherUnmaskedContacts}, {teacherTested}, {teachersPositive}, {teachersPositveAfterUmContact});
"""
# execute the sql query and get the response
try:
response = execute_statement(query_sql)
results = 'Successfully submitted'
status_code = 200
except Exception as e:
print(e)
results = 'Failed'
status_code = 500
return {
"isBase64Encoded": False,
'statusCode': status_code,
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
'body': json.dumps({"message" : results})
}
第 30 行 --> initialId = data["initials_id"]
这是事件有效负载:
{"initials_id":"BS","age_id":23,"role_id":"Student","grade_id":10,"symptomatic_id":true,"masked_id":true,"tested_id":true,"test_date_id":"2022-02-10T17:25:00.000Z","student_total_contacts":1,"student_unmasked_contacts":2,"students_tested":3,"students_positive":4,"students_postive_after_um_contact":5,"teacher_contacts":6,"teacher_unmasked_contacts":7,"teachers_tested":8,"teachers_positive":9,"teachers_positive_after_um_contact":10}
这是它生成的错误日志
Endpoint response body before transformations: {"errorMessage": "'initials_id'", "errorType": "KeyError", "stackTrace": [" File \"/var/task/contactTracker.py\", line 30, in lambda_handler\n initialId = event[\"initials_id\"]\n"]}
Wed Mar 02 19:44:04 UTC 2022 : Lambda execution failed with status 200 due to customer function error: 'initials_id'. Lambda request id: cd2146ab-b914-4048-bd4b-b863946d9d20
Wed Mar 02 19:44:04 UTC 2022 : Method completed with status: 502
谢谢!
This Lambda function inserts a JSON object from request body into Postgres table.
def lambda_handler(event, context):
data = json.loads(event["body"])
initialId = data["initials_id"]
ageId = data["age_id"]
roleId = data["role_id"]
gradeId = data["grade_id"]
symptomaticId = data["symptomatic_id"]
maskedId = data["masked_id"]
testedId = data["tested_id"]
testDateId = data["test_date_id"]
studentTotalContacts = data["student_total_contacts"]
studentUnmaskedContacts = data["student_unmasked_contacts"]
studentsTested = data["students_tested"]
studentsPositive = data["students_positive"]
studentsPositiveAfterUmContact = data["students_positive_after_um_contact"]
teacherContacts = data["teacher_contacts"]
teacherUnmaskedContacts = data["teacher_unmasked_contacts"]
teacherTested = data["teachers_tested"]
teachersPositive = data["teachers_positive"]
teachersPositveAfterUmContact = data["teachers_positive_after_um_contact"]
query_sql=f"""
INSERT INTO wustl.contact_tracker_submissions (initials_id, age_id, role_id, grade_id, symptomatic_id, masked_id,
tested_id, test_date_id, student_total_contacts, student_unmasked_contacts,
students_tested, students_positive, students_positive_after_um_contact,
teacher_contacts, teacher_unmasked_contacts, teachers_tested, teachers_positive,
teachers_positive_after_um_contact )
VALUES({initialId}, {ageId}, {roleId}, {gradeId}, {symptomaticId}, {maskedId}, {testedId}, {testDateId}, {studentTotalContacts},
{studentUnmaskedContacts}, {studentsTested}, {studentsPositive}, {studentsPositiveAfterUmContact}, {teacherContacts},
{teacherUnmaskedContacts}, {teacherTested}, {teachersPositive}, {teachersPositveAfterUmContact});
"""
# execute the sql query and get the response
try:
response = execute_statement(query_sql)
results = 'Successfully submitted'
status_code = 200
except Exception as e:
print(e)
results = 'Failed'
status_code = 500
return {
"isBase64Encoded": False,
'statusCode': status_code,
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
'body': json.dumps({"message" : results})
}
line 30 --> initialId = data["initials_id"]
Here is the event payload:
{"initials_id":"BS","age_id":23,"role_id":"Student","grade_id":10,"symptomatic_id":true,"masked_id":true,"tested_id":true,"test_date_id":"2022-02-10T17:25:00.000Z","student_total_contacts":1,"student_unmasked_contacts":2,"students_tested":3,"students_positive":4,"students_postive_after_um_contact":5,"teacher_contacts":6,"teacher_unmasked_contacts":7,"teachers_tested":8,"teachers_positive":9,"teachers_positive_after_um_contact":10}
Here is the error log it generates
Endpoint response body before transformations: {"errorMessage": "'initials_id'", "errorType": "KeyError", "stackTrace": [" File \"/var/task/contactTracker.py\", line 30, in lambda_handler\n initialId = event[\"initials_id\"]\n"]}
Wed Mar 02 19:44:04 UTC 2022 : Lambda execution failed with status 200 due to customer function error: 'initials_id'. Lambda request id: cd2146ab-b914-4048-bd4b-b863946d9d20
Wed Mar 02 19:44:04 UTC 2022 : Method completed with status: 502
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不打印
data
的值并确认数据中存在initials_id
Why don't you print the value of
data
and confirminitials_id
exist in the data