I am doing a POC on API Gateway integration with AWS Lambda using Flask, to build a simple S3 upload function. However when I write a simple Hello World function using Flask it's failing with below error. The API gateway has proxy lambda enabled. However I am not sure why its not passing the event.
#Lambda Handler is fl.app
from flask import Flask
app = Flask(__name__)
@app.route("/resourcetest")
def hello():
return "Hello World!"
Response
Response
{
"errorMessage": "'SERVER_NAME'",
"errorType": "KeyError",
"stackTrace": [
[
"/var/task/flask/app.py",
2464,
"__call__",
"return self.wsgi_app(environ, start_response)"
],
[
"/var/task/flask/app.py",
2442,
"wsgi_app",
"ctx = self.request_context(environ)"
],
[
"/var/task/flask/app.py",
2359,
"request_context",
"return RequestContext(self, environ)"
],
[
"/var/task/flask/ctx.py",
292,
"__init__",
"self.url_adapter = app.create_url_adapter(self.request)"
],
[
"/var/task/flask/app.py",
2174,
"create_url_adapter",
"subdomain=subdomain,"
],
[
"/var/task/werkzeug/routing.py",
1594,
"bind_to_environ",
"wsgi_server_name = get_host(environ).lower()"
],
[
"/var/task/werkzeug/wsgi.py",
168,
"get_host",
"rv = environ[\"SERVER_NAME\"]"
]
]
}
Function Logs
START RequestId: 5c0f26d9-00ac-4901-b224-c56c23fdff39 Version: $LATEST
'SERVER_NAME': KeyError
Traceback (most recent call last):
File "/var/task/flask/app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "/var/task/flask/app.py", line 2442, in wsgi_app
ctx = self.request_context(environ)
File "/var/task/flask/app.py", line 2359, in request_context
return RequestContext(self, environ)
File "/var/task/flask/ctx.py", line 292, in __init__
self.url_adapter = app.create_url_adapter(self.request)
File "/var/task/flask/app.py", line 2174, in create_url_adapter
subdomain=subdomain,
File "/var/task/werkzeug/routing.py", line 1594, in bind_to_environ
wsgi_server_name = get_host(environ).lower()
File "/var/task/werkzeug/wsgi.py", line 168, in get_host
rv = environ["SERVER_NAME"]
KeyError: 'SERVER_NAME'
END RequestId: 5c0f26d9-00ac-4901-b224-c56c23fdff39
REPORT RequestId: 5c0f26d9-00ac-4901-b224-c56c23fdff39 Duration: 6.05 ms Billed Duration: 7 ms Memory Size: 128 MB Max Memory Used: 53 MB Init Duration: 292.92 ms
Request ID
5c0f26d9-00ac-4901-b224-c56c23fdff39