Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda C# - Accessing custom context

I have a simple Lambda function written in .NET Core (C#) that uses the APIGatewayProxyRequest object to go through all the request properties.

If I test this lambda function (from AWS Lambda), and pass it a sample event config that contains basic information:

enter image description here

I can get this information like so:

public string FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context)
        logger.Logger.Log($"Body: {request.Body}  \n");
        logger.Logger.Log($"Path: {request.Path}  \n");
        logger.Logger.Log($"Resource: {request.Resource}  \n");

How is it that I can access custom context or authorizer values from the same data:

enter image description here

I have tried:

logger.Logger.Log($"RequestContext Authorizor: {request.RequestContext.Authorizer}  \n");

Including it's different properties (StringKey, PrincipleId etc.)

It seems from Node.js, this would be simply achieved by using this:

event.requestContext.authorizer.customKey

There is no such thing in C#?

like image 260
Hexie Avatar asked May 15 '17 23:05

Hexie


1 Answers

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.

like image 164
Hexie Avatar answered Sep 19 '22 15:09

Hexie