Lambda Auth Function Error C# - Please Help

0

Hi everyone,

I've created an Lambda Auth Function in C# to for basic authentication for REST APIs. However, once deployed and configured I'm receiving this error when doing a quick test in the AWS console:

Authorizer result body before parsing: {"statusCode":200,"headers":{"Content-Type":"application/json"},"body":"{"principalId":"User","policyDocument":{"Version":"5/22/2024 12:56:02 PM +00:00","Statement":[{"Effect":"Allow","Action":["execute-api:Invoke"],"Resource":["ResourceHere"]}]}}","isBase64Encoded":false} : Execution failed due to configuration error: Invalid JSON in response: Unrecognized field "statusCode", not marked as ignorable : AuthorizerConfigurationException

Below is a quick example of the function:

       public async Task<APIGatewayCustomAuthorizerResponse> MyFunction(APIGatewayProxyRequest request, ILambdaContext context)
       {
           request.Headers.TryGetValue("Authorization", out var credentials);
           
           // Do stuff

            return new APIGatewayCustomAuthorizerResponse
            {
                PrincipalID = "enterHere",
                PolicyDocument = new APIGatewayCustomAuthorizerPolicy
                {
                    Version = "VersionNumber",
                    Statement = new List<APIGatewayCustomAuthorizerPolicy.IAMPolicyStatement>
                    {
                        new() {
                            Action = actions,
                            Effect = effect,
                            Resource = resources
                        }
                    }
                }
            };

       }

From the error message, am I returning the correct output model or is there some configuration I haven't found?

The function tested fine using the Mock Lambda Tool.

Many thanks,

Dev-FP.

  • Just an update, I manage to resolve this by simple making changes so the function doesn't need to be async.

    Updated function is in the post should anyone else come across this issue.

    Thanks for anyone who was taking a look.

Dev-FP
asked 24 days ago160 views
1 Answer
1
Accepted Answer

Just an update, I manage to resolve this by making a simple change to remove the async function.

Updated function is below should anyone else come across this issue.

public APIGatewayCustomAuthorizerResponse MyFunction(APIGatewayProxyRequest request, ILambdaContext context)
       {
           request.Headers.TryGetValue("Authorization", out var credentials);
           
           // Do stuff

            return new APIGatewayCustomAuthorizerResponse
            {
                PrincipalID = "enterHere",
                PolicyDocument = new APIGatewayCustomAuthorizerPolicy
                {
                    Statement = new List<APIGatewayCustomAuthorizerPolicy.IAMPolicyStatement>
                    {
                        new() {
                            Action = actions,
                            Effect = effect,
                            Resource = resources
                        }
                    }
                }
            };

       }

Thanks for anyone who was taking a look.

Dev-FP
answered 24 days ago
profile picture
EXPERT
reviewed 24 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions