AWS Lambda C# - Accessing custom context

假装没事ソ 提交于 2019-12-01 06:47:04

So, after spending 3 days troubleshooting this and with the help of the AWS engineers, this is what I've found;

  • There is a limitation with accessing the $context, $authorizer or any other custom variables from a Lambda function, written through .Net Core in C#

A new service request is being created for the AWS team for this.

To explain:

Currently, in node.js you have access to the entire payload of data being passed through to the Lambda function (within the event parameter), which includes all custom variables (you could access it directly - for the question example, like this: event.requestContext.authorizer.customKey.

This is not the same for the C# equivalent - which uses the APIGatewayProxyRequest request object within a Lambda function. So although you have access to the entire payload (including all the custom variables) within node, within C#, you only have access to the APIGatewayProxyRequest object. Properties of which can be found here:

Or in short:

public string Body { get; set; }
public IDictionary<string, string> Headers { get; set; }
public string HttpMethod { get; set; }
public bool IsBase64Encoded { get; set; }
public string Path { get; set; }
public IDictionary<string, string> PathParameters { get; set; }
public IDictionary<string, string> QueryStringParameters { get; set; }
public ProxyRequestContext RequestContext { get; set; }
public string Resource { get; set; }
public IDictionary<string, string> StageVariables { get; set; }

Being based off an object, this will not allow access to custom or "unknown" properties, even though they are part of the payload.

Long story short, as of right now: if you wish you work with custom variables of any sort, you would either need to code it through node(event) / python, or possibly overwrite an existing property within the APIGatewayProxyRequest object.

UPDATE:

There is a work around to accessing the entire payload of the data coming in:

A work around till then is have your Lambda function take in a System.IO.Stream instead of APIGatewayProxyRequest. Then you have access to the original JSON which you can parse yourself. You can grab the information you need from that JSON and then deserialize JSON to APIGatewayProxyRequest as well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!