如何在 AWS Lambda 上執行和部署 IronPDF .NET
1. 使用容器範本創建AWS Lambda
使用Amazon.Lambda.Templates NuGet套件以腳手架化一個基於容器的Lambda項目(dotnet new lambda.image.EmptyFunction),遵循AWS當前指南:使用容器映像部署.NET Lambda函數。 AWS目前維護的基礎映像包含.NET 8、9和10 - 請選擇下列與您的目標架構相匹配的Dockerfile。
2. 新增套件依賴項
這些相依性是 Chrome 在此 AWS 環境中運作所必需的。
請依照以下說明修改 Docker 檔案:
AWS Lambda 與 .NET 5
AWS Lambda 與 .NET 7
AWS Lambda 與 .NET 8
public.ecr.aws/lambda/dotnet:8基底映像檔(Amazon Linux 2023)上,Docker 構建失敗,因為xorg-x11-utils現已被xprop淘汰,導致dnf依賴解決衝突。 在依賴安裝行中,用xprop替換xorg-x11-utils來修復它。3. 新增 IronPDF (Linux) NuGet 包
安裝IronPdf.Linux
- 在解決方案資源管理器中,以滑鼠右鍵按一下“參考”,然後選擇“管理 NuGet 套件”。
- 選擇瀏覽並搜尋
IronPdf.Linux。 - 選擇軟體包並安裝。
4. 修改 FunctionHandler 程式碼
本範例將從網頁https://ironpdf.com/創建一個PDF文件並將其保存到/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 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 FunctionPublic 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 Function5. 增加記憶體和超時時間
IronPDF 需要比 Lambda 的預設值更多的時間和記憶體。 您可以在aws-lambda-tools-defaults.json中配置它。 請根據您的功能進行調整。 在這個範例中,我們將它設定為 512 (MB) 和 330 (秒)。
{
"function-memory-size": 512,
"function-timeout": 330
}
您也可以使用 Lambda 控制台更新此設定。 導航至配置AWS Lambda函數文章以獲取更多信息。
6. 發布
使用dotnet lambda deploy-function來構建Docker映像,將其推送至Amazon ECR,並部署Lambda函數。 請參閱AWS的使用容器映像部署.NET Lambda函數指南中的“使用基礎映像創建和部署映像”部分以獲取完整的發布和測試工作流,包括dotnet lambda invoke-function進行測試。
7. 試試看!
您可以使用Lambda控制台或通過使用Visual Studio的AWS Toolkit激活Lambda函數。
常見問題
我如何在AWS Lambda上部署IronPDF?
要在AWS Lambda上部署IronPDF,您需要使用Amazon.Lambda.Templates NuGet套件創建一個帶有容器模板的AWS Lambda函數。在設置您的專案後,添加必要的依賴項,包括IronPDF (Linux) NuGet包,並配置您的函數以使用IronPDF處理PDF生成。
使用IronPDF與AWS Lambda需要哪些先決條件?
在使用IronPDF與AWS Lambda之前,您需要在AWS環境中安裝Chrome的依賴項,並確保已設置正確的.NET框架(使用AWS支援的基礎映像)。另外,加入IronPDF (Linux) NuGet包以管理PDF操作。
如何在AWS Lambda上配置IronPDF的臨時存儲?
您可以透過在代碼中設置TempFolderPath和CustomDeploymentDirectory屬性為AWS臨時存儲位置,通常為‘/tmp’,來配置IronPDF在AWS Lambda上的臨時存儲。
我需要為了讓IronPDF在AWS上運行做任何特定設置嗎?
是的,您必須將IronPdf.Installation.TempFolderPath和IronPdf.Installation.CustomDeploymentDirectory設置為AWS的臨時目錄,並禁用Chrome渲染的GPU功能,因為Lambda運行在無頭環境中。
IronPDF在AWS Lambda上需要哪些內存和超時設置?
IronPDF需要比預設設置更多的內存和超時。建議將內存設置為512 MB,超時設置為330秒。這些可以在aws-lambda-tools-defaults.json文件中或通過AWS Lambda控制台進行配置。
部署後如何測試我的IronPDF Lambda函數?
在部署您的IronPDF Lambda函式後,您可以使用AWS Lambda主控台或透過Visual Studio使用AWS Toolkit for Visual Studio進行測試。此外,還可以使用dotnet lambda invoke-function指令進行測試。
為什麼在使用AWS Lambda時應禁用IronPDF的GPU模式?
在AWS Lambda上使用IronPDF時,禁用GPU模式是必要的,因為Lambda函數環境是無頭的,且GPU渲染不可用。使用IronPdf.Engines.Chrome.ChromeGpuModes.Disabled設置。
如何確保我的Docker依賴項已正確配置以供IronPDF使用?
IronPDF提供了IronPdf.Installation.LinuxAndDockerDependenciesAutoConfig進行Linux和Docker依賴項的自動配置。啟用這個選項有助於在AWS Lambda的Docker環境中高效管理依賴項。
在AWS中構建和部署具有IronPDF功能的步驟是什麼?
若要在AWS上建立和部署具有IronPDF的函式,使用dotnet lambda deploy-function來建立Docker映像,將其推送至Amazon ECR,並部署您的函式。請遵循AWS的部署指南,以便從構建到測試的完整工作流程。
我能使用AWS S3與IronPDF一起存儲生成的PDF嗎?
可以,使用IronPDF在AWS Lambda上生成PDF後,您可以將它們上傳到AWS S3進行存儲。這通常涉及使用AWS SDK或其他工具,將文件從Lambda的臨時存儲轉移到S3中。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。