如何在 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 文件:

使用 .NET 5 的 AWS Lambda

AWS Lambda 与 .NET 7

AWS Lambda 与 .NET 8

3.添加 IronPdf (Linux) NuGet 包。

安装<代码>IronPdf.Linux</代码

1.在解决方案资源管理器中,右键单击 "引用"、"管理 NuGet 包"。 2.选择浏览并搜索 IronPdf.Linux.

  1. 选择包并安装。

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 Support with Container Images"文档后半部分的说明发布和测试您的 Lambda 函数。

7.试用!

您可以使用 Lambda 控制台或通过 Visual Studio 使用 AWS Toolkit for 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 下载 16,685,821 | 版本: 2025.12 刚刚发布