DEV Community

Prabhjeet Singh
Prabhjeet Singh

Posted on

AWS Lambda Basics: Part 2

In the previous post, We explored the usage of AWS Lambda console. Here, we will discuss the actual implementation of Lambda Function for our use case, that is, thumbnail Generation from videos stored in S3.
Project repository can be found here for reference .

You use a deployment package to deploy your function code to Lambda. Lambda supports two types of deployment packages: container images and .zip files or jar files.

Lambda provides the following libraries for Java functions:

com.amazonaws:aws-lambda-java-core (required) – Defines handler method interfaces and the context object that the runtime passes to the handler. If you define your own input types, this is the only library that you need.

com.amazonaws:aws-lambda-java-events – Input types for events from services that invoke Lambda functions.

com.amazonaws:aws-lambda-java-log4j2 – An appender library for Apache Log4j 2 that you can use to add the request ID for the current invocation to your function logs.

Below are the Maven dependencies for these libraries with latest versions as of writing this post. Add these to pom file of Maven project.

  <dependencies>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-lambda-java-core</artifactId>
      <version>1.2.1</version>
    </dependency>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-lambda-java-events</artifactId>
      <version>3.1.0</version>
    </dependency>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-lambda-java-log4j2</artifactId>
      <version>1.2.0</version>
    </dependency>
  </dependencies>
Enter fullscreen mode Exit fullscreen mode
Building a deployment package with Maven

To build a deployment package with Maven, use the Maven Shade plugin. The plugin creates a JAR file that contains the compiled function code and all of its dependencies . Maven dependency for the plugin is below.

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.2</version>
        <configuration>
          <createDependencyReducedPom>false</createDependencyReducedPom>
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
Enter fullscreen mode Exit fullscreen mode

If you use the appender library (aws-lambda-java-log4j2), you must also configure a transformer for the Maven Shade plugin. The transformer library combines versions of a cache file that appear in both the appender library and in Log4j.

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.2</version>
        <configuration>
          <createDependencyReducedPom>false</createDependencyReducedPom>
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="com.github.edwgiz.maven_shade_plugin.log4j2_cache_transformer.PluginsCacheFileTransformer">
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>com.github.edwgiz</groupId>
            <artifactId>maven-shade-plugin.log4j2-cachefile-transformer</artifactId>
            <version>2.13.0</version>
          </dependency>
        </dependencies>
      </plugin>
Enter fullscreen mode Exit fullscreen mode

To build the deployment package, use the mvn package command. It will generate JAR file in target directory.

The AWS Lambda function handler is the method in your function code that processes events. When your function is invoked, Lambda runs the handler method. When the handler exits or returns a response, it becomes available to handle another event.

Handler interfaces

The aws-lambda-java-core library defines two interfaces for handler methods. Use the provided interfaces to simplify handler configuration and validate the handler method signature at compile time.

  • com.amazonaws.services.lambda.runtime.RequestHandler

  • com.amazonaws.services.lambda.runtime.RequestStreamHandler

The RequestHandler interface is a generic type that takes two parameters: the input type and the output type. Both types must be objects. When you use this interface, the Java runtime deserializes the event into an object with the input type, and serializes the output into text. Use this interface when the built-in serialization works with your input and output types.

// Handler value: example.Handler
public class Handler implements RequestHandler<Map<String,String>, String>{
  @Override
  public String handleRequest(Map<String,String> event, Context context)
Enter fullscreen mode Exit fullscreen mode

To use your own serialization, implement the RequestStreamHandler interface. With this interface, Lambda passes your handler an input stream and output stream. The handler reads bytes from the input stream, writes to the output stream, and returns void.

Conclusion

AWS SDK for Java should be explored as per your use case and requirements. Also, There are advanced topics besides these like layers for AWS Lambda for further exploration.

Thanks for your time. Please share and provide reactions if you like the blog.

Top comments (1)

Collapse
 
vishwaranjan06 profile image
Vishwa Ranjan

Keep up the good work ..