calavera / aws-lambda-events

Rust event types for AWS Lambda
MIT License
128 stars 55 forks source link

Lambda Handler fails to execute when using `ApiGatewayV2CustomAuthorizerV1Request` or `ApiGatewayV2CustomAuthorizerV2Request` #131

Open ygormartins opened 1 year ago

ygormartins commented 1 year ago

I'm trying to create a custom ApiGatewayV2 authorizer lambda for a WebSocket API using ApiGatewayV2CustomAuthorizerV1Request or ApiGatewayV2CustomAuthorizerV2Request, but the lambda fails to run in both cases. It runs the main() function with no issues, but code inside the handler_fn is being completely ignored, and no error whatsoever is being thrown. I've tried using other types, such as ApiGatewayCustomAuthorizerRequest, and they work just fine, but they're incompatible with the API Gateway version and return incorrect/missing data.

Sometimes it outputs an "Unknown.Runtime" error message directly in the lambda logs.

I'm using lambda_runtime and aws_lambda_events, both v0.7.0. I've tried v0.7.1 and v0.7.2 and had the exact same result.

Here's the code:

use aws_lambda_events::apigw::ApiGatewayV2CustomAuthorizerV2Request;
use lambda_runtime::{service_fn, Error, LambdaEvent};

#[tokio::main]
async fn main() -> Result<(), Error> {
    println!("Running main function");

    let handler = service_fn(|event| handler_fn(event));

    lambda_runtime::run(handler).await?;

    println!("Finished running");

    Ok(())
}

async fn handler_fn(
    event: LambdaEvent<ApiGatewayV2CustomAuthorizerV2Request>,
) -> Result<ApiGatewayV2CustomAuthorizerV2Request, Error> {
    println!("Query params:");
    println!("{:#?}", event.payload.query_string_parameters);

    Ok(event.payload)
}

Here are the lambda logs:

2023-01-05T20:53:34.722-03:00 | Running main function
2023-01-05T20:53:34.729-03:00 | START RequestId: e320b0aa-6596-4396-abb5-2989ebfad615 Version: $LATEST
2023-01-05T20:53:34.731-03:00 | END RequestId: e320b0aa-6596-4396-abb5-2989ebfad615
2023-01-05T20:53:34.731-03:00 | REPORT RequestId: e320b0aa-6596-4396-abb5-2989ebfad615 Duration: 1.57 ms Billed

And here's my lambda configuration:

Screenshot from 2023-01-05 20-56-48 Screenshot from 2023-01-05 20-57-14

Also, what's the difference between ApiGatewayV2CustomAuthorizerV1Request and ApiGatewayV2CustomAuthorizerV2Request?

Thanks!

calavera commented 1 year ago

This discussion might be relevant to what you're trying to do: https://github.com/awslabs/aws-lambda-rust-runtime/discussions/530

ygormartins commented 1 year ago

This discussion might be relevant to what you're trying to do: https://github.com/awslabs/aws-lambda-rust-runtime/discussions/530

This refers to AWS SAM role issues with APIGateway authorisers (had the exact same issue yesterday!)

In my case, serde was failing to serialise the event payload because some fields from both V1 and V2 types weren't present (such as http_method from V1) and weren't marked as Option<>. I believe this is happening because I'm using a custom authoriser for a WebSocket API, and some fields are different from HTTP ones.

I believe there's two ways to fix this:

For now, I have created a custom struct with only the fields I'll be using in my lambda

naokiri commented 1 year ago

Reporting another case related to this problem.

I've used ApiGatewayV2CustomAuthorizerV2Request for an HTTP APIGatewayV2, payload version 2.0, and the API didn't work and the cloudwatch log of $context.authorizer.error recorded

"The Lambda Authorizer function returned the following error: missing field cookies at line 1 column 1439. Check your Lambda function code and try again."

So this issue happens not only for WebSocket API but also when the field is not actually necessary for HTTP API case too. I think adding Option<> is a better solution in short term.

calavera commented 1 year ago

@naokiri the error with cookies should be solved in 0.7.3. I believe #123 fixed it, but it was never released until today.

naokiri commented 1 year ago

@calavera v0.7.3 worked. Thank you!