avatar
generate Java package to AWS Lambda with Maven Lambda

» First and foremost, use the following command to list all available archetypes that you can use with the archetypeArtifactId parameter when generating a new project.

$ mvn archetype:generate -Dfilter=org.apache.maven.archetypes:

Choose archetype:
1: remote -> org.apache.maven.archetypes:maven-archetype-archetype (An archetype which contains a sample archetype.)
...
8: remote -> org.apache.maven.archetypes:maven-archetype-profiles (-)
9: remote -> org.apache.maven.archetypes:maven-archetype-quickstart (An archetype which contains a sample Maven project.)
...
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 9

» Use Maven command generates new Maven project using the maven-archetype-quickstart archetype. Let us break down the command and its parameters:

mvn archetype:generate -DgroupId=com.app.flagtickportal -DartifactId=flagtick-portal -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

» Here is folder structure:

flagtick-portal/
|-- src/
|   |-- main/
|   |   |-- java/
|   |   |   |-- com/
|   |   |       |-- app/
|   |   |           |-- flagtickportal/
|   |   |               |-- MyLambdaHandler.java
|   |   |-- test/
|   |   |-- resources/
|   |       |-- application.properties
|-- target/
|-- .gitignore
|-- pom.xml

» In circumstance of serverless deployments, you generally don't need to include the target folder and the test folder in the package that you upload to a serverless platform, such as AWS Lambda.

flagtick-portal/
|-- src/
|   |-- main/
|   |   |-- java/
|   |   |   |-- com/
|   |   |       |-- app/
|   |   |           |-- flagtickportal/
|   |   |               |-- MyLambdaHandler.java
|   |   |-- resources/
|   |       |-- application.properties
|-- .gitignore
|-- pom.xml

» After that, make the necessary changes to packages in the pom.xml file. Then, run mvn clean install to update all external libraries.

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.app.flagtickportal</groupId>
  <artifactId>flagtickPortal</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Flagtick Sample Lambda Function</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
    <awsjavasdk.version>1.11.934</awsjavasdk.version>
    <pdfbox.version>3.0.0-alpha2</pdfbox.version>
  </properties>

  <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.9.0</version>
    </dependency>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-java-sdk-core</artifactId>
      <version>${awsjavasdk.version}</version>
    </dependency>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-java-sdk-kms</artifactId>
      <version>${awsjavasdk.version}</version>
    </dependency>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-java-sdk-s3</artifactId>
      <version>${awsjavasdk.version}</version>
    </dependency>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-java-sdk-rdsdata</artifactId>
      <version>${awsjavasdk.version}</version>
    </dependency>
    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>jmespath-java</artifactId>
      <version>${awsjavasdk.version}</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.6.7.4</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.dataformat</groupId>
      <artifactId>jackson-dataformat-cbor</artifactId>
      <version>2.6.7</version>
    </dependency>
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.1.3</version>
    </dependency>
    <dependency>
      <groupId>joda-time</groupId>
      <artifactId>joda-time</artifactId>
      <version>2.8.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.5.13</version>
    </dependency>
    <dependency>
      <groupId>software.amazon.ion</groupId>
      <artifactId>ion-java</artifactId>
      <version>1.0.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.pdfbox</groupId>
      <artifactId>pdfbox</artifactId>
      <version>${pdfbox.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.pdfbox</groupId>
      <artifactId>fontbox</artifactId>
      <version>${pdfbox.version}</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.2</version>
      </plugin>
      <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>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.2.0</version>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>com.app.flagtickportal.App</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

» Reload the project after Maven installation to ensure all repositories are installed.

» Use main is like the starting point for regular testing, and handleRequest is what AWS Lambda uses when your function gets a job.

package com.app.flagtickportal;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;

public class App implements RequestHandler&lt;APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent>
{
    public static void main( String[] args )
    {
        System.out.println( "Moving forward!" );
    }

    @Override
    public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
        return null;
    }
}

» You can access the GitHub link here to download and explore the structure of a Maven Java project used for AWS Lambda.

» Generate the deployment package by executing the mvn package command. This Maven command compiles your Java source code, runs tests, and packages the application into a deployable format, typically a JAR file.

mvn package

As you can see inside the target folder of your Maven Java project.

Note: If you want to upload different versions of the JAR to AWS Lambda, simply adjust the version in the pom.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.app.flagtickportal</groupId>
  <artifactId>flagtick-portal</artifactId>
  <packaging>jar</packaging>
  <version>1.1-SNAPSHOT</version>
  <name>Flagtick Sample Lambda Function</name>
  ...

» Navigate to the AWS Lambda function, go to Code source, and choose Upload from (.zip or .jar file). Proceed to upload the JAR file.


You need to login to do this manipulation!