Is it feasible to create multiple Lambda functions (Java) in a jar

谁说我不能喝 提交于 2021-02-04 19:15:10

问题


I am new to AWS Lambda and I am using AWS Eclipse plugin to develop Lambda functions.

Question: Is it possible to use a single .jar for all different Lambda functions. In this single .jar file can I have classes for different Lambda functions.

Otherwise, should I create separate .jar files for each Lambda function and upload the .jars separately for each Lambda functions.


回答1:


It's possible to use one .jar, but it's not possible for lambdas to share one .jar. You will need to upload the jar file or provide the same lambda with the s3 location, the location can be the same.

Of course lambda is just a bit of code, so if you want one lambda to share one .jar, it can call different functions based on a value inside the event payload.

Below is an example how one jar could be used in multiple lambdas:

Lambda #1 handler:

example.Hello::myHandler

Lambda #2 handler:

example.Hello::mySecondHandler

example code:

package example;

import com.amazonaws.services.lambda.runtime.Context; 
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class Hello implements RequestHandler<Integer, String>{
    public String myHandler(int myCount, Context context) {
        return String.valueOf(myCount);
    }
    public String mySecondHandler(int mySum, Context context) {
        return String.valueOf(mySum);
    }
}

Below is an example how one lambda can have essentially two different events:

package example;

import com.amazonaws.services.lambda.runtime.Context; 
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class Hello implements RequestHandler<String, String>{

    public String myMainHandler(String event_type, Context context) {

      switch (event_type) {
            case "myHandler": return this.myHandler()
            case "mySecondHandler": return this.mySecondHandler()
            default: return "Good Bye";
        }
    }

    public String myHandler() {
        return "Hello";
    }
    public String mySecondHandler() {
        return "World";
    }
}


来源:https://stackoverflow.com/questions/49208841/is-it-feasible-to-create-multiple-lambda-functions-java-in-a-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!