How to Run & Deploy IronPDF .NET on AWS Lambda

This article was translated from English: Does it need improvement?
Translated
View the article in English
class="container-fluid">
class="row">
class="col-md-2"> Amazon Web Services V1 related to How to Run & Deploy IronPDF .NET on AWS Lambda

1. 使用容器模板(.NET 5)创建 AWS Lambda

请参考 AWS 官方文档的第一部分:.NET 5 AWS Lambda 支持容器镜像

2. 添加包依赖

这些依赖项是为了在此 AWS 环境中使用 Chrome 而必需的。

请按照这些说明修改 Docker 文件:

使用 .NET 5 的 AWS Lambda

AWS Lambda with .NET 7

AWS Lambda with .NET 8

3. Add the IronPDF (Linux) NuGet package

Install IronPdf.Linux

  1. In Solution Explorer, right-click References, Manage NuGet Packages.
  2. Select Browse and search IronPdf.Linux.
  3. Select the package and install.

4. Modify FunctionHandler code

This example will create a PDF file from a webpage https://ironpdf.com/ and save it to /tmp. To view this PDF, you must upload it to another service such as S3.

It is necessary to configure the temporary folder when using IronPDF on AWS Lambda. Please use the TempFolderPath and CustomDeploymentDirectory properties to do this.

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. Increase Memory and Timeout

IronPDF requires more time and memory than the default value of Lambda. You can configure it in aws-lambda-tools-defaults.json. Please adjust this to match your function. In this example, we will set it to 512 (MB) and 330 (seconds).

{
    "function-memory-size": 512,
    "function-timeout": 330
}

You also can update this configuration using the Lambda console. Navigate to the Configuring AWS Lambda functions article for more information.

6. Publish

Please follow the instructions in the latter part of the '.NET 5 AWS Lambda Support with Container Images' document to publish and test your Lambda function.

7. Try it out!

You can activate the Lambda function using the Lambda console or via Visual Studio by using the AWS Toolkit for Visual Studio.

常见问题解答

如何使用.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,154,058 | 版本: 2025.11 刚刚发布