Trying to integrate Flask with AWS Lambda through SAM. However, I am getting 404 Error when I hit the API.
import awsgi
from flask import (
Flask,
jsonify,
)
app = Flask(__name__)
@app.route('/')
def index():
return {
"statusCode": 200,
"body": json.dumps({
"message": "hello world",
# "location": ip.text.replace("\n", "")
}),
}
def lambda_handler(event, context):
return awsgi.response(app, event, context)
Invoking Lambda Function HelloWorldFunction
START RequestId: e0d920ad-cc19-4292-bf69-a1d148bfc98e Version: $LATEST
[ERROR] KeyError: 'httpMethod'
Traceback (most recent call last):
File "/var/task/app.py", line 21, in lambda_handler
return awsgi.response(app, event, context)
File "/var/task/awsgi/__init__.py", line 172, in response
output = app(environ(event, context), sr)
File "/var/task/awsgi/__init__.py", line 121, in environ
'REQUEST_METHOD': event['httpMethod'],END RequestId: e0d920ad-cc19-4292-bf69-a1d148bfc98e
REPORT RequestId: e0d920ad-cc19-4292-bf69-a1d148bfc98e Duration: 12.56 ms Billed Duration: 13 ms Memory Size: 128 MB Max Memory Used: 63 MB Init Duration: 557.31 ms
{"errorMessage": "'httpMethod'", "errorType": "KeyError", "requestId": "e0d920ad-cc19-4292-bf69-a1d148bfc98e", "stackTrace": [" File \"/var/task/app.py\", line 21, in lambda_handler\n return awsgi.response(app, event, context)\n", " File \"/var/task/awsgi/__init__.py\", line 172, in response\n output = app(environ(event, context), sr)\n", " File \"/var/task/awsgi/__init__.py\", line 121, in environ\n 'REQUEST_METHOD': event['httpMethod'],\n"]}
I feel it is something simple I am missing.
If I replace lambda_handler
with the tutorial code from AWS I get expected output.
This below is that code. So my AWS Lambda Proxy Resource is configured correctly for simple JSON but not for awsgi
.
def lambda_handler(event, context):
return {
"statusCode": 200,
"body": json.dumps({
"message": "hello world",
# "location": ip.text.replace("\n", "")
}),
}
What am I doing wrong? Can anyone help? I have been trying for a week now.