如何在 AWS Lambda 上執行與部署 IronPDF for .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 for .NET

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

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

2. 新增套件依賴項

在這個 AWS 環境中,Chrome 需要這些依賴項。

請根據以下說明修改 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 程式碼

此範例將從網頁https://ironpdf.com/建立 PDF 檔案,並儲存至 /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 函式從網址建立 PDF 檔案?

若要在 .NET AWS Lambda 函式中透過網址建立 PDF,請使用 IronPDF 的 ChromePdfRenderer.RenderUrlAsPdf 方法。請確保已透過 TempFolderPathCustomDeploymentDirectory 屬性設定好暫存資料夾。

我該採取哪些步驟來在 AWS Lambda 上設定 IronPDF?

透過設定 TempFolderPathCustomDeploymentDirectory 來設定暫存資料夾路徑、使用 ChromeGpuModes.Disabled 停用 GPU,並將 LinuxAndDockerDependenciesAutoConfig 設為 true 以啟用 Linux 和 Docker 依賴項的自動配置,即可在 AWS Lambda 上設定 IronPDF。

為何調整 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 的 AWS Toolkit 來啟用並測試您的 AWS Lambda 函式,該工具可讓您直接在 IDE 中進行無縫部署與測試。

在 AWS Lambda 上使用 .NET 5 容器範本有何意義?

在 AWS Lambda 上使用 .NET 5 容器範本具有重要意義,因為它能協助部署應用程式及其依賴項(例如 IronPDF),確保跨環境的一致性。

如何在 AWS Lambda 函式中使用 IronPDF 處理例外情況?

在您的 AWS Lambda 函式中處理例外狀況時,請將 IronPDF 操作封裝在 try-catch 區塊中,並使用 context.Logger.LogLine 記錄錯誤以利除錯與疑難排解。

Curtis Chau
技術撰稿人

Curtis Chau 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

除了開發工作之外,Curtis 對物聯網(IoT)抱有濃厚興趣,致力於探索整合硬體與軟體的創新方法。閒暇時,他喜歡玩遊戲和開發 Discord 機器人,將對科技的熱愛與創意相結合。

準備開始了嗎?
Nuget 下載 19,014,616 | 版本: 2026.5 just released
Still Scrolling Icon

還在往下捲動嗎?

想要快速確認成果嗎? PM > Install-Package IronPdf
執行範例 觀看您的 HTML 轉為 PDF。