如何在 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
- 在解決方案資源管理器中,以滑鼠右鍵按一下"參考",然後選擇"管理 NuGet 套件"。
- 選擇"瀏覽"並搜尋
IronPdf.Linux。 - 選擇軟體包並安裝。
4. 修改 FunctionHandler 程式碼
本範例將從網頁建立 PDF 檔案。https://ironpdf.com/並將其儲存到/tmp目錄。 要查看此 PDF 文件,您必須將其上傳到其他服務,例如 S3。
在 AWS Lambda 上使用 IronPDF 時,需要設定臨時資料夾。 請使用TempFolderPath和CustomDeploymentDirectory屬性來實現此目的。
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());
}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方法。請確保已使用TempFolderPath和CustomDeploymentDirectory屬性配置臨時資料夾。
如何在AWS Lambda上配置IronPDF?
透過使用TempFolderPath和CustomDeploymentDirectory設定臨時資料夾路徑,使用ChromeGpuModes.Disabled停用 GPU,以及將LinuxAndDockerDependenciesAutoConfig設定為 true 來啟用 Linux 和 Docker 依賴項的自動配置,從而在 AWS Lambda 上配置 IronPDF AWS。
為什麼調整 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 for Visual Studio 啟動和測試您的 AWS Lambda 函數,從而實現直接從 IDE 無縫部署和測試。
在 AWS Lambda 上使用 .NET 5 容器範本有何意義?
在 AWS Lambda 上使用 .NET 5 容器範本意義重大,因為它有助於部署應用程式及其相依性(例如 IronPDF),從而確保跨環境的一致性。
如何使用 IronPDF 處理 AWS Lambda 函數中的異常?
在 AWS Lambda 函數中處理異常時,請將 IronPDF 操作包裝在 try-catch 區塊中,並使用context.Logger.LogLine記錄錯誤以進行偵錯和故障排除。






