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);
}
}
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
Package:
$ zip function.zip *.class
Next, letโs navigate to the AWS Lambda Console.
Click on the Create function
button.
Assign a lambda name and select Java 17 as the runtime.
Click on the Upload from
button.
Click Upload
button.
Choose the function.zip
file that we generated.
Click Edit
on the Code properties
section.
Change the Handler to Function::add
and click Save
.
Go to the Test
tab.
Change the JSON payload to:
{
"x": 123,
"y": 333
}
Click the Test
button, then expand the Details
section to see the result.
Thatโs it! You have successfully created and tested your Java 17 AWS Lambda function. The output should be:
{
"result": 456
}
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)