如何在 AWS Lambda 上執行和部署 IronPDF .NET

This article was translated from English: Does it need improvement?
Translated
View the article in English
Amazon Web Services V1 related to 如何在 AWS Lambda 上執行和部署 IronPDF .NET

1. 使用容器範本 (.NET 5) 建立 AWS Lambda 函數

請參閱 AWS 官方文件的第一部分: .NET 5 AWS Lambda 對容器鏡像的支援

2. 新增套件依賴項

這些相依性是 Chrome 在此 AWS 環境中運作所必需的。

請依照以下說明修改 Docker 檔案:

AWS Lambda 與 .NET 5

AWS Lambda 與 .NET 7

AWS Lambda 與 .NET 8

3. 新增 IronPDF (Linux) NuGet 包

安裝IronPdf.Linux

  1. 在解決方案資源管理器中,以滑鼠右鍵按一下"參考",然後選擇"管理 NuGet 套件"。
  2. 選擇"瀏覽"並搜尋IronPdf.Linux
  3. 選擇軟體包並安裝。

4. 修改 FunctionHandler 程式碼

本範例將從網頁建立 PDF 檔案。https://ironpdf.com/並將其儲存到/tmp目錄。 要查看此 PDF 文件,您必須將其上傳到其他服務,例如 S3。

在 AWS Lambda 上使用 IronPDF 時,需要設定臨時資料夾。 請使用TempFolderPathCustomDeploymentDirectory屬性來實現此目的。

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. 增加記憶體和超時時間

IronPDF 需要比 Lambda 的預設值更多的時間和記憶體。 您可以在aws-lambda-tools-defaults.json中進行配置。 請根據您的功能進行調整。 在這個範例中,我們將它設定為 512 (MB) 和 330 (秒)。

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

您也可以使用 Lambda 控制台更新此設定。 請參閱"配置 AWS Lambda 函數"一文以了解更多資訊。

6. 發布

請依照" .NET 5 AWS Lambda 容器鏡像支援"文件後半部的說明發佈和測試您的 Lambda 函數。

7. 試試看!

您可以使用Lambda 控制台或透過 Visual Studio 使用AWS Toolkit for Visual Studio來啟動 Lambda 函數。

常見問題解答

如何使用 .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,685,821 | 版本: 2025.12 剛發表