我正在尝试使用API网关和Flask与Lambda一起设置应用程序。当我使用简单的lambda函数返回一个字符串时,一切都正常工作。
然而,当我尝试通过flask-lambda运行相同的代码时,出现了以下错误:
def lambda_handler(event, context):
return "Hello world"
然而,当我尝试通过flask-lambda运行相同的代码时,出现了以下错误:
from flask_lambda import FlaskLambda
app = FlaskLambda(__name__)
@app.route('/', methods=['GET', 'POST'])
def home():
return "Hello world"
if __name__ == '__main__':
app.run(debug=True)
以下是我通过浏览器收到的错误信息:
{"errorMessage": "'wsgi.url_scheme'", "errorType": "KeyError", "stackTrace": [" File \"/var/task/flask_lambda.py\", line 97, in __call__\n return super(FlaskLambda, self).__call__(event, context)\n", " File \"/var/task/flask/app.py\", line 2088, in __call__\n return self.wsgi_app(environ, start_response)\n", " File \"/var/task/flask/app.py\", line 2065, in wsgi_app\n ctx = self.request_context(environ)\n", " File \"/var/task/flask/app.py\", line 1982, in request_context\n return RequestContext(self, environ)\n", " File \"/var/task/flask/ctx.py\", line 309, in __init__\n self.url_adapter = app.create_url_adapter(self.request)\n", " File \"/var/task/flask/app.py\", line 1774, in create_url_adapter\n return self.url_map.bind_to_environ(\n", " File \"/var/task/werkzeug/routing.py\", line 1651, in bind_to_environ\n wsgi_server_name = get_host(environ).lower()\n", " File \"/var/task/werkzeug/wsgi.py\", line 113, in get_host\n environ[\"wsgi.url_scheme\"],\n"]}
我不确定是否需要在Lambda中添加环境变量。我已经添加了以下环境变量以进行测试,只是试图将它们应用到实际场景中:
Environment:
Variables:
TABLE_NAME: blah-table
REGION_NAME: !Ref AWS::Region
SERVER_NAME: !Sub 'xxxxxxxxx.execute-api.${AWS::Region}.amazonaws.com'
SERVER_PORT: 443
HTTP_HOST: !Sub 'xxxxxxxx.execute-api.${AWS::Region}.amazonaws.com'
WSGI_SERVER_NAME: !Sub 'xxxxxxxx.execute-api.${AWS::Region}.amazonaws.com'
非常感谢任何对正确方向的推动。
sam local invoke "MyLambdaFunction" -n env.json
。 - Abhishek