Using IronPDF to Create PDF Files 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
# Sample Dockerfile -- .NET 5
FROM public.ecr.aws/lambda/dotnet:5.0
# Copy function code
COPY "bin/Release/net5.0/linux-x64/publish" /var/task
# Add required packages for Chrome
RUN yum install -y atk cups-libs libXcomposite libXcursor libXdamage libXext \
libXi libXtst pango alsa-lib at-spi2-atk gtk3 libdrm libgbm
AWS Lambda with .NET 7
# Sample Dockerfile -- .NET 7
FROM public.ecr.aws/lambda/dotnet:7.0
# Copy function code
COPY "bin/Release/net7.0/linux-x64/publish" /var/task
# Add required packages for Chrome
RUN yum install -y atk cups-libs libXcomposite libXcursor libXdamage libXext \
libXi libXtst pango alsa-lib at-spi2-atk gtk3 libdrm libgbm
AWS Lambda with .NET 8
# Sample Dockerfile -- .NET 8
FROM public.ecr.aws/lambda/dotnet:8.0
# Copy function code
COPY "bin/Release/net8.0/linux-x64/publish" /var/task
# Add required packages for Chrome
RUN yum install -y atk cups-libs libXcomposite libXcursor libXdamage libXext \
libXi libXtst pango alsa-lib at-spi2-atk gtk3 libdrm libgbm
3. Add the IronPDF (Linux) NuGet Package
Install IronPdf.Linux
- In Solution Explorer, right-click References, Manage NuGet Packages
- Select Browse and search for
IronPdf.Linux
- Select the package and install it.
4. Modify FunctionHandler Code
The following example will create a PDF file from a webpage (IronPDF website) and save it to /tmp
. To view this PDF, you must upload it 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.
using System;
using Amazon.Lambda.Core;
using IronPdf;
// Handler function to render a webpage as PDF and save it
public Casing FunctionHandler(string input, ILambdaContext context)
{
try
{
context.Logger.LogLine($"START FunctionHandler RequestId: {context.AwsRequestId} Input: {input}");
// AWS temporary storage path
var awsTmpPath = @"/tmp/";
// Optional logging configuration for debugging
// IronPdf.Logging.Logger.EnableDebugging = true;
// IronPdf.Logging.Logger.LogFilePath = awsTmpPath;
// IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;
// Set your IronPDF license key
IronPdf.License.LicenseKey = "YOUR_LICENSE_KEY";
// Disable GPU usage in Chrome engine for AWS compatibility
IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled;
// Configure temporary paths for IronPDF
IronPdf.Installation.TempFolderPath = awsTmpPath;
IronPdf.Installation.CustomDeploymentDirectory = awsTmpPath;
// Automatically configure necessary Linux and Docker dependencies
IronPdf.Installation.LinuxAndDockerDependenciesAutoConfig = true;
context.Logger.LogLine($"Creating IronPdf.ChromePdfRenderer");
var renderer = new IronPdf.ChromePdfRenderer();
context.Logger.LogLine($"Rendering PDF from URL");
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 as: {fileName}");
pdfDoc.SaveAs(fileName);
// You can upload the saved PDF file to any location like S3.
context.Logger.LogLine($"COMPLETE!");
}
catch (Exception e)
{
context.Logger.LogLine($"[ERROR] FunctionHandler : {e.Message}");
}
// Return a casing object with both lower and upper version of input
return new Casing(input?.ToLower(), input?.ToUpper());
}
using System;
using Amazon.Lambda.Core;
using IronPdf;
// Handler function to render a webpage as PDF and save it
public Casing FunctionHandler(string input, ILambdaContext context)
{
try
{
context.Logger.LogLine($"START FunctionHandler RequestId: {context.AwsRequestId} Input: {input}");
// AWS temporary storage path
var awsTmpPath = @"/tmp/";
// Optional logging configuration for debugging
// IronPdf.Logging.Logger.EnableDebugging = true;
// IronPdf.Logging.Logger.LogFilePath = awsTmpPath;
// IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;
// Set your IronPDF license key
IronPdf.License.LicenseKey = "YOUR_LICENSE_KEY";
// Disable GPU usage in Chrome engine for AWS compatibility
IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled;
// Configure temporary paths for IronPDF
IronPdf.Installation.TempFolderPath = awsTmpPath;
IronPdf.Installation.CustomDeploymentDirectory = awsTmpPath;
// Automatically configure necessary Linux and Docker dependencies
IronPdf.Installation.LinuxAndDockerDependenciesAutoConfig = true;
context.Logger.LogLine($"Creating IronPdf.ChromePdfRenderer");
var renderer = new IronPdf.ChromePdfRenderer();
context.Logger.LogLine($"Rendering PDF from URL");
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 as: {fileName}");
pdfDoc.SaveAs(fileName);
// You can upload the saved PDF file to any location like S3.
context.Logger.LogLine($"COMPLETE!");
}
catch (Exception e)
{
context.Logger.LogLine($"[ERROR] FunctionHandler : {e.Message}");
}
// Return a casing object with both lower and upper version of input
return new Casing(input?.ToLower(), input?.ToUpper());
}
Imports System
Imports Amazon.Lambda.Core
Imports IronPdf
' Handler function to render a webpage as PDF and save it
Public Function FunctionHandler(ByVal input As String, ByVal context As ILambdaContext) As Casing
Try
context.Logger.LogLine($"START FunctionHandler RequestId: {context.AwsRequestId} Input: {input}")
' AWS temporary storage path
Dim awsTmpPath = "/tmp/"
' Optional logging configuration for debugging
' IronPdf.Logging.Logger.EnableDebugging = true;
' IronPdf.Logging.Logger.LogFilePath = awsTmpPath;
' IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;
' Set your IronPDF license key
IronPdf.License.LicenseKey = "YOUR_LICENSE_KEY"
' Disable GPU usage in Chrome engine for AWS compatibility
IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled
' Configure temporary paths for IronPDF
IronPdf.Installation.TempFolderPath = awsTmpPath
IronPdf.Installation.CustomDeploymentDirectory = awsTmpPath
' Automatically configure necessary Linux and Docker dependencies
IronPdf.Installation.LinuxAndDockerDependenciesAutoConfig = True
context.Logger.LogLine($"Creating IronPdf.ChromePdfRenderer")
Dim renderer = New IronPdf.ChromePdfRenderer()
context.Logger.LogLine($"Rendering PDF from URL")
Dim pdfDoc = renderer.RenderUrlAsPdf("https://ironpdf.com/")
Dim guid As System.Guid = System.Guid.NewGuid()
Dim fileName = $"/tmp/{input}_{guid}.pdf" ' Save file to /tmp
context.Logger.LogLine($"Saving PDF as: {fileName}")
pdfDoc.SaveAs(fileName)
' You can upload the saved PDF file to any location like S3.
context.Logger.LogLine($"COMPLETE!")
Catch e As Exception
context.Logger.LogLine($"[ERROR] FunctionHandler : {e.Message}")
End Try
' Return a casing object with both lower and upper version of input
Return New Casing(input?.ToLower(), input?.ToUpper())
End Function
5. Increase Memory and Timeout
IronPDF requires more time and memory than the default value of Lambda. You can configure it in aws-lambda-tools-defaults.json
. Please adjust these 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 AWS Lambda Memory Configuration Guide 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 AWS Lambda Console or via Visual Studio by using the AWS Toolkit for Visual Studio.
Frequently Asked Questions
What is the purpose of using IronPDF on AWS Lambda?
IronPDF is used on AWS Lambda to programmatically create PDF files in a C# environment hosted on Amazon AWS.
How do I set up an AWS Lambda function with a .NET container template?
You can set up an AWS Lambda function using a .NET container template by following AWS's official documentation on .NET 5 AWS Lambda Support with Container Images.
What package dependencies are needed for using IronPDF on AWS Lambda?
You need to install several packages required for Chrome, including atk, cups-libs, and libXcomposite, among others, to use IronPDF on AWS Lambda.
How do you install the IronPDF NuGet package in a .NET project?
In Solution Explorer, right-click References, select Manage NuGet Packages, search for 'IronPdf.Linux', and install it.
How can I modify the FunctionHandler code to create a PDF in AWS Lambda?
You need to configure the temporary folder path, set the IronPDF license key, disable GPU usage, and use the IronPdf.ChromePdfRenderer to render a webpage as a PDF.
What changes are needed in AWS Lambda configuration for IronPDF?
Increase the memory size to 512 MB and timeout to 330 seconds in the aws-lambda-tools-defaults.json file for IronPDF.
How can I publish the AWS Lambda function with IronPDF?
Follow the instructions in the AWS documentation for publishing and testing your Lambda function with .NET 5 container images.
How do I activate the Lambda function using Visual Studio?
You can activate the Lambda function via Visual Studio using the AWS Toolkit for Visual Studio.
Why is it necessary to configure temporary paths in IronPDF on AWS Lambda?
Configuring temporary paths is necessary because AWS Lambda functions use a specific temporary storage directory, and IronPDF requires setting TempFolderPath and CustomDeploymentDirectory for compatibility.
Can I use IronPDF with different .NET versions on AWS Lambda?
Yes, IronPDF can be used with different .NET versions on AWS Lambda, such as .NET 5, .NET 7, and .NET 8, by modifying the Dockerfile accordingly.