Accessing AWS Lambda Context inside Function using SpringBootRequestHandler?

huangapple go评论94阅读模式
英文:

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:

  1. 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?
  2. If #1 is no, then how might I pass the context to this apply function?
  3. 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&lt;S3Event, String&gt; {
    @Override
    public Object handleRequest(S3Event event, Context context) {
        return super.handleRequest(event, context);
    }
}
@Component(&quot;b&quot;)
public class B implements Function&lt;S3Event, String&gt; {

    @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:

  1. 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?
  2. If #1 is no, then how might I pass the context to this apply function?
  3. 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;

huangapple
  • 本文由 发表于 2020年8月12日 12:59:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63370049.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定