You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

本地Docker部署Rust版AWS Lambda对接Kong Gateway遇502错误求助

问题

  • 本地Docker环境运行Rust开发的AWS Lambda,通过Kong Gateway访问时返回502错误
  • 前置操作:
    1. 基于amazon/aws-lambda-provided:latest镜像构建Rust Lambda函数,x86_64架构编译,本地Docker单独运行正常
    2. 按教程配置Kong模拟AWS Gateway,但调用Kong接口时触发错误

错误响应

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

Lambda追踪日志

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) } }

问题根源

Kong的AWS Lambda插件默认传递自定义格式的请求(包含request_uri_args/request_uri等字段),但你使用的Rust Lambda依赖官方LambdaEvent<T>类型,该类型仅支持AWS API Gateway、ELB或Lambda Function URL的标准事件结构,无法识别Kong的自定义格式。Kong插件不会自动转换请求格式,需手动配置开启转换。


解决方案

1. 启用Kong Lambda插件的AWS请求格式转换

配置插件的aws_request参数为true,让Kong将请求转换为API Gateway V1的标准事件格式,匹配Rust Lambda的LambdaEvent要求:

# 全局配置Lambda插件
kong plugin configure lambda --config aws_request=true

# 或针对特定路由/服务配置
kong route create --name lambda-route --paths /lambda --service lambda-service --plugins lambda:aws_request=true

2. 验证Lambda事件格式

配置完成后,Lambda收到的事件应符合以下API Gateway V1结构:

{
  "resource": "/",
  "path": "/",
  "httpMethod": "POST",
  "headers": { "Content-Type": "application/json", ... },
  "queryStringParameters": {},
  "body": "...",
  "isBase64Encoded": false
}

3. 确保Lambda响应符合要求

Rust Lambda返回的响应必须包含statusCode字段,否则Kong会因无法解析响应返回502:

use lambda_runtime::{Error, LambdaEvent};
use serde_json::{json, Value};

async fn handler(event: LambdaEvent<Value>) -> Result<Value, Error> {
    Ok(json!({
        "statusCode": 200,
        "headers": {"Content-Type": "application/json"},
        "body": json!({"message": "Success"}).to_string()
    }))
}

内容的提问来源于stack exchange,提问作者shpitc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 06:26:01