英文:
Accessing AWS Lambda Context inside Function using SpringBootRequestHandler?
问题
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
}
}
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.
The three questions I have are:
- In an actual AWS environment, would the idea of setting a static variable work? As in, is the flow for the AWS function truly A::handleRequest -> B::apply?
- If #1 is no, then how might I pass the context to this apply function?
- If neither 1 or 2 is possible, is there a design that is that allows context to be passed?
英文:
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
}
}
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.
The three questions I have are:
- In an actual AWS environment, would the idea of setting a static variable work? As in, is the flow for the AWS function truly A::handleRequest -> B::apply?
- If #1 is no, then how might I pass the context to this apply function?
- If neither 1 or 2 is possible, is there a design that is that allows context to be passed?
答案1
得分: 1
使用Function
类中的Lambda
Context
,您只需使用com.amazonaws.services.lambda.runtime.Context
类的Autowired
注释,例如:
@Autowired
private Context 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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论