AWS Lambda IronPDF 於 Amazon Linux 2
This article was translated from English: Does it need improvement?
Translated
View the article in English
AWS Lambda / Amazon Linux 2(請以管理員/root 身分安裝 IronPdf.Linux)
此資訊亦刊載於我們的網站 - AWS Lambda IronPDF 指南
AWS 說明
- 參閱 AWS .NET 5 Lambda 支援與容器映像檔
- 建立並使用以下 Dockerfile:
# Use the .NET 5 AWS Lambda runtime as the base image
FROM public.ecr.aws/lambda/dotnet:5.0
WORKDIR /var/task
# Install the necessary dependencies for IronPDF
RUN yum install -y pango.x86_64 \
libXcomposite.x86_64 \
libXcursor.x86_64 \
libXdamage.x86_64 \
libXext.x86_64 \
libXi.x86_64 \
libXtst.x86_64 \
cups-libs.x86_64 \
libXScrnSaver.x86_64 \
libXrandr.x86_64 \
GConf2.x86_64 \
alsa-lib.x86_64 \
atk.x86_64 \
gtk3.x86_64 \
ipa-gothic-fonts \
xorg-x11-fonts-100dpi \
xorg-x11-fonts-75dpi \
xorg-x11-utils \
xorg-x11-fonts-cyrillic \
xorg-x11-fonts-Type1 \
xorg-x11-fonts-misc \
glibc-devel.x86_64 \
at-spi2-atk.x86_64 \
mesa-libgbm.x86_64
# This COPY command copies the .NET Lambda project's build artifacts from the host machine into the image.
COPY "bin/Release/lambda-publish" .
- 將 IronPdf.Linux 套件新增至您的解決方案中。
- 修改
_FunctionHandler程式碼 - 此範例將從網頁(IronPDF)建立 PDF 檔案,並儲存至/tmp。 若要檢視此 PDF 檔案,必須將其上傳至其他服務,例如 S3。(官方 AWS 範例請參閱此處 - AWS S3 基礎知識)
// Define the function handler for AWS Lambda
public Casing FunctionHandler(string input, ILambdaContext context)
{
try
{
// Start logging the function process with input details
context.Logger.LogLine($"START FunctionHandler RequestId: {context.AwsRequestId} Input: {input}");
var awsTmpPath = @"/tmp/"; // AWS temporary storage path
// Optional: Enable debugging for IronPdf
// IronPdf.Logging.Logger.EnableDebugging = true;
// IronPdf.Logging.Logger.LogFilePath = awsTmpPath;
// IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;
// Set your license key for IronPDF
IronPdf.License.LicenseKey = "YOUR_LICENSE_KEY";
// Configuration for IronPDF rendering
IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled;
IronPdf.Installation.DefaultRenderingEngine = IronPdf.Rendering.PdfRenderingEngine.Chrome;
Environment.SetEnvironmentVariable("TEMP", awsTmpPath, EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("TMP", awsTmpPath, EnvironmentVariableTarget.Process);
IronPdf.Installation.TempFolderPath = awsTmpPath;
IronPdf.Installation.CustomDeploymentDirectory = awsTmpPath;
IronPdf.Installation.LinuxAndDockerDependenciesAutoConfig = true;
context.Logger.LogLine($"Creating IronPdf.ChromePdfRenderer");
// Create instance of ChromePdfRenderer and render PDF from URL
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"; // Name for the PDF file
// Save the rendered PDF document
context.Logger.LogLine($"Saving PDF name : {fileName}");
pdfDoc.SaveAs(fileName);
// Here you can upload the saved PDF file to anywhere such as AWS S3.
context.Logger.LogLine($"COMPLETE!");
}
catch (Exception e)
{
// Log errors if any occur
context.Logger.LogLine($"[ERROR] FunctionHandler : {e.Message}");
}
// Return a new instance of Casing with lower and upper case input strings
return new Casing(input?.ToLower(), input?.ToUpper());
}
// Casing record to hold lower and upper case strings
public record Casing(string Lower, string Upper);
// Define the function handler for AWS Lambda
public Casing FunctionHandler(string input, ILambdaContext context)
{
try
{
// Start logging the function process with input details
context.Logger.LogLine($"START FunctionHandler RequestId: {context.AwsRequestId} Input: {input}");
var awsTmpPath = @"/tmp/"; // AWS temporary storage path
// Optional: Enable debugging for IronPdf
// IronPdf.Logging.Logger.EnableDebugging = true;
// IronPdf.Logging.Logger.LogFilePath = awsTmpPath;
// IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;
// Set your license key for IronPDF
IronPdf.License.LicenseKey = "YOUR_LICENSE_KEY";
// Configuration for IronPDF rendering
IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled;
IronPdf.Installation.DefaultRenderingEngine = IronPdf.Rendering.PdfRenderingEngine.Chrome;
Environment.SetEnvironmentVariable("TEMP", awsTmpPath, EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("TMP", awsTmpPath, EnvironmentVariableTarget.Process);
IronPdf.Installation.TempFolderPath = awsTmpPath;
IronPdf.Installation.CustomDeploymentDirectory = awsTmpPath;
IronPdf.Installation.LinuxAndDockerDependenciesAutoConfig = true;
context.Logger.LogLine($"Creating IronPdf.ChromePdfRenderer");
// Create instance of ChromePdfRenderer and render PDF from URL
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"; // Name for the PDF file
// Save the rendered PDF document
context.Logger.LogLine($"Saving PDF name : {fileName}");
pdfDoc.SaveAs(fileName);
// Here you can upload the saved PDF file to anywhere such as AWS S3.
context.Logger.LogLine($"COMPLETE!");
}
catch (Exception e)
{
// Log errors if any occur
context.Logger.LogLine($"[ERROR] FunctionHandler : {e.Message}");
}
// Return a new instance of Casing with lower and upper case input strings
return new Casing(input?.ToLower(), input?.ToUpper());
}
// Casing record to hold lower and upper case strings
public record Casing(string Lower, string Upper);
' Define the function handler for AWS Lambda
Public Function FunctionHandler(ByVal input As String, ByVal context As ILambdaContext) As Casing
Try
' Start logging the function process with input details
context.Logger.LogLine($"START FunctionHandler RequestId: {context.AwsRequestId} Input: {input}")
Dim awsTmpPath = "/tmp/" ' AWS temporary storage path
' Optional: Enable debugging for IronPdf
' IronPdf.Logging.Logger.EnableDebugging = true;
' IronPdf.Logging.Logger.LogFilePath = awsTmpPath;
' IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;
' Set your license key for IronPDF
IronPdf.License.LicenseKey = "YOUR_LICENSE_KEY"
' Configuration for IronPDF rendering
IronPdf.Installation.ChromeGpuMode = IronPdf.Engines.Chrome.ChromeGpuModes.Disabled
IronPdf.Installation.DefaultRenderingEngine = IronPdf.Rendering.PdfRenderingEngine.Chrome
Environment.SetEnvironmentVariable("TEMP", awsTmpPath, EnvironmentVariableTarget.Process)
Environment.SetEnvironmentVariable("TMP", awsTmpPath, EnvironmentVariableTarget.Process)
IronPdf.Installation.TempFolderPath = awsTmpPath
IronPdf.Installation.CustomDeploymentDirectory = awsTmpPath
IronPdf.Installation.LinuxAndDockerDependenciesAutoConfig = True
context.Logger.LogLine($"Creating IronPdf.ChromePdfRenderer")
' Create instance of ChromePdfRenderer and render PDF from URL
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" ' Name for the PDF file
' Save the rendered PDF document
context.Logger.LogLine($"Saving PDF name : {fileName}")
pdfDoc.SaveAs(fileName)
' Here you can upload the saved PDF file to anywhere such as AWS S3.
context.Logger.LogLine($"COMPLETE!")
Catch e As Exception
' Log errors if any occur
context.Logger.LogLine($"[ERROR] FunctionHandler : {e.Message}")
End Try
' Return a new instance of Casing with lower and upper case input strings
Return New Casing(input?.ToLower(), input?.ToUpper())
End Function
' Casing record to hold lower and upper case strings
'INSTANT VB TODO TASK: C# 'records' are not converted by Instant VB:
'public record Casing(string Lower, string Upper)
$vbLabelText
$csharpLabel
- 增加記憶體與超時設定 - IronPDF 所需的時間與記憶體資源,會比 Lambda 的預設值更多。 此設定可於
aws-lambda-tools-defaults.json中進行配置。 請根據您的函式需求調整這些數值。
在此範例中,我們將數值設定為 512 (MB) 和 330 (秒):
"function-memory-size": 512,
"function-timeout": 330,
亦可透過 Lambda 控制台更新設定 - AWS Lambda 記憶體設定。
- 請參照文件最後部分,發布並測試 Lambda 函式:AWS .NET 5 Lambda 支援與容器映像檔
- Lambda 函式亦可透過 Lambda 控制台、AWS Lambda 控制台,或透過 Visual Studio 搭配 AWS Toolkit for Visual Studio 來呼叫。
若您收到"GPU 程序無法使用"的訊息,可能是因為安裝時未以系統管理員權限執行。 請嘗試以系統管理員/root 權限重新執行該軟體。 或者,您也可以在 Dockerfile 中加入以下內容:
RUN chmod 755 "runtimes/linux-x64/native/IronCefSubprocess"準備開始了嗎?
Nuget 下載 19,014,616 | 版本: 2026.5 just released

