Search code examples
dockerrustaws-lambdakong

AWS Lambda Rust with Kong Gateway Run locally in docker


I'm trying to run locally in docker AWS Lambda implemented with Rust with a Kong Gateway.

  1. Created Lambda function and executed with docker as per next article: Rust AWS Lambda in Docker

Works as expected. Just replace the docker base image with an up to date one FROM amazon/aws-lambda-provided:latest

and used rust build for the x86_64, which is the default one.

  1. Configured Kong to behave as AWS Gateway as per next article: Kong with AWS Lambda

When invoking Kong getting the next response(502):

{
    "error": "could not JSON decode Lambda function response: statusCode validation failed",
    "message": "Bad Gateway"
}

When tracing lambda I see:

2024-07-29 11:40:59 TRACE Lambda runtime invoke{requestId="2c4b0ff5-d53e-4d03-8149-579fe4ad7e0a"}: raw JSON event received from Lambda body="{\"request_uri_args\":{},\"request_uri\":\"/\",\"request_body\":\"\\r\\n\",\"request_headers\":{\"content-length\":\"2\",\"postman-token\":\"045224b8-59f7-432c-890b-0268c3230f57\",\"connection\":\"keep-alive\",\"accept-encoding\":\"gzip, deflate, br\",\"accept\":\"*/*\",\"user-agent\":\"PostmanRuntime/7.37.3\",\"host\":\"127.0.0.1:8000\",\"content-type\":\"application/json\"},\"request_method\":\"POST\"}" 2024-07-29 11:40:59 ERROR Lambda runtime invoke{requestId="2c4b0ff5-d53e-4d03-8149-579fe4ad7e0a"}: Request payload deserialization into LambdaEvent<T> failed. The handler will not be called. Log at TRACE level to see the payload. error=DeserializeError { inner: Error { path: Path { segments: [] }, original: Error("this function expects a JSON payload from Amazon API Gateway, Amazon Elastic Load Balancer, or AWS Lambda Function URLs, but the data doesn't match any of those services' events", line: 0, column: 0) } }

I see that the request structure and keys names are different from expected by lambda.

Shouldn't Kong transform it to acceptable?

Please help me to understand what I'm doing wrong.


Solution

  • Your lambda trace suggests that it expects a fixed-shape payload matching one of three event shapes it understands. Kong's default event is not one of them. According to the documentation, you can set a awsgateway_compatible totrue in the lambda plugin configuration - with this option Kong will try to match API Gateway payload shape.

    awsgateway_compatible

    boolean, default: false

    An optional value that defines whether the plugin should wrap requests into the Amazon API gateway.