AWS Lambda Reduce Jar Size

AWS Lambda limits the size of jar to 50 MB.

There are some things you can do to reduce the size of your jar.

use [[ProGuard|]]

! Summary


to make fat jar:

`sbt assembly`

to make test jar

`sbt test:package`

to shrink file

`java -jar /opt/proguard/lib/proguard.jar @shrink.proguard`

to test that jar works:

`scala -J-Xmx4G -J-Xms4G -cp “/home/neil/.ivy2/cache/org.scalatest/scalatest_2.11/bundles/scalatest_2.11-2.2.6.jar:./target/scala-2.11/neatapp-assembly-0.1-SNAPSHOT_shrunk.jar:./target/scala-2.11/neatapp_2.11-0.1-SNAPSHOT-tests.jar” org.activeintel.MySpec`
! Configuring ProGuard


-injars your-assembly-0.1-SNAPSHOT.jar
-outjars your-assembly-0.1-SNAPSHOT_shrunk.jar
-libraryjars /usr/lib/jvm/java-8-oracle/jre/lib/rt.jar
-keep class org.activeintel.** {
public protected private *;
-keep class org.something.else.** {
public protected private *;

! Test

This step is very important to check the sanity of your shrunk jar.

!! Shrinking

First build the fat jar: `sbt assembly`

Then shrink it:

scala -J-Xmx4G -J-Xms4G -cp “/home/neil/.ivy2/cache/org.scalatest/scalatest_2.11/bundles/scalatest_2.11-2.2.6.jar:/home/neil/tmp/agric/sector-graph-extractor-assembly-0.1-SNAPSHOT_shrunk.jar:/home/neil/tmp/agric/test-cases/target/scala-2.11/sector-graph-extractor-test_2.11-0.1-SNAPSHOT.jar” org.activeintel.sector.graph.extractor.SectorGraphExtractorSpec
!! Packaging Tests in a jar

modify `build.sbt` [[ref|]]
publishArtifact in (Test, packageBin) := true // jar of tests: `sbt test:package`

to package run:

`sbt test:package`

keywords: scala sbt reduce jar size shrink aws lambda smaller remove unused dependencies

About Neil Rubens

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *