Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 566
Robin Varghese Avatar asked Mar 10 '18 12:03

Robin Varghese


People also ask

Can you have multiple Lambda functions?

Serverless applications usually consist of multiple Lambda functions. Each Lambda function can use only one runtime but you can use multiple runtimes across multiple functions. This enables you to choose the best runtime for the task of the function.

How many Lambda functions can I create?

Each region in your AWS account has a Lambda concurrency limit. The limit applies to all functions in the same region and is set to 1000 by default.

How many Lambda functions can run at once?

The default concurrency limit per AWS Region is 1,000 invocations at any given time. The default burst concurrency quota per Region is between 500 and 3,000, which varies per Region. There is no maximum concurrency limit for Lambda functions.

Can Lambda run jar file?

Lambda supports two types of deployment packages: container images and . zip file archives. This page describes how to create your deployment package as a . zip file or Jar file, and then use the deployment package to deploy your function code to AWS Lambda using the AWS Command Line Interface (AWS CLI).


1 Answers

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";
    }
}
like image 57
Diego Goding Avatar answered Sep 23 '22 20:09

Diego Goding