Details on format of Java ZIP file supported by AWS Lambda

418 views Asked by At

Where is documentation on exactly what format of contents of a ZIP file that AWS Lambda supports for Java deployments?

Sure, I've read Deploy Java Lambda functions with .zip or JAR file archives and a dozen other similar pages. But they all just say "use Maven Shade Plugin" or "use this setting on Gradle". This doesn't tell me specifications on the actual content recognized and accepted by AWS Lambda. (For example, AWS Lambda doesn't support multirelease JARs. Where is that documented?)

Maybe I want to use another approach than Maven Shade Plugin to create my ZIP/JAR files for AWS Lambda. Does AWS Lambda support a directory with library JARs in addition to classes? Does it only support a package-hierarchy of class files? What exactly must go in this ZIP/JAR, and what variations does AWS Lambda support? Where is this documented?

2

There are 2 answers

3
Frank AFRIAT On

I discourage you of using the Shade Plugin. See my post on medium : https://medium.com/@frank-afriat/the-only-good-but-little-known-way-to-package-your-java-lambda-db82f45d8d5d

where I recommend using this in your maven project:

     <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
                <dependencies>
                  <dependency>
                    <groupId>io.microlam</groupId>
                    <artifactId>microlam-assembly-descriptor</artifactId>
                    <version>1.3</version>
                  </dependency>
                </dependencies>
        
        <configuration>
          <descriptorRefs>
              <descriptorRef>aws-lambda</descriptorRef>
          </descriptorRefs>
        </configuration>
          
            <executions>
            <execution>
                <id>aws-lambda-deployment-package</id>
                <phase>package</phase>
                <goals>
                <goal>single</goal>
                </goals>
            </execution>
            </executions>
    </plugin>

The lambda bundle will appear in your target folder after calling:

mvn package

Regarding the structure of the lambda bundle, the dependencies will go inside a lib folder in the archive.

I suggest you to try my framework https://microlam.io for ease of use and to get a working example.

0
Garret Wilson On

I have verified two layouts accepted by AWS Lambda in practice.

Bare, Unpacked Fat JAR

The first is simply an unpacking of all dependency JARs into the root directory—a "fat JAR" or "uber JAR". This is what the Maven Shade Plugin does, and it has all sorts of downsides. Like Frank Afriat said in his answer, I don't recommend it.

Fat JAR with Lib JARs

A better layout, suggested by Maarten Brak in another answer, is to include only your own project's unpacked class files, but place all the JARs inside a lib/ subdirectory. This doesn't have the downsides of the Maven Shade Plugin (such as dueling files with the same name and the dilemma of whether to merge them or delete them or move them; and what to do with multirelease JARs). It looks like this:

  • com/
  • com/example/
  • com/example/Foo.class
  • lib/
  • lib/foo.jar
  • lib/bar.jar

To generate a JAR file like this, you can use the Maven Assembly Descriptor at the answer I mentioned, or use the Spring Boot Plugin if one day they address Issue #36101 which I filed today.

Note that this answer is provided just to be helpful and it is based upon my research and testing. It is not official documentation. If AWS official documentation exists, an answer here on that subject would be welcome.