How to Upload Java Jar File to Aws Lamda

Picture by: Valve Software [CC BY-SA four.0 (https://creativecommons.org/licenses/by-sa/iv.0)]

Deploying lawmaking to AWS Lambda can be a walk in the park, particularly when you're using a language that can be written inside Lambda'southward internal text editor, such equally Node.js or Python, and not Java.

In order to deploy a function to Lambda in Java, you starting time need to packet it up in either a jar or zero file. Although this is a seemingly simple task, information technology tin can exist challenging for a Java developer who lacks experience with Maven and Gradle.

Now, that'southward fine. The issue, however, is that in order to become it in the proper .zip or .jar format, a parcel manager such as Maven or Gradle needs to be used. (A plugin for Eclipse that utilizes the AWS SDK is some other option).

In this tutorial, I'll walk you through the process with Maven using the command line.

Getting Started:

  • Installing the Needed Software
  • Creating a New Project to Test Lambda

Configuring the Project for Maven:

  • Making sure the Project can Compile with Maven
  • Adding the AWS dependencies for Maven

Finishing Up

  • Compiling the Projection
  • Uploading the Projection to Lambda

Getting Started

Before nosotros need can deploy our package to lambda, we showtime need to brand sure that we have maven installed, and that we have a Java project that tin interface with AWS Lambda.

Installing the Needed Software

Before y'all exercise anything, install maven onto your computer. One time you have it installed, get into your terminal and cheque that it's configured correctly by running mvn --version

You should get a response similar to:

            Apache Maven 3.vi.0 (97c98ec64a1fdfee7767ce5...; 2018-10-24T14:41:47-04:00)          

Creating a New Project to Examination Lambda

In your favorite Java IDE (mine is Intellij), create a new maven project. In your src directory, create a package named example. Then inside instance, create a new Coffee course called Hullo. Delete the code that was machine-generated, and add in the lawmaking beneath (taken from the AWS documentation):

            bundle instance;            import java.io.InputStream;
import java.io.OutputStream;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import com.amazonaws.services.lambda.runtime.Context;
public class Hello implements RequestStreamHandler{
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
int alphabetic character;
while((letter = inputStream.read()) != -1)
{
outputStream.write(Graphic symbol.toUpperCase(letter));
}
}
}

In this tutorial, I'll be using Git Fustigate for Windows. Any final will work, still. Once you take your final open, navigate to the root directory of the project you want to deploy on Lambda with cd <path to directory>.

NOTE: if mvn isntall says you're pointing to the wrong path, set the JAVA_HOME environment system variable to the jdk in program files

Configuring the Projection for Maven

If you created the project initially every bit a maven projection, you should be all set up to compile it and button it upwards to AWS Lambda!

Otherwise, follow the steps below to configure it every bit one.

Making sure the Project tin can Compile with Maven

Starting time, you'll demand to add a pom.xml (Project Object Model) in the main directory of your project. The pom.xml contains the metadata for your project, such as what class should exist treated as the project entry point, and what dependencies the project needs.

You lot tin can observe a list of pom.xml examples hither on the maven website. You can also use the code snippet below as a template to build your pom.xml file off of.

            <projection>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</project>

To add together this file using the command line, navigate to your master project directory, the 1 containing the src folder, and run touch pom.xml

This volition create a pom.xml file. Side by side, type in vim pom.xml to open the file in the vim editor. Printing the key i to enter insert mode, and so press <shift> <insert> at the same time to paste the above code from your clipboard.

Press <escape> to exit Vim's insert manner, and and so enter wq! and press <enter> to salvage and exit vim

Calculation the AWS dependencies for Maven

In lodge to interface with AWS lambda, you need to import the RequestStreamHandler in your code.

With Maven, any imports yous have need to exist specified in the pom.xml file, where they are referred to as dependencies.

The specific dependency you lot need for the RequestStreamHandler is the aws-lambda-java-cadre dependency. To add together this to your pom.xml file, open it up in Vim once more, and add the beneath code within the <projection>...</project> tags.

            <dependencies>              
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-cadre</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>

Individual dependencies are independent in <dependency> tags, and those dependencies are contained in <dependencies> tags.

When the projection is compiled with maven, these dependency listings will be used to download the corresponding source lawmaking from online repositories into your project.

Finishing Up

Once you take a projection ready and ready to integrate with AWS Lambda, it's time to compile information technology with Maven!

Compiling Your Project

In your last, navigate to the main project directory. This is the directory that contains your pom.xml and src directory.

Run mvn install

You should see an output similar this (Fig. ane)with build success if your lawmaking was compiled successfully:

Fig. 1. Maven built successfully (Motion-picture show by: Michael Madden)

Uploading the Project to Lambda

One time your build is compiled successfully, it's fourth dimension to upload the .jar file maven created.

This file will be located in the target directory that maven created. The path to this file volition be

            <Your Project directory>/target/<NameOfProject-SNAPSHOT.jar>          

Open the Lambda panel in AWS, go to the function where you want to upload this project, and select upload goose egg or .jar file as in Fig two .

Fig. 2. Upload the .jar file (Moving picture by: Michael Madden)

Congratulations! You've successfully deployed your Coffee package to AWS Lambda!

southerlandunearithe1961.blogspot.com

Source: https://medium.com/swlh/deploying-a-java-package-to-aws-lambda-with-maven-8ecb25ce6443

0 Response to "How to Upload Java Jar File to Aws Lamda"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel