AWS Lambda C # - Accessing Custom Context
I have a simple Lambda function written in .NET Core (C #) that uses an object APIGatewayProxyRequest
to traverse all the properties of a request.
If I test this lambda function (from AWS Lambda) and pass it a random event config containing basic information:
I can get this information like this:
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 can I access custom context or authorizer values ββfrom the same data:
I tried:
logger.Logger.Log($"RequestContext Authorizor: {request.RequestContext.Authorizer} \n");
Including its various properties ( StringKey
, PrincipleId
etc.)
It looks like from Node.js it would simply be achieved with this:
event.requestContext.authorizer.customKey
Isn't there such a thing in C #?
source to share
So after spending 3 days troubleshooting and with the help of AWS engineers, this is what I found;
- There is a restriction on access to
$context
,$authorizer
or any othercustom variables
of a Lambda function written via .Net Core inC#
To do this, the AWS team creates a new service request.
Explain:
Currently, node.js, you have access to the entire data payload transmitted Lambda functions (option event
), which includes all user-defined variables (you can access it directly - for example, Example: event.requestContext.authorizer.customKey
.
This is not the same for the C # equivalent that uses the APIGatewayProxyRequest request object in a lambda function. Therefore, while you have access to the entire payload (including all user variables) inside a node, in C # you only have access to the APIGatewayProxyRequest object. The properties of which can be found here :
Or in one word:
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; }
Based on the object, this will prevent custom or "unknown" properties from being accessed, even if they are part of the payload.
Long story short, for now: if you want you to work with custom variables of any type, you either need to program it through node (event) / python, or perhaps rewrite the existing property in <object href = "https: // github.com/aws/aws-lambda-dotnet/blob/master/Libraries/src/Amazon.Lambda.APIGatewayEvents/APIGatewayProxyRequest.cs "rel =" noreferrer "> APIGatewayProxyRequest.
UPDATE:
There is work to be done on accessing all of the data payload going into:
Work as long as your Lambda function takes in System.IO.Stream instead of APIGatewayProxyRequest. Then you have access to the original JSON, which you can parse yourself. You can get the information you need from this JSON and then deserialize the JSON into APIGatewayProxyRequest.
source to share