AWS Lambda Hello world Example in Java.

AWS Lambda is a compute service from Amazon which follows Serverless Architecture which is also known as FaaS (Function as a Service). AWS Lambda lets you run your code on highly available compute infrastructure, and does not require provisioning or managing of servers. It executes your code only when requested via different client applications and scales automatically, from a few requests per day to thousands per second. Payment terms are based on the compute time your application consumes and there is no charge when your Lambda functions are not invoked.

AWS Lambda allows us to run code for many different types of application and backend services, without the need of server administration overheads. With AWS Lambda the upfront costs and efforts associated with the administration of the servers, operating system maintenance, provisioning of server capacity, provisioning of redundant servers for scaling, server uptime monitoring etc can be reduced to zero. All that is needed is to supply the code in any of the programming languages that AWS Lambda supports. Currently AWS Lambda supports Node.js, Java, C# and Python.

Step 1: Create Lambda Function

Handler – When Java is used as a programming language to develop Lambda functions, Handler is the java class that implements the Lambda function. The code that you want to execute to fulfill a particular request can be written inside this handler function.

Following two approaches are supported by AWS Lambda for creating a handler:

  • Loading the handler method directly without having to implement an interface. This section describes this approach.
  • Implementing standard interfaces provided as part of aws-lambda-java-core library (interface approach). For more information, see Leveraging Predefined Interfaces for Creating Handler (Java).

The general syntax for the handler is as follows:

outputType handler-name(inputType input, Context context) {
   ...
}

In order for AWS Lambda to successfully invoke a handler, it must be invoked with input data that can be serialized into the data type of the input parameter.

For example, consider the following Java example code.

package example;

import com.amazonaws.services.lambda.runtime.Context; 

public class Hello {
    public String myHandler(int myCount, Context context) {
        return String.valueOf(myCount);
    }
}

Step 2: Create Deployment Package using Maven

This section shows how to package your Java code into a deployment package using Maven at the command line.

 Project pom.xml file add the following project information and configuration details for Maven to build the project.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>doc-examples</groupId>
  <artifactId>lambda-java-example</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>lambda-java-example</name>

  <dependencies>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-lambda-java-core</artifactId>
      <version>1.1.0</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.3</version>
        <configuration>
          <createDependencyReducedPom>false</createDependencyReducedPom>
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Note – Maven Shade Plugin allows to create a Jar together with its dependency Jars into a single executable Jar file, so called fat Jar or uber Jar.

Now you can build the project using Maven at the command line.

$ mvn package

The build creates this resulting .jar, using information in the pom.xml to do the necessary transforms. This is a standalone .jar (.zip file) that includes all the dependencies. This is your deployment package that you can upload to AWS Lambda to create a Lambda function.

Step 3: Create Lambda function through the AWS console

When you package and upload this code to create your Lambda function, you will specify the package.class-name::handler method reference as the handler.

  1. Sign into the AWS Management Console, open the AWS Lambda console and choose the region.
  2. Choose Create a Lambda function.
  3. In Step 1: Select blueprint, choose the hello-world blueprint or skip this step.
  4. In Step 2: Configure function, specify the following values.
    1. Enter a function name
    2. Select Java 8 from the Runtime list.
    3. Choose Upload a .ZIP file, click Upload, and then choose the .jar created as part of Step 2 – Create Deployment package using Maven.
    4. In Handler, specify package.class-name::handler
    5. For the IAM Role, create a role by that allows you to write to CloudWatch logs.
    6. For Memory, specify 256 (or the amount of memory you need).
    7. For Timeout, specify 15 seconds (or the response timeout right for you).

5 Comments

  1. Pingback: writeessay

Leave a Reply

Your email address will not be published. Required fields are marked *