awsgi
awsgi copied to clipboard
Is there a way to access event and context inside the flask route
@app.route(BASE_ROUTE, methods=['GET'])
def hello_world():
# event=request.environ['event']
print('--hello--')
# print(event)
return jsonify(message="hello world")
def handler(event, context):
print('---event--')
print(event)
print('---context--')
print(context)
return awsgi.response(app, event, context)
Above is my code structure, I was wondering how can I access the event and context inside the route. I have a custom authenticated API gateway settings which returns a token inside the event, I want to ask that token across the flask application
Same Question, is there any way to get the context and event object please.
if it is a just token string, you can inject it into HTTP headers
def handler(event, context):
print('---event--')
print(event)
print('---context--')
print(context)
event.get("headers")["token"] = token_from_lambda_event
return awsgi.response(app, event, context)
or you can get the whole event object
@app.before_request
def before_request() -> None:
event = globals.request.environ.get("awsgi.event", {})
from flask import Flask, jsonify, request
@app.route("/wfr", methods=["POST"])
def wfr():
event = request.environ.get("awsgi.event", {})
context = request.environ.get("awsgi.context", {})
return {
"statusCode": 200,
"body": json.dumps(
{
"message": "wfr invoked",
}),
"isBase64Encoded": False,
}
👀 Debugger Variables
event=
{'body': '{"name":"phonny", "args":{"startDate": "01/01/2021", "endDate": "01/31/2021"}}', 'resource': '/{proxy+}', 'path': '/wfr', 'httpMethod': 'POST', 'isBase64Encoded': False, 'queryStringParameters': {'foo': 'bar'}, 'pathParameters': {'proxy': 'wfr'}, 'stageVariables': {'baz': 'qux'}, 'headers': {'Accept': 'application/json,text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Accept-Encoding': 'gzip, deflate, sdch', 'Accept-Language': 'en-US,en;q=0.8', 'Cache-Control': 'max-age=0', 'CloudFront-Forwarded-Proto': 'https', 'CloudFront-Is-Desktop-Viewer': 'true', 'CloudFront-Is-Mobile-Viewer': 'false', 'CloudFront-Is-SmartTV-Viewer': 'false', 'CloudFront-Is-Tablet-Viewer': 'false', 'CloudFront-Viewer-Country': 'US', 'Host': '1234567890.execute-api.us-east-1.amazonaws.com', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Custom User Agent String', 'Via': '1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)', 'X-Amz-Cf-Id': 'cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==', 'X-Forwarded-For': '127.0.0.1, 127.0.0.2', 'X-Forwarded-Port': '80', 'X-Forwarded-Proto': 'http'}, 'requestContext': {'accountId': '123456789012', 'resourceId': '123456', 'stage': 'Dev', 'requestId': 'c6af9ac6-7b61-11e6-9a41-93e8deadbeef', 'requestTime': '09/Apr/2015:12:34:56 +0000', 'requestTimeEpoch': 1428582896000, 'identity': {'cognitoIdentityPoolId': None, 'accountId': None, 'cognitoIdentityId': None, 'caller': None, 'accessKey': None, 'sourceIp': '127.0.0.1', 'cognitoAuthenticationType': None, 'cognitoAuthenticationProvider': None, 'userArn': None, 'userAgent': 'Custom User Agent String', 'user': None}, 'path': '/Dev', 'resourcePath': '/{proxy+}', 'httpMethod': 'GET', 'apiId': '1234567890', 'protocol': 'HTTP/1.1'}}
context =
LambdaContext([aws_request_id=06c948a0-31ed-49c7-9db7-8d25c2ca89f1,log_group_name=aws/lambda/NSMBenefitsProxyServer,log_stream_name=$LATEST,function_name=NSMBenefitsProxyServer,memory_limit_in_mb=2000,function_version=$LATEST,invoked_function_arn=arn:aws:lambda:us-east-1:012345678912:function:NSMBenefitsProxyServer,client_context=None,identity=CognitoIdentity([cognito_identity_id=None,cognito_identity_pool_id=None])])
Hope this helps someone.