AWS Lambda IronPDF on Amazon Linux 2

This article was translated from English: Does it need improvement?
Translated
View the article in English

AWS Lambda / Amazon Linux 2 (以管理員/root 身份安裝 IronPdf.Linux)

此信息也顯示在我們的網站上 - AWS Lambda IronPDF 指南

AWS 說明

  1. 查看 AWS .NET 5 Lambda 支持容器映像
  2. 創建並使用以下 Dockerfile:
# Use the .NET 5 AWS Lambda runtime as the base image
FROM public.ecr.aws/lambda/dotnet:5.0
WORKDIR /var/task

# Install the necessary dependencies for IronPDF
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

# This COPY command copies the .NET Lambda project's build artifacts from the host machine into the image.
COPY "bin/Release/lambda-publish" .
  1. IronPdf.Linux 套件添加到您的解決方案中。
  2. 修改 _FunctionHandler 代碼 - 此示例將從網頁創建 PDF (IronPDF) 並將其保存到 /tmp。 為了查看這個 PDF,必須將其上傳到其他服務,如 S3。(這裡有官方的 AWS 示例 - AWS S3 基礎知識
// Define the function handler for AWS Lambda
public Casing FunctionHandler(string input, ILambdaContext context)
{
    try
    {
        // Start logging the function process with input details
        context.Logger.LogLine($"START FunctionHandler RequestId: {context.AwsRequestId} Input: {input}");

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

        // Optional: Enable debugging for IronPdf
        // IronPdf.Logging.Logger.EnableDebugging = true;
        // IronPdf.Logging.Logger.LogFilePath = awsTmpPath;
        // IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;

        // Set your license key for IronPDF
        IronPdf.License.LicenseKey = "YOUR_LICENSE_KEY";

        // Configuration for IronPDF rendering
        IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled;
        IronPdf.Installation.DefaultRenderingEngine = IronPdf.Rendering.PdfRenderingEngine.Chrome;
        Environment.SetEnvironmentVariable("TEMP", awsTmpPath, EnvironmentVariableTarget.Process);
        Environment.SetEnvironmentVariable("TMP", awsTmpPath, EnvironmentVariableTarget.Process);
        IronPdf.Installation.TempFolderPath = awsTmpPath;
        IronPdf.Installation.CustomDeploymentDirectory = awsTmpPath;
        IronPdf.Installation.LinuxAndDockerDependenciesAutoConfig = true;

        context.Logger.LogLine($"Creating IronPdf.ChromePdfRenderer");

        // Create instance of ChromePdfRenderer and render PDF from URL
        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"; // Name for the PDF file

        // Save the rendered PDF document
        context.Logger.LogLine($"Saving PDF name : {fileName}");
        pdfDoc.SaveAs(fileName);

        // Here you can upload the saved PDF file to anywhere such as AWS S3.
        context.Logger.LogLine($"COMPLETE!");
    }
    catch (Exception e)
    {
        // Log errors if any occur
        context.Logger.LogLine($"[ERROR] FunctionHandler : {e.Message}");
    }

    // Return a new instance of Casing with lower and upper case input strings
    return new Casing(input?.ToLower(), input?.ToUpper());
}

// Casing record to hold lower and upper case strings
public record Casing(string Lower, string Upper);
// Define the function handler for AWS Lambda
public Casing FunctionHandler(string input, ILambdaContext context)
{
    try
    {
        // Start logging the function process with input details
        context.Logger.LogLine($"START FunctionHandler RequestId: {context.AwsRequestId} Input: {input}");

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

        // Optional: Enable debugging for IronPdf
        // IronPdf.Logging.Logger.EnableDebugging = true;
        // IronPdf.Logging.Logger.LogFilePath = awsTmpPath;
        // IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;

        // Set your license key for IronPDF
        IronPdf.License.LicenseKey = "YOUR_LICENSE_KEY";

        // Configuration for IronPDF rendering
        IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled;
        IronPdf.Installation.DefaultRenderingEngine = IronPdf.Rendering.PdfRenderingEngine.Chrome;
        Environment.SetEnvironmentVariable("TEMP", awsTmpPath, EnvironmentVariableTarget.Process);
        Environment.SetEnvironmentVariable("TMP", awsTmpPath, EnvironmentVariableTarget.Process);
        IronPdf.Installation.TempFolderPath = awsTmpPath;
        IronPdf.Installation.CustomDeploymentDirectory = awsTmpPath;
        IronPdf.Installation.LinuxAndDockerDependenciesAutoConfig = true;

        context.Logger.LogLine($"Creating IronPdf.ChromePdfRenderer");

        // Create instance of ChromePdfRenderer and render PDF from URL
        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"; // Name for the PDF file

        // Save the rendered PDF document
        context.Logger.LogLine($"Saving PDF name : {fileName}");
        pdfDoc.SaveAs(fileName);

        // Here you can upload the saved PDF file to anywhere such as AWS S3.
        context.Logger.LogLine($"COMPLETE!");
    }
    catch (Exception e)
    {
        // Log errors if any occur
        context.Logger.LogLine($"[ERROR] FunctionHandler : {e.Message}");
    }

    // Return a new instance of Casing with lower and upper case input strings
    return new Casing(input?.ToLower(), input?.ToUpper());
}

// Casing record to hold lower and upper case strings
public record Casing(string Lower, string Upper);
' Define the function handler for AWS Lambda
Public Function FunctionHandler(ByVal input As String, ByVal context As ILambdaContext) As Casing
	Try
		' Start logging the function process with input details
		context.Logger.LogLine($"START FunctionHandler RequestId: {context.AwsRequestId} Input: {input}")

		Dim awsTmpPath = "/tmp/" ' AWS temporary storage path

		' Optional: Enable debugging for IronPdf
		' IronPdf.Logging.Logger.EnableDebugging = true;
		' IronPdf.Logging.Logger.LogFilePath = awsTmpPath;
		' IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;

		' Set your license key for IronPDF
		IronPdf.License.LicenseKey = "YOUR_LICENSE_KEY"

		' Configuration for IronPDF rendering
		IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled
		IronPdf.Installation.DefaultRenderingEngine = IronPdf.Rendering.PdfRenderingEngine.Chrome
		Environment.SetEnvironmentVariable("TEMP", awsTmpPath, EnvironmentVariableTarget.Process)
		Environment.SetEnvironmentVariable("TMP", awsTmpPath, EnvironmentVariableTarget.Process)
		IronPdf.Installation.TempFolderPath = awsTmpPath
		IronPdf.Installation.CustomDeploymentDirectory = awsTmpPath
		IronPdf.Installation.LinuxAndDockerDependenciesAutoConfig = True

		context.Logger.LogLine($"Creating IronPdf.ChromePdfRenderer")

		' Create instance of ChromePdfRenderer and render PDF from URL
		Dim renderer = New IronPdf.ChromePdfRenderer()
		context.Logger.LogLine($"Rendering PDF")
		Dim pdfDoc = renderer.RenderUrlAsPdf("https://ironpdf.com/")
		Dim guid As System.Guid = System.Guid.NewGuid()
		Dim fileName = $"/tmp/{input}_{guid}.pdf" ' Name for the PDF file

		' Save the rendered PDF document
		context.Logger.LogLine($"Saving PDF name : {fileName}")
		pdfDoc.SaveAs(fileName)

		' Here you can upload the saved PDF file to anywhere such as AWS S3.
		context.Logger.LogLine($"COMPLETE!")
	Catch e As Exception
		' Log errors if any occur
		context.Logger.LogLine($"[ERROR] FunctionHandler : {e.Message}")
	End Try

	' Return a new instance of Casing with lower and upper case input strings
	Return New Casing(input?.ToLower(), input?.ToUpper())
End Function

' Casing record to hold lower and upper case strings
'INSTANT VB TODO TASK: C# 'records' are not converted by Instant VB:
'public record Casing(string Lower, string Upper)
$vbLabelText   $csharpLabel
  1. 增加內存和超時 - IronPDF 需要比 Lambda 的默認值更多的時間和內存。 這可以在 aws-lambda-tools-defaults.json 中配置。 調整這些值以符合您功能的需求。

在此示例中,我們設置為 512 (MB) 和 330 (秒):

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

也可以通過 Lambda 控制台更新配置 - AWS Lambda 記憶體配置

  1. 請遵循文檔的最後部分來發布並嘗試 Lambda 功能 AWS .NET 5 Lambda 支持容器映像
  2. Lambda function can also be invoked using the Lambda console, AWS Lambda Console or invoked via Visual Studio by using the AWS Toolkit for Visual Studio.

如果您收到“GPU 進程無法使用”的信息,則可能是未以管理員身份運行安裝。 請嘗試以管理員/root 權限再次運行軟件。 或者,在您的 Dockerfile 中添加以下內容:

RUN chmod 755 "runtimes/linux-x64/native/IronCefSubprocess"
Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 16,154,058 | 版本: 2025.11 剛剛發布