调用Lambda Function URL报错:请求不符合API Gateway/ALB格式
解决Lambda Function URL调用时的InvalidRequestEventException错误
问题根源
你遇到的InvalidRequestEventException错误,本质是Lambda函数的代码只支持解析API Gateway v1或ALB的代理事件格式,但Function URL发送的是API Gateway v2标准的事件格式,两者结构不同,导致代码无法识别请求事件。
解决方案
1. 调整Lambda代码以支持API Gateway v2事件格式
根据你使用的开发方式,分两种情况处理:
情况一:自定义Lambda handler代码
如果是自己编写的handler,需要将输入事件类型从APIGatewayProxyRequestEvent(v1)替换为APIGatewayV2ProxyRequestEvent(v2),同时输出事件也对应换成APIGatewayV2ProxyResponseEvent。
示例Java代码:
import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.RequestHandler; import com.amazonaws.services.lambda.runtime.events.APIGatewayV2ProxyRequestEvent; import com.amazonaws.services.lambda.runtime.events.APIGatewayV2ProxyResponseEvent; import java.util.HashMap; import java.util.Map; public class MyApiHandler implements RequestHandler<APIGatewayV2ProxyRequestEvent, APIGatewayV2ProxyResponseEvent> { @Override public APIGatewayV2ProxyResponseEvent handleRequest(APIGatewayV2ProxyRequestEvent input, Context context) { APIGatewayV2ProxyResponseEvent response = new APIGatewayV2ProxyResponseEvent(); response.setStatusCode(200); // 设置响应内容和CORS头 response.setBody("Success: Received GET request via Function URL"); Map<String, String> headers = new HashMap<>(); headers.put("Access-Control-Allow-Origin", "*"); response.setHeaders(headers); return response; } }
情况二:使用Serverless框架(如Spring Boot Adapter)
如果用了AWS Serverless Java Container这类框架,需要初始化时指定支持API Gateway v2事件:
import com.amazonaws.serverless.proxy.spring.SpringBootLambdaContainerHandler; import com.amazonaws.serverless.proxy.model.AwsProxyRequest; import com.amazonaws.serverless.proxy.model.AwsProxyResponse; import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.RequestStreamHandler; import com.amazonaws.serverless.proxy.exceptions.ContainerInitializationException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class StreamLambdaHandler implements RequestStreamHandler { private static SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler; static { try { // 显式初始化支持API Gateway v2的handler handler = SpringBootLambdaContainerHandler.getHandler( YourSpringBootApplication.class, com.amazonaws.services.lambda.runtime.events.APIGatewayV2ProxyRequestEvent.class, com.amazonaws.services.lambda.runtime.events.APIGatewayV2ProxyResponseEvent.class ); } catch (ContainerInitializationException e) { throw new RuntimeException("Failed to initialize Spring Boot container", e); } } @Override public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException { handler.proxyStream(inputStream, outputStream, context); } }
2. 确认Function URL配置(你的现有配置已符合要求)
你当前的配置:
- 认证类型设为
NONE - CORS允许
* - 权限策略正确(允许匿名调用且绑定了
lambda:FunctionUrlAuthType: NONE条件)
这些都是正确的,无需调整。
3. Postman/浏览器调用方式
直接使用Function URL的完整地址(例如https://abc123.lambda-url.us-east-1.on.aws/)发起GET请求即可:
- 无需添加SigV4签名(因为Auth type为NONE)
- 不需要特殊请求头,GET请求可以直接发送;若需要传递参数,直接拼在URL后(如
https://abc123.lambda-url.us-east-1.on.aws/?name=test)
验证方法
修改代码后重新部署Lambda函数,再用Postman发起请求:
- 若返回200状态码和你设置的响应内容,说明问题解决
- 若仍报错,检查代码中是否还有其他依赖API Gateway v1事件的逻辑(比如读取
input.getPathParameters()这类方法,v2虽有同名方法,但事件结构细节有差异,需对应调整)
内容的提问来源于stack exchange,提问作者AylaWinters
相关产品推荐
相关产品推荐

