Where we demonstrate creating a Lambda function written in Java on the Java 21 runtime.
A Windows 11 workstation was used for the process below.
IMPORTANT a free account registration is required to download the Oracle JDK
- Java Development Kit (JDK) - login required
- Java Development Kit (JDK) version 21
- https://www.oracle.com/java/technologies/downloads/
- Download the Windows x64 installer
- Run the installer (Defaults are fine)
- C:\Program Files\Java\jdk-21\
- Environment variables
- Search for "Edit the system environment variables" in the Windows search bar
- Click Environment Variables in the System Properties window
- Under System variables, click New...
- Variable name: JAVA_HOME
- Variable value: set to the JDK installation directory (e.g., C:\Program Files\Java\jdk-21).
- Click OK and then click OK
- Verify from command line
java -versionjavac -version
- Install Maven
- https://maven.apache.org/download.cgi
- Select and download "Binary zip archive" (for Windows)
- Unzip the file to your desired installation directory (e.g.,
C:\Program Files\Apache\maven) - Environment variables
- Search for "Edit the system environment variables" in the Windows search bar
- Click Environment Variables in the System Properties window
- Under System variables, click New...
- Variable name: M2_HOME
- Variable value: set to the Maven installation directory (e.g., C:\Program Files\Apache\maven).
- Under System variables, edit Path
- Click New
- Add the path to the bin directory inside the Maven directory (e.g. C:\Program Files\Apache\maven\bin)
- Test from command line (open a new command prompt if needed)
mvn -version
- Open a terminal and navigate to your desired project directory
mkdir my-java-lambda-projectcd my-java-lambda-project- Create a file pom.xml in your project folder root with the contents of pom.xml
mkdir srcmkdir src\mainmkdir src\main\javamkdir src\main\java\commkdir src\main\java\com\doritoescd src\main\java\com\doritoes- Create a file in
src/main/java/com/doritoesnamed MyRequestHandler.java with the contents of lambda_test.java
- Open command line in the project folder (e.g.,
my-java-lambda-project) mvn clean package- The jar file will be created in the
targetfolder- similar to
MyRequestHandler-1.0-SNAPSHOT.jar - this is the JAR file containing your compiled Java code
- similar to
- Browse to (https://console.aws.amazon.com) and log in
- In the search bar enter "Lamba" and click on Lambda
- Click Create a function
- Author from scratch
- Function name: myIPFunctionJava
- Runtime: Java 21
- Change the default Handler from example.Hello::handleRequest to com.doritoes.MyRequestHandler::handleRequest
- Architecture: the architecture you build your function for
- make sure it matches the function you built
- Leave the rest at defaults, click Create function
- In the Code source pane, click Upload from > .jar file
- Click Upload, select file MyRequestHandler-1.0-SNAPSHOT.jar within your target folder, and click Save
- Click Test
- For the test event
- Event name: Test
- Test Event JSON: test_event.json
- This provides a dummy IP address for the test to work
- Click Save
- Click Test and the test should succeed
- For the test event