我是 AWS 新手。我的要求是按预定的时间间隔触发 lambda 函数(spring-boot)以保持温暖。我能够触发 lambda,但它给出错误 “com.amazonaws.serverless.exceptions.InvalidRequestEventException:传入事件不是来自 Amazon API Gateway 或应用程序负载均衡器的有效请求”。 我的 lambda 公开了一些 Http 端点,这些端点通过邮递员和 api 网关工作正常。我应该在 lambda 中进行哪些更改,使其保持温暖并无缝处理计划事件,例如仅打印 Lambda Triggered 而不是此错误?
LambdaHandler.java
public class LambdaHandler implements RequestStreamHandler {
private static SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
static {
try {
handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(Application.class);
// If you are using HTTP APIs with the version 2.0 of the proxy model, use the getHttpApiV2ProxyHandler
// method: handler = SpringBootLambdaContainerHandler.getHttpApiV2ProxyHandler(Application.class);
} catch (ContainerInitializationException e) {
// if we fail here. We re-throw the exception to force another cold start
e.printStackTrace();
throw new RuntimeException("Could not initialize Spring Boot application", e);
}
}
@Override
public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException {
handler.proxyStream(input, output, context);
}
}
SAM 中的EventBridgeRule
MyEventBridgeRule:
Type: AWS::Events::Rule
Properties:
EventPattern:
source:
- "aws.events"
ScheduleExpression: "cron(0/5 * * * ? *)" # Example schedule expression
State: ENABLED
Targets:
- Arn: !GetAtt ProductInfoSamFunction.Arn
Id: "MyLambdaBridgeTarget"
您使用的事件类型与预期不同。 EventBridge 调用 Lambda 异步,因此它不是 APIGW/代理类型事件。这是一个包含一些选项的线程:https://stackoverflow.com/a/77481297/9172713