I'm writing a MicroService and my lambda handler takes JSON in the body of the request and builds a Jinja2 template. My Lambda function is working properly and returns a status code of 200, but when I call the function through my API Gateway I get a 502 response.
def lambda_handler(event, context):
try:
file_object = s3.get_object(Bucket= 'bucket_name', Key='object_name')
file_content = file_object["Body"].read().decode('utf-8')
template = Template(file_content)
rendered_template = template.render(resume = request_body)
# Do some logic to place render in s3 and get path
response = {
'statusCode': 200,
'header': {'Content-Type': 'application/json'},
'body': 'path to file'
}
return response
except Exception as e:
print(e)
raise e
The request body I am using is similar to:
{
"username": "john-doe",
"location": "US",
.
.
.
}
The error I am receiving in response is:
{
"errorMessage": "'str object' has no attribute 'lastName'",
"errorType": "UndefinedError",
"stackTrace": [
" File \"/var/task/resume_service.py\", line 39, in lambda_handler\n raise e\n",
" File \"/var/task/resume_service.py\", line 25, in lambda_handler\n generate = template.render(resume=request_body)\n",
" File \"/var/task/jinja2/asyncsupport.py\", line 76, in render\n return original_render(self, *args, **kwargs)\n",
" File \"/var/task/jinja2/environment.py\", line 1008, in render\n return self.environment.handle_exception(exc_info, True)\n",
" File \"/var/task/jinja2/environment.py\", line 780, in handle_exception\n reraise(exc_type, exc_value, tb)\n",
" File \"/var/task/jinja2/_compat.py\", line 37, in reraise\n raise value.with_traceback(tb)\n",
" File \"<template>\", line 28, in top-level template code\n",
" File \"/var/task/jinja2/environment.py\", line 411, in getitem\n return obj[argument]\n"
]
}
How come the Lambda function succeeds when I call it by itself, but fails when I call it through the API Gateway?