Skip to content

Instantly share code, notes, and snippets.

@lobster1234
Last active July 6, 2023 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lobster1234/f93ab36e051372e3ea6193b0a4e59840 to your computer and use it in GitHub Desktop.
Save lobster1234/f93ab36e051372e3ea6193b0a4e59840 to your computer and use it in GitHub Desktop.
Serverless Framework Java : Multiple Functions in same class

Specify packaging as individual in serverless.yml

service: aws-java-maven # NOTE: update this with your service name

provider:
  name: aws
  runtime: java8

package:
  individually: true
  

Add another handler in the same class

package com.serverless;

import java.util.Collections;
import java.util.Map;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

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

public class Handler implements RequestHandler<Map<String, Object>, ApiGatewayResponse> {

	private static final Logger LOG = Logger.getLogger(Handler.class);

	@Override
	public ApiGatewayResponse handleRequest(Map<String, Object> input, Context context) {
		BasicConfigurator.configure();

		LOG.info("received: " + input);
		Response responseBody = new Response("Go Serverless v1.x! Your function executed successfully! - function 1", input);
		return ApiGatewayResponse.builder()
				.setStatusCode(200)
				.setObjectBody(responseBody)
				.setHeaders(Collections.singletonMap("X-Powered-By", "AWS Lambda & serverless"))
				.build();
	}

	public ApiGatewayResponse handleRequest2(Map<String, Object> input, Context context) {
		Response responseBody = new Response("Go Serverless v1.x! Your function executed successfully! - function 2", input);
		return ApiGatewayResponse.builder()
				.setStatusCode(200)
				.setObjectBody(responseBody)
				.setHeaders(Collections.singletonMap("X-Powered-By", "AWS Lambda & serverless"))
				.build();
	}
}

Specify artifacts with the functions, individually

functions:
  hello:
    handler: com.serverless.Handler::handleRequest
    package:
      artifact: target/hello-dev.jar
  hello2:
    handler: com.serverless.Handler::handleRequest2
    package:
      artifact: target/hello-dev.jar

Deploy and invoke

This does not work with invoke local as of this writing (1.25.0).

bash-3.2$ mvn clean install

bash-3.2$ sls deploy
Service Information
service: aws-java-maven
stage: dev
region: us-east-1
stack: aws-java-maven-dev
api keys:
  None
endpoints:
  None
functions:
  hello: aws-java-maven-dev-hello
  hello2: aws-java-maven-dev-hello2
 
bash-3.2$ sls invoke -f hello
{
    "statusCode": 200,
    "body": "{\"message\":\"Go Serverless v1.x! Your function executed successfully! - function 1\",\"input\":{}}",
    "headers": {
        "X-Powered-By": "AWS Lambda & serverless"
    },
    "isBase64Encoded": false
}
bash-3.2$ sls invoke -f hello2
{
    "statusCode": 200,
    "body": "{\"message\":\"Go Serverless v1.x! Your function executed successfully! - function 2\",\"input\":{}}",
    "headers": {
        "X-Powered-By": "AWS Lambda & serverless"
    },
    "isBase64Encoded": false
}
@Youssef-Beltagy
Copy link

What if I want the two functions to have different parameters/return type? Is there anything which prevents that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment