This section provides examples of creating .zip file as your deployment package. You can use any build and packaging tool you like to create this zip. Regardless of the tools you use, the resulting .zip file must have the following structure:
- All compiled class files and resource files at the root level.
- All required jars to run the code in the
/lib
directory.
Note
You can also build a standalone .jar (also a zipped file) as your deployment package. For examples of creating standalone .jar using Maven, see AWS Lambda Deployment Package in Java.
The following examples use Gradle build and deployment tool to create the .zip.
Important
Gradle version 2.0 or later is required.
You will need to download Gradle. For instructions, go to the gradle website, https://gradle.org/ .
At the end of this walkthrough, you will have a project directory (project-dir
) with content having the following structure:
project-dir/build.gradle
project-dir/src/main/java/
The /java
folder will contain your code. For example, if your package name is example
, and you have a Hello.java
class in it, the structure will be:
project-dir/src/main/java/example/Hello.java
After you build the project, the resulting .zip file (that is, your deployment package), will be in the project-dir/build/distributions
subdirectory.
-
Create a project directory (
project-dir
). -
In the
project-dir
, createbuild.gradle
file and add the following content:apply plugin: 'java' repositories { mavenCentral() } dependencies { compile ( 'com.amazonaws:aws-lambda-java-core:1.1.0', 'com.amazonaws:aws-lambda-java-events:1.1.0' ) } task buildZip(type: Zip) { from compileJava from processResources into('lib') { from configurations.compileClasspath } } build.dependsOn buildZip
Note
The repositories section refers to Maven Central Repository. At the build time, it fetches the dependencies (that is, the two AWS Lambda libraries) from Maven Central.
The buildZip
task describes how to create the deployment package .zip file.
For example, if you unzip the resulting .zip file you should find any of the compiled class files and resource files at the root level. You should also find a /lib
directory with the required jars for running the code.
If you are following other tutorial topics in this guide, the specific tutorials might require you to add more dependencies. Make sure to add those dependencies as required.
-
In the
project-dir
, create the following structure:project-dir/src/main/java/
-
Under the
/java
subdirectory you add your Java files and folder structure, if any. For example, if you Java package name isexample
, and source code isHello.java
, then your directory structure looks like this:project-dir/src/main/java/example/Hello.java
-
Run the following gradle command to build and package the project in a .zip file.
project-dir> gradle build
-
Verify the resulting
project-dir.zip
file in theproject-dir/build/distributions
subdirectory. -
Now you can upload the .zip file, your deployment package to AWS Lambda to create a Lambda function and test it by manually invoking it using sample event data. For instruction, see Create a Lambda Function Authored in Java.
You may choose not to use the Maven Central repository. Instead have all the dependencies in the project folder. In this case your project folder (project-dir
) will have the following structure:
project-dir/jars/ (all jars go here)
project-dir/build.gradle
project-dir/src/main/java/ (your code goes here)
So if your Java code has example
package and Hello.java
class, the code will be in the following subdirectory:
project-dir/src/main/java/example/Hello.java
You build.gradle
file should be as follows:
apply plugin: 'java'
dependencies {
compile fileTree(dir: 'jars', include: '*.jar')
}
task buildZip(type: Zip) {
from compileJava
from processResources
into('lib') {
from configurations.compileClasspath
}
}
build.dependsOn buildZip
Note that the dependencies specify fileTree
which identifies project-dir/jars
as the subdirectory that will include all the required jars.
Now you build the package. Run the following gradle command to build and package the project in a .zip file.
project-dir> gradle build