How to Run & Deploy IronPDF .NET on AWS Lambda

This article was translated from English: Does it need improvement?
Translated
View the article in English
class="container-fluid">
class="row">
class="col-md-2"> Amazon Web Services V1 related to How to Run & Deploy IronPDF .NET on AWS Lambda

1. 使用容器模板 (.NET 5) 創建AWS Lambda

請參考AWS的這份官方文件的第一部分:.NET 5 AWS Lambda支持帶有容器圖像

2. 添加套件依賴

這些依賴是AWS環境中Chrome所需的。

請根據這些指引修改Docker文件:

AWS Lambda與.NET 5

AWS Lambda with .NET 7

AWS Lambda with .NET 8

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 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.

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

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

        // [Optional] Enable logging for debugging
        // Uncomment these lines if you encounter issues
        // 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 for Chrome rendering in headless environments
        IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled;

        // Configure IronPDF to use the AWS temporary directory
        IronPdf.Installation.TempFolderPath = awsTmpPath;
        IronPdf.Installation.CustomDeploymentDirectory = awsTmpPath;

        // Automatically configure Linux and Docker dependencies
        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 with name: {fileName}");
        pdfDoc.SaveAs(fileName);

        // Place for future code to upload the PDF file to a service like AWS 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 location

        // [Optional] Enable logging for debugging
        // Uncomment these lines if you encounter issues
        // 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 for Chrome rendering in headless environments
        IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled;

        // Configure IronPDF to use the AWS temporary directory
        IronPdf.Installation.TempFolderPath = awsTmpPath;
        IronPdf.Installation.CustomDeploymentDirectory = awsTmpPath;

        // Automatically configure Linux and Docker dependencies
        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 with name: {fileName}");
        pdfDoc.SaveAs(fileName);

        // Place for future code to upload the PDF file to a service like AWS S3.

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

    return new Casing(input?.ToLower(), input?.ToUpper());
}
Public Function FunctionHandler(ByVal input As String, ByVal context As ILambdaContext) As Casing
	Try
		context.Logger.LogLine($"START FunctionHandler RequestId: {context.AwsRequestId} Input: {input}")

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

		' [Optional] Enable logging for debugging
		' Uncomment these lines if you encounter issues
		' 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 for Chrome rendering in headless environments
		IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled

		' Configure IronPDF to use the AWS temporary directory
		IronPdf.Installation.TempFolderPath = awsTmpPath
		IronPdf.Installation.CustomDeploymentDirectory = awsTmpPath

		' Automatically configure Linux and Docker dependencies
		IronPdf.Installation.LinuxAndDockerDependenciesAutoConfig = True

		context.Logger.LogLine($"Creating IronPdf.ChromePdfRenderer")
		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" ' Save file to /tmp

		context.Logger.LogLine($"Saving PDF with name: {fileName}")
		pdfDoc.SaveAs(fileName)

		' Place for future code to upload the PDF file to a service like AWS S3.

		context.Logger.LogLine($"COMPLETE!")
	Catch e As Exception
		context.Logger.LogLine($"[ERROR] FunctionHandler : {e.Message}")
	End Try

	Return New Casing(input?.ToLower(), input?.ToUpper())
End Function
$vbLabelText   $csharpLabel

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 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.

常見問題解答

如何使用 .NET AWS Lambda 函數從 URL 創建 PDF?

要在 .NET AWS Lambda 函數中從 URL 創建 PDF,請使用 IronPDF 的 ChromePdfRenderer.RenderUrlAsPdf 方法。確認使用 TempFolderPathCustomDeploymentDirectory 屬性配置臨時文件夾。

我應該採取哪些步驟來配置 AWS Lambda 上的 IronPDF?

通過設置臨時文件夾路徑使用 TempFolderPathCustomDeploymentDirectory 在 AWS Lambda 上配置 IronPDF,禁用 GPU 使用 ChromeGpuModes.Disabled,並通過將 LinuxAndDockerDependenciesAutoConfig 設置為 true 啟用 Linux 和 Docker 依賴項的自動配置。

為什麼調整 AWS Lambda 的記憶體和超時設置很重要?

調整 AWS Lambda 的記憶體和超時設置很重要,因為 IronPDF 需要比默認設置更多的資源。這可以在 aws-lambda-tools-defaults.json 中配置,或通過 AWS Lambda 主控台配置。

如何確保正確安裝 AWS Lambda 的 Chrome 依賴項?

通過根據文章中提供的特定於您 .NET 版本的說明修改 Dockerfile 來確保正確安裝 Chrome 依賴項。這包括在 AWS 環境中安裝 Chrome 所需的 Linux 套件。

我可以使用 Visual Studio 啟用並測試我的 AWS Lambda 函數嗎?

是的,您可以使用 Visual Studio 通過 Visual Studio 的 AWS Toolkit 啟用並測試您的 AWS Lambda 函數,這允許直接從 IDE 無縫部署和測試。

在 AWS Lambda 上使用 .NET 5 容器模板有何重要意義?

在 AWS Lambda 上使用 .NET 5 容器模板具有重要意義,因為它促進了應用程序及其依賴項(如 IronPDF)的部署,確保跨環境的一致性。

如何在使用 IronPDF 的 AWS Lambda 函數中處理異常?

使用 try-catch 塊封裝 IronPDF 操作處理 AWS Lambda 函數中的異常,使用 context.Logger.LogLine 記錄錯誤以進行調試和故障排除。

Curtis Chau
技術作家

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

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

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