Updated January 6, 2025
Share:

How to Run & Deploy IronPDF .NET on AWS Lambda

Amazon Web Services V1 related to How to Run & Deploy IronPDF .NET on AWS Lambda

1. Create AWS Lambda with a container template (.NET 5)

Please refer to the first part of this official document from AWS: .NET 5 AWS Lambda Support with Container Images.

2. Add package dependencies

These dependencies are required for Chrome in this AWS environment.

Please modify the Docker file according to these instructions:

AWS Lambda with .NET 5

# base image (AWS Lambda)
FROM public.ecr.aws/lambda/dotnet:5.0
RUN yum update -y && yum install -y gcc-c++ glibc-devel.i686 glibc.i686 pango.x86_64 libXcomposite.x86_64 libXcursor.x86_64 dbus-glib-devel && yum install -y libXdamage.x86_64 libXext.x86_64 libXi.x86_64 libXtst.x86_64 cups-libs.x86_64 libXScrnSaver.x86_64 && yum install -y libXrandr.x86_64 GConf2.x86_64 alsa-lib.x86_64 atk.x86_64 gtk3.x86_64 ipa-gothic-fonts xorg-x11-fonts-100dpi && yum install -y xorg-x11-fonts-75dpi xorg-x11-utils xorg-x11-fonts-cyrillic xorg-x11-fonts-Type1 xorg-x11-fonts-misc && yum install -y glibc-devel.x86_64 at-spi2-atk.x86_64 mesa-libgbm.x86_64 libxshmfence libxkbcommon amazon-linux-extras && amazon-linux-extras install epel -y && yum install -y libgdiplus && yum clean all && rm -rf /var/cache/yum
# This COPY command copies the .NET Lambda project's build artifacts from the host machine into the image.
# The source of the COPY should match where the .NET Lambda project publishes its build artifacts. If the Lambda function is being built
# with the AWS .NET Lambda Tooling, the `--docker-host-build-output-dir` switch controls where the .NET Lambda project
# will be built. The .NET Lambda project templates default to having `--docker-host-build-output-dir`
# set in the aws-lambda-tools-defaults.json file to "bin/Release/lambda-publish".
#
# Alternatively Docker multi-stage build could be used to build the .NET Lambda project inside the image.
# For more information on this approach checkout the project's README.md file.
WORKDIR /var/task
COPY "bin/Release/lambda-publish" .
CMD ["AWSLambdaNet5Container::AWSLambdaNet5Container.PrintPdf::FunctionHandler"]

AWS Lambda with .NET 7

# base image (AWS Lambda)
FROM public.ecr.aws/lambda/dotnet:7
# install necessary packages
RUN yum update -y && yum groupinstall -y "Development Tools" && yum install -y gcc-c++ glibc-devel.i686 glibc.i686 pango.x86_64 libXcomposite.x86_64 libXcursor.x86_64 dbus-glib-devel && yum install -y libXdamage.x86_64 libXext.x86_64 libXi.x86_64 libXtst.x86_64 cups-libs.x86_64 libXScrnSaver.x86_64 && yum install -y libXrandr.x86_64 GConf2.x86_64 alsa-lib.x86_64 atk.x86_64 gtk3.x86_64 ipa-gothic-fonts xorg-x11-fonts-100dpi && yum install -y xorg-x11-fonts-75dpi xorg-x11-utils xorg-x11-fonts-cyrillic xorg-x11-fonts-Type1 xorg-x11-fonts-misc && yum install -y glibc-devel.x86_64 at-spi2-atk.x86_64 mesa-libgbm.x86_64 libxshmfence libxkbcommon amazon-linux-extras && amazon-linux-extras install epel -y && yum install -y libgdiplus && yum clean all && rm -rf /var/cache/yum
WORKDIR /var/task
# This COPY command copies the .NET Lambda project's build artifacts from the host machine into the image.
# The source of the COPY should match where the .NET Lambda project publishes its build artifacts. If the Lambda function is being built
# with the AWS .NET Lambda Tooling, the `--docker-host-build-output-dir` switch controls where the .NET Lambda project
# will be built. The .NET Lambda project templates default to having `--docker-host-build-output-dir`
# set in the aws-lambda-tools-defaults.json file to "bin/Release/lambda-publish".
#
# Alternatively Docker multi-stage build could be used to build the .NET Lambda project inside the image.
# For more information on this approach checkout the project's README.md file.
COPY "bin/Release/lambda-publish" .

AWS Lambda with .NET 8

FROM public.ecr.aws/lambda/dotnet:8
# install necessary packages
RUN dnf update -y && dnf install -y gcc-c++ pango.x86_64 libXcomposite.x86_64 libXcursor.x86_64 dbus-glib-devel && dnf install -y libXdamage.x86_64 libXi.x86_64 libXtst.x86_64 cups-libs.x86_64 libXScrnSaver.x86_64 && dnf install -y libXrandr.x86_64 alsa-lib.x86_64 atk.x86_64 gtk3.x86_64 ipa-gothic-fonts xorg-x11-fonts-100dpi && dnf install -y xorg-x11-fonts-75dpi xorg-x11-utils xorg-x11-fonts-cyrillic xorg-x11-fonts-Type1 xorg-x11-fonts-misc && dnf install -y mesa-libgbm.x86_64 && dnf install -y nss-3.90.0-3.amzn2023.0.4.x86_64
WORKDIR /var/task
# This COPY command copies the .NET Lambda project's build artifacts from the host machine into the image.
# The source of the COPY should match where the .NET Lambda project publishes its build artifacts. If the Lambda function is being built
# with the AWS .NET Lambda Tooling, the `--docker-host-build-output-dir` switch controls where the .NET Lambda project
# will be built. The .NET Lambda project templates default to having `--docker-host-build-output-dir`
# set in the aws-lambda-tools-defaults.json file to "bin/Release/lambda-publish".
#
# Alternatively Docker multi-stage build could be used to build the .NET Lambda project inside the image.
# For more information on this approach checkout the project's README.md file.
COPY "bin/Release/lambda-publish" .

3. Add the IronPDF (Linux) NuGet package

Install IronPdf.Linux

  1. In Solution Explorer, right-click References, Manage NuGet Packages
  2. Select Browse and search IronPdf.Linux
  3. Select the package and install.

4. Modify FunctionHandler code

This example will create a pdf file from a webpage https://ironpdf.com/ and save to /tmp. In order to view this pdf you must upload this pdf to another service such as S3.

It is necessary to configure the temporary folder when using IronPDF on AWS Lambda. Please use the TempFolderPath and CustomDeploymentDirectory properties to do this.

public Casing FunctionHandler(string input, ILambdaContext context)
{
    try
    {
        context.Logger.LogLine($"START FunctionHandler RequestId: {context.AwsRequestId} Input: {input}");

        var awsTmpPath = @"/tmp/"; // AWS temporary storage

        //[optional] enable logging (please uncomment these code if you face any problem)
        //IronPdf.Logging.Logger.EnableDebugging = true;
        //IronPdf.Logging.Logger.LogFilePath = awsTmpPath;
        //IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;

        //set your license key
        IronPdf.License.LicenseKey = "YOUR_LICENSE_KEY";

        //set ChromeGpuMode to Disabled
        IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled;

        //set IronPDF Temp Path
        IronPdf.Installation.TempFolderPath = awsTmpPath;
        IronPdf.Installation.CustomDeploymentDirectory = awsTmpPath;

        //set auto LinuxAndDockerDependenciesAutoConfig
        IronPdf.Installation.LinuxAndDockerDependenciesAutoConfig = true;

        context.Logger.LogLine($"creating IronPdf.ChromePdfRenderer");
        var Renderer = new IronPdf.ChromePdfRenderer();

        context.Logger.LogLine($"rendering Pdf");
        using var pdfDoc = Renderer.RenderUrlAsPdf("https://ironpdf.com/");

        var guid = Guid.NewGuid();
        var fileName = $"/tmp/{input}_{guid}.pdf"; //save file to /tmp

        context.Logger.LogLine($"saving pdf name : {fileName}");
        pdfDoc.SaveAs(fileName);

        //here you can upload saved pdf file to anywhere such as S3. 

        context.Logger.LogLine($"COMPLETE!");
    }
    catch (Exception e)
    {
        context.Logger.LogLine($"[ERROR] FunctionHandler : {e.Message}");
    }

    return new Casing(input?.ToLower(), input?.ToUpper());
}
public Casing FunctionHandler(string input, ILambdaContext context)
{
    try
    {
        context.Logger.LogLine($"START FunctionHandler RequestId: {context.AwsRequestId} Input: {input}");

        var awsTmpPath = @"/tmp/"; // AWS temporary storage

        //[optional] enable logging (please uncomment these code if you face any problem)
        //IronPdf.Logging.Logger.EnableDebugging = true;
        //IronPdf.Logging.Logger.LogFilePath = awsTmpPath;
        //IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;

        //set your license key
        IronPdf.License.LicenseKey = "YOUR_LICENSE_KEY";

        //set ChromeGpuMode to Disabled
        IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled;

        //set IronPDF Temp Path
        IronPdf.Installation.TempFolderPath = awsTmpPath;
        IronPdf.Installation.CustomDeploymentDirectory = awsTmpPath;

        //set auto LinuxAndDockerDependenciesAutoConfig
        IronPdf.Installation.LinuxAndDockerDependenciesAutoConfig = true;

        context.Logger.LogLine($"creating IronPdf.ChromePdfRenderer");
        var Renderer = new IronPdf.ChromePdfRenderer();

        context.Logger.LogLine($"rendering Pdf");
        using var pdfDoc = Renderer.RenderUrlAsPdf("https://ironpdf.com/");

        var guid = Guid.NewGuid();
        var fileName = $"/tmp/{input}_{guid}.pdf"; //save file to /tmp

        context.Logger.LogLine($"saving pdf name : {fileName}");
        pdfDoc.SaveAs(fileName);

        //here you can upload saved pdf file to anywhere such as S3. 

        context.Logger.LogLine($"COMPLETE!");
    }
    catch (Exception e)
    {
        context.Logger.LogLine($"[ERROR] FunctionHandler : {e.Message}");
    }

    return new Casing(input?.ToLower(), input?.ToUpper());
}

5. Increase Memory and Timeout

IronPDF requires more time and memory than the default value of Lambda. You can configure it at aws-lambda-tools-defaults.json. Please adjust this to match your function. In this example we will set it to 512 (MB) and 330 (seconds)

"function-memory-size" : 512, 
"function-timeout"     : 330,

You also can update this configuration using the Lambda console. Navigate to the Configuring AWS Lambda functions article for more information.

6. Publish

Please follow the instructions in the latter part of the '.NET 5 AWS Lambda Support with Container Images' document to publish and test your Lambda function.

7. Try it out!

You can activate the Lambda function using the Lambda console or via Visual Studio by using the AWS Toolkit for Visual Studio.