如何在 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
- 在"解決方案總覽"中,右鍵點擊"參考項目",選擇"管理 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 Function5. 增加記憶體與超時設定
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屬性配置。
我應該採取哪些步驟來配置IronPDF在AWS Lambda上?
通過設置臨時文件夾路徑TempFolderPath和CustomDeploymentDirectory,禁用GPU使用ChromeGpuModes.Disabled,並將LinuxAndDockerDependenciesAutoConfig設為true啟用Linux和Docker依賴項的自動配置來配置IronPDF。
為什麼調整AWS Lambda的記憶體和超時設定很重要?
調整AWS Lambda的記憶體和超時設定至關重要,因為IronPDF比預設設定需要更多資源。這可以在aws-lambda-tools-defaults.json或AWS Lambda主控台中配置。
如何確保Chrome依賴項正確安裝於AWS Lambda?
確保Chrome依賴項正確安裝,方法是根據文章中提供的指示按您的.NET版本修改Dockerfile。這包括在AWS環境安裝Chrome所需的Linux套件。
我可以使用Visual Studio激活並測試我的AWS Lambda功能嗎?
可以,您可以使用Visual Studio的AWS工具包激活並測試您的AWS Lambda功能,這允許在IDE中直接進行無縫部署和測試。
在AWS Lambda上使用.NET 5的容器範本有何意義?
在AWS Lambda上使用.NET 5的容器範本很重要,因為它有助於應用程式及其依賴項的部署,如IronPDF,確保跨環境一致性。
如何使用IronPDF在AWS Lambda功能中處理例外?
通過將您的IronPDF操作包裹在try-catch塊中處理AWS Lambda功能中的例外,並使用context.Logger.LogLine記錄錯誤以進行除錯和故障排除。
How can I ensure my Docker dependencies are correctly configured for IronPDF?
IronPDF offers an automatic configuration for Linux and Docker dependencies using IronPdf.Installation.LinuxAndDockerDependenciesAutoConfig. Enabling this option helps manage dependencies efficiently in a dockerized AWS Lambda environment.
What are the steps for building and deploying a function with IronPDF on AWS?
To build and deploy a function with IronPDF on AWS, use `dotnet lambda deploy-function` to create a Docker image, push it to Amazon ECR, and deploy your function. Follow AWS’s deployment guide for a complete workflow from building to testing.
Can I use AWS S3 with IronPDF for storing generated PDFs?
Yes, after generating PDFs using IronPDF on AWS Lambda, you can upload them to AWS S3 for storage. This typically involves using AWS SDK or other tools to transfer files from Lambda’s temporary storage to S3.

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。