如何在 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. 使用容器模板(.NET 5)创建 AWS Lambda

请参考 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 控制台,或借助 AWS Toolkit for Visual Studio 在 Visual Studio 中激活 Lambda 函数。

常见问题解答

如何使用.NET AWS Lambda函数从URL创建PDF?

要在.NET AWS Lambda函数中从URL创建PDF,使用IronPDF的ChromePdfRenderer.RenderUrlAsPdf方法。确保使用TempFolderPathCustomDeploymentDirectory属性配置临时文件夹。

我应该采取哪些步骤来配置AWS Lambda上的IronPDF?

通过设置TempFolderPathCustomDeploymentDirectory来配置AWS Lambda上的IronPDF,禁用ChromeGpuModes.Disabled的GPU,并通过LinuxAndDockerDependenciesAutoConfig设置为true启用Linux和Docker依赖项的自动配置。

为什么调整AWS Lambda的内存和超时设置很重要?

调整AWS Lambda的内存和超时设置非常重要,因为IronPDF需要比默认设置更多的资源。这可以在aws-lambda-tools-defaults.json中配置,也可以通过AWS Lambda控制台配置。

如何确保AWS Lambda正确安装了Chrome依赖项?

通过根据文章中提供的说明修改Dockerfile,确保正确安装AWS Lambda的Chrome依赖项。这包括在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函数中处理异常?

通过在try-catch块中包装您的IronPDF操作、使用context.Logger.LogLine记录错误来调试和排查故障来处理AWS Lambda函数中的异常。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 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文件。