使用 SpringBootRequestHandler 在函数内部访问 AWS Lambda 上下文? [英] Accessing AWS Lambda Context inside Function using SpringBootRequestHandler?
问题描述
我需要为我的 lambda 函数访问 AWS Lambda 的上下文.我正在使用 SpringBootRequestHandler
来接收请求,它应该调用一个实现 Function 接口的类.下面是两个类的代码示例:
I need access to the AWS Lambda's Context for my lambda function. I am using SpringBootRequestHandler
to receive the request, which should call a class implementing the Function interface. Below is a code sample of the two classes:
public class A extends SpringBootRequestHandler<S3Event, String> {
@Override
public Object handleRequest(S3Event event, Context context) {
return super.handleRequest(event, context);
}
}
@Component("b")
public class B implements Function<S3Event, String> {
@Override
public String apply(S3Event string) {
// this is where I need the context
// this function should be called by handleRequest
}
}
我使用spring-cloud-starter-function-web
来调用本地运行的B类中的apply方法,但是这只允许直接调用B类,而A类的handleRequest永远不会被执行.我曾尝试在 B 类中设置静态 Context 变量并在 handleRequest 开始时设置它,但鉴于 handleRequest 不会在本地执行,这不起作用.
I am using spring-cloud-starter-function-web
to call the apply method in class B running locally, but this only allows directly calling class B, while class A's handleRequest is never executed. I have tried setting a static Context variable in class B and setting it at the start of handleRequest, but given that handleRequest will not execute locally, this does not work.
我的三个问题是:
- 在实际的 AWS 环境中,设置静态变量的想法可行吗?如在,AWS 函数的流程真的是 A::handleRequest ->B::申请?
- 如果 #1 不是,那么我如何将上下文传递给这个应用函数?
- 如果 1 或 2 都不可能,是否有允许传递上下文的设计?
推荐答案
要在 Function 类中使用 Lambda
Context
您只需要使用带有 com.amazonaws.services.lambda.runtime.Context
类,例如:
To use the Lambda
Context
in the Function class you just need to use Autowired annotation with the com.amazonaws.services.lambda.runtime.Context
class, for example:
@Autowired
private Context context;
这篇关于使用 SpringBootRequestHandler 在函数内部访问 AWS Lambda 上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!