We will create our first Lambda with Java as a Maven project. The javadoc comments and package-info.java files required for checkstyle checks from the parent are not shown here. We are also making use of the Maven shade plugin from the parent for generating the JAR files. You may refer to the code files for each recipe for the complete code:
- Create the Java project based on Maven.
Create a Java project based on Maven with our common parent, declared as shown next in the POM file:
You may use an IDE such as Intellij IDEA or Eclipse for working with the examples in this book.
<groupId>tech.heartin.books.serverless-cookbook</groupId>
<artifactId>helloworld-lambda</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>tech.heartin.books.serverlesscookbook</groupId>
<artifactId>serverless-cookbook-parent-aws-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
- Also, declare the only dependency we need for our hello world lambda project in the POM file:
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>${aws.lambda.java.core.version}</version
</dependency>
</dependencies>
The dependency versions (for example, aws.lambda.java.core.version) are defined in the POM file for the parent project serverless-cookbook-parent-aws-java.
- Create the Lambda handler class and package it as a JAR.
Create a class, HelloWorldLambdaHandler, that implements the interface, RequestHandler:
package tech.heartin.books.serverlesscookbook;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
public final class HelloWorldLambdaHandler implements RequestHandler<String, String> {
public String handleRequest(final String s, final Context context) {
context.getLogger().log("input: " + s + "\n");
String greeting = "Hello " + s;
return greeting;
}
}
To package the Lambda as a JAR file, from the project root folder, run the following:
mvn clean package
Two JARS will be created: one with only class files (starting with original-) and an Uber JAR with dependencies (starting with serverless-). You can easily differentiate between one and the other looking at their sizes. We will use the JAR file that starts with original- and that has only the class files for this recipe.
- Deploy the Lambda handler to the AWS:
- Log in to the AWS console, and go to Lambda dashboard by clicking on Services and searching or selecting Lambda. Currently, it is under the compute category.
- Create a Lambda function as follows:
- Click on Create Function.
- Select Author From Scratch, which is the default.
- Give a name, such as myHelloWorldLambda.
- Select Java 8 as the runtime.
- Under Role, select Create new role from one or more templates.
- Give a role name, such as myHelloWorldLambda.
- Leave the field for specifying Policy templates blank.
- Click on Create Function. You should see a success message after a while.
- Upload the Lambda JAR:
Go to the Function code section and do the following:
-
- Select Code entry type as Upload a .zip or .jar file.
- Select Java 8 as the runtime.
- Specify the fully qualified class name with handler method name as the following: tech.heartin.books.serverlesscookbook.HelloWorldLambdaHandler::handleRequest.
- Click on Upload under Function package and select the JAR file. You can select the JAR whose name starts with original-.
- Click on Save to save with defaults for other fields.
- We can test the uploaded JAR:
- Select Configure test events from the Select a test event dropdown next to the Test button.
- Select Create new test event.
- Give a name for the event: MyHelloWorldTest.
- Within the JSON request content area, just specify your name, such as Heartin.
- Click on Create. If successful, it will take you to the myHelloWorldLambda function page.
- From the myHelloWorldLambda function page, select the test event, MyHelloWorldTest, next to the Test button, and click the Test button.
- You should see the message Hello Heartin after expanding the details of execution result.
- We can also check the logs printed using context.getLogger().log():
- Under the Log output section, you can see the log you printed.
- You can also see the log in the CloudWatch service. There should be a Click here link to view the CloudWatch log group. Click on the link, wait or refresh for a stream that matches your invocation time, and click on the stream link to see the log statement within CloudWatch.