How to Run IronPDF for Java in AWS Lambda

Important: Required Settings

  • Zip Deployment is not supported since IronPDF requires the execution of binaries at runtime.
  • You must set PackageType to Image because IronPDF for Java only supports Docker deployment.
  • You must use an AmazonLinux2 Docker image.
  • You must set the following IronPdfEngineWorkingDirectory:
import com.ironsoftware.ironpdf.Settings;
import java.nio.file.Paths;

// Setting the working directory for IronPDF engine
Settings.setIronPdfEngineWorkingDirectory(Paths.get("/tmp/"));
import com.ironsoftware.ironpdf.Settings;
import java.nio.file.Paths;

// Setting the working directory for IronPDF engine
Settings.setIronPdfEngineWorkingDirectory(Paths.get("/tmp/"));
JAVA

Note: This is required because it is the only path that AWS allows for the execution environment.

  • Increase the /tmp size. The default value is 512 MB. Please set it to at least 1024 MB.
  • Include the ironpdf-engine-linux-x64 dependency in your project:
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-engine-linux-x64</artifactId>
    <version>2022.xx.x</version>
</dependency>
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-engine-linux-x64</artifactId>
    <version>2022.xx.x</version>
</dependency>
XML
  • Set Lambda timeout to 330 seconds due to a slow start.
  • Set Lambda memory size to at least 1024 MB.

Quick Start with AWS Toolkit for IntelliJ IDEA (AWS SAM)

  1. Install Tools:

    Optional (for local testing):

  2. Create Project: (File -> New -> Project...)

AWS Lambda Project Creation

  1. Configuration:
    • Package Type: Image
    • Runtime: java8 or java11
    • SAM Template: Maven

AWS Lambda Configuration

  1. Add the following dependencies to your pom.xml:
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>2.0.3</version>
</dependency>
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-engine-linux-x64</artifactId>
    <version>2022.11.1</version>
</dependency>
<dependency>
    <groupId>io.perfmark</groupId>
    <artifactId>perfmark-api</artifactId>
    <version>0.26.0</version>
</dependency>
<dependency>
    <groupId>io.grpc</groupId>
    <artifactId>grpc-okhttp</artifactId>
    <version>1.50.2</version>
</dependency>
<dependency>
    <groupId>io.grpc</groupId>
    <artifactId>grpc-netty-shaded</artifactId>
    <version>1.50.2</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>2.0.3</version>
</dependency>
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-engine-linux-x64</artifactId>
    <version>2022.11.1</version>
</dependency>
<dependency>
    <groupId>io.perfmark</groupId>
    <artifactId>perfmark-api</artifactId>
    <version>0.26.0</version>
</dependency>
<dependency>
    <groupId>io.grpc</groupId>
    <artifactId>grpc-okhttp</artifactId>
    <version>1.50.2</version>
</dependency>
<dependency>
    <groupId>io.grpc</groupId>
    <artifactId>grpc-netty-shaded</artifactId>
    <version>1.50.2</version>
</dependency>
XML
  1. Change the handleRequest function code in App.java to:
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.Settings;

import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

// AWS Lambda function to generate a PDF from a URL using IronPDF.
public class App {
    public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) {
        APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent();

        // Enable debugging for IronPDF (optional)
        Settings.setDebug(true);

        // Set the working directory for the IronPDF engine (required)
        Settings.setIronPdfEngineWorkingDirectory(Paths.get("/tmp/"));

        try {
            context.getLogger().log("RENDER PDF");

            // Render the PDF from a URL
            PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://www.google.com");

            context.getLogger().log("RENDER PDF SUCCESS");

            // Save the generated PDF to a file
            pdf.saveAs("/tmp/my-first-pdf.pdf");

            // Set HTTP response headers
            Map<String, String> headers = new HashMap<>();
            headers.put("Content-Type", "application/json");
            headers.put("X-Custom-Header", "application/json");

            // Return the successful response
            return response
                    .withHeaders(headers)
                    .withStatusCode(200)
                    .withBody("ENJOY IRON-PDF!");

        } catch (Exception e) {
            // Return the error response
            return response
                    .withBody("{" + e.getMessage() + "}")
                    .withStatusCode(500);
        }
    }
}
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.Settings;

import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

// AWS Lambda function to generate a PDF from a URL using IronPDF.
public class App {
    public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) {
        APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent();

        // Enable debugging for IronPDF (optional)
        Settings.setDebug(true);

        // Set the working directory for the IronPDF engine (required)
        Settings.setIronPdfEngineWorkingDirectory(Paths.get("/tmp/"));

        try {
            context.getLogger().log("RENDER PDF");

            // Render the PDF from a URL
            PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://www.google.com");

            context.getLogger().log("RENDER PDF SUCCESS");

            // Save the generated PDF to a file
            pdf.saveAs("/tmp/my-first-pdf.pdf");

            // Set HTTP response headers
            Map<String, String> headers = new HashMap<>();
            headers.put("Content-Type", "application/json");
            headers.put("X-Custom-Header", "application/json");

            // Return the successful response
            return response
                    .withHeaders(headers)
                    .withStatusCode(200)
                    .withBody("ENJOY IRON-PDF!");

        } catch (Exception e) {
            // Return the error response
            return response
                    .withBody("{" + e.getMessage() + "}")
                    .withStatusCode(500);
        }
    }
}
JAVA
  1. Set Lambda config in template.yaml:
Globals:
  Function:
    Timeout: 400
    MemorySize: 2048
    EphemeralStorage:
      Size: 1024
# Do not modify other configurations
Globals:
  Function:
    Timeout: 400
    MemorySize: 2048
    EphemeralStorage:
      Size: 1024
# Do not modify other configurations
YAML
  1. Update Dockerfile:
    • Note: For Java 8, use java8.al2 images because they use AmazonLinux2, while java8 uses the old AmazonLinux.
FROM public.ecr.aws/sam/build-java8.al2:latest as build-image
WORKDIR "/task"
COPY src/ src/
COPY pom.xml ./
RUN mvn -q clean install
RUN mvn dependency:copy-dependencies -DincludeScope=compile

FROM public.ecr.aws/lambda/java:8.al2
RUN yum update -y
RUN yum install -y pango.x86_64 libXcomposite.x86_64 libXcursor.x86_64 \
    libXdamage.x86_64 libXext.x86_64 libXi.x86_64 libXtst.x86_64 \
    cups-libs.x86_64 libXScrnSaver.x86_64 libXrandr.x86_64 GConf2.x86_64 \
    alsa-lib.x86_64 atk.x86_64 gtk3.x86_64 ipa-gothic-fonts \
    xorg-x11-fonts-100dpi xorg-x11-fonts-75dpi xorg-x11-utils \
    xorg-x11-fonts-cyrillic xorg-x11-fonts-Type1 xorg-x11-fonts-misc \
    glibc-devel.x86_64 at-spi2-atk.x86_64 mesa-libgbm.x86_64 libxkbcommon \
    amazon-linux-extras
RUN amazon-linux-extras install epel -y
RUN yum install -y libgdiplus
RUN chmod 777 /tmp/
COPY --from=build-image /task/target/classes /var/task/
COPY --from=build-image /task/target/dependency /var/task/lib

# Command can be overridden by providing a different command in the template directly.
CMD ["helloworld.App::handleRequest"]
  1. Build the project:
sam build -u
sam build -u
SHELL
  1. Deploy the project:
sam deploy --guided
sam deploy --guided
SHELL
  1. Enjoy IronPDF in AWS Lambda! Now your function is live at: Access AWS Lambda Console

Frequently Asked Questions

How can I create and edit PDFs in Java on AWS Lambda?

You can use IronPDF for Java within AWS Lambda by setting up the environment with an 'AmazonLinux2' Docker image and configuring the necessary settings such as increasing the /tmp directory size and setting the Lambda timeout.

Why can't I use Zip Deployment for PDF processing in AWS Lambda?

Zip Deployment is not supported for IronPDF in AWS Lambda because it requires binary execution at runtime. The 'PackageType' must be set to 'Image' for Docker deployment.

What are the required configurations for IronPDF's working directory in AWS Lambda?

Set the working directory for the IronPDF engine to '/tmp/' and ensure the directory size is increased to at least 1024 MB to handle PDF processing efficiently.

What dependencies should be included for PDF generation in a Maven project on AWS Lambda?

Include the 'ironpdf-engine-linux-x64' dependency in your Maven project's pom.xml, along with other necessary libraries to enable PDF creation and editing.

How can I render a PDF from a URL in AWS Lambda using Java?

Utilize the IronPDF method PdfDocument.renderUrlAsPdf to convert a URL into a PDF document, and save it with the pdf.saveAs method.

What are the recommended Lambda timeout and memory settings for PDF processing?

Set the Lambda timeout to 330 seconds and allocate at least 1024 MB of memory to accommodate the processing needs of IronPDF in AWS Lambda.

Which runtime environments can be used for running PDF operations in Java on AWS Lambda?

IronPDF supports 'java8' and 'java11' runtime environments for executing PDF processing tasks on AWS Lambda.

How can I quickly start with AWS Toolkit for IntelliJ IDEA for PDF processing in Java?

Install IntelliJ IDEA, AWS Toolkit, SAM CLI, and Docker. Optionally, for local testing, set up Java 8 and Maven, then follow the quick start guide to create and configure your project.

What is the process to deploy a Java PDF processing project on AWS Lambda using SAM CLI?

First, build your project with the command sam build -u, then deploy it using sam deploy --guided to effectively set up your PDF processing in AWS Lambda.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.
Talk to an Expert Five Star Trust Score Rating

Ready to Get Started?