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.

Master AWS Lambda with Java 17 in Just 2 Minutes! - YouTube

In this video, we'll show you how to set up AWS Lambda with Java 17 in just 2 minutes!Don't forget to like, comment, and subscribe for more content like this...

favicon youtube.com

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)