DEV Community

Carlos Chacin โ˜•๐Ÿ‘ฝ
Carlos Chacin โ˜•๐Ÿ‘ฝ

Posted on • Originally published at carloschac.in on

Java 17 + AWS Lambda

Header

AWS recently announced Java 17 support for Lambdas, In this blog post, weโ€™ll explore how to create a Java 17 AWS Lambda Function without IDE, build tool or any dependency.

Fist letโ€™s create the lambda function code:

public class Function {

  public record Input(int x, int y) {}

  public record Output(long result) {}

  public Output add(Input input) {
    return new Output(input.x + input.y);
  }
}

Enter fullscreen mode Exit fullscreen mode

In this example, we have an add method that takes an Input java record with 2 fields: x and y, and returns an Output java record with the sum of x and y assigned to the result field.

This is the only requirement for a AWS Lambda Function:

An instance method that takes one single parameter and returns something an object or Void.

Thatโ€™s it. Now we can proceed to compile and package our AWS Lambda Function:

Compile:

$ javac Function.java

Enter fullscreen mode Exit fullscreen mode

Package:

$ zip function.zip *.class

Enter fullscreen mode Exit fullscreen mode

Next, letโ€™s navigate to the AWS Lambda Console. 2

Click on the Create function button.

3

Assign a lambda name and select Java 17 as the runtime.

4

Click on the Upload from button.

5

Click Upload button.

6

Choose the function.zip file that we generated.

7

Click Edit on the Code properties section.

8

Change the Handler to Function::add and click Save.

9

Go to the Test tab.

10

Change the JSON payload to:

{
  "x": 123,
  "y": 333
}

Enter fullscreen mode Exit fullscreen mode

Click the Test button, then expand the Details section to see the result.

11

Thatโ€™s it! You have successfully created and tested your Java 17 AWS Lambda function. The output should be:

{
  "result": 456
}

Enter fullscreen mode Exit fullscreen mode

12

This demonstrates the simplicity and power of using Java 17 with AWS Lambda. With just a few steps, you can leverage the latest Java version to build efficient and scalable serverless applications.

Feel free to explore further and unleash the full potential of Java 17 on AWS Lambda. Happy coding!

Top comments (0)