How to Use IronWord on AWS Lambda

This article was translated from English: Does it need improvement?
Translated
View the article in English

This article provides a comprehensive guide to setting up an AWS Lambda function using IronWord. You will learn how to configure IronWord to create and manipulate Word documents within an AWS Lambda environment.

Installation

Since this example will read/write Word documents to an S3 bucket, the AWSSDK.S3 NuGet package is required.

Using IronWord ZIP Package on AWS Lambda

When using the IronWord ZIP package, it’s important to set a temporary deployment path because AWS Lambda has a read-only filesystem except for the /tmp/ folder. You must configure IronWord to use this folder for its runtime files:

var awsTmpPath = @"/tmp/";
IronSoftware.Word.Installation.DeploymentPath = awsTmpPath;
var awsTmpPath = @"/tmp/";
IronSoftware.Word.Installation.DeploymentPath = awsTmpPath;
Dim awsTmpPath = "/tmp/"
IronSoftware.Word.Installation.DeploymentPath = awsTmpPath
$vbLabelText   $csharpLabel

Integrating IronWord with AWS

Create an AWS Lambda Project

Use Visual Studio to create a containerized AWS Lambda project:

  1. Install the AWS Toolkit for Visual Studio
  2. Select AWS Lambda Project (.NET Core - C#)
  3. Choose the .NET 8 (Container Image) blueprint and finish the setup
  4. Select container image as the deployment type

Add Package Dependencies

Add IronWord and AWSSDK.S3 packages to your project via NuGet. The IronWord library works smoothly on AWS Lambda with the correct setup. Run the following command to install IronWord to your AWS project seamlessly:

Install-Package IronWord

Update your project's Dockerfile to use the .NET 8 Lambda base image and copy your build artifacts:

FROM public.ecr.aws/lambda/dotnet:8

RUN dnf update -y

WORKDIR /var/task

COPY "bin/Release/lambda-publish"  .

Modify the FunctionHandler Code

Below is an example Lambda function that creates a simple Word document, saves it as a .docx file, and uploads it to an S3 bucket.

using Amazon.Lambda.Core;
using Amazon.S3;
using Amazon.S3.Model;
using IronWord;
using IronWord.Models;
using System.Text;

// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace IronWordAwsLambda;

public class Function
{
    private static readonly IAmazonS3 _s3Client = new AmazonS3Client(Amazon.RegionEndpoint.APSoutheast1);

    public async Task FunctionHandler(ILambdaContext context)
    {
        var awsTmpPath = @"/tmp/";
        License.LicenseKey = "YOUR-LICENSE-KEY"; // Replace if you have one

        string filename = Guid.NewGuid() + ".docx";
        string localPath = Path.Combine(awsTmpPath, filename);
        string bucketName = "your-s3-bucket-name";
        string objectKey = $"IronWordAwsLambda/{filename}";

        try
        {
            // Create Word Document
            var doc = new WordDocument();
            Paragraph paragraph = new Paragraph(new TextContent("Hello from IronWord on AWS Lambda!"));
            doc.AddParagraph(paragraph);
            doc.SaveAs(localPath);

            context.Logger.LogLine("Word document created.");

            // Upload to S3
            byte[] fileBytes = await File.ReadAllBytesAsync(localPath);
            await UploadToS3Async(bucketName, objectKey, fileBytes);

            context.Logger.LogLine($"Document uploaded to S3: {bucketName}/{objectKey}");
        }
        catch (Exception ex)
        {
            context.Logger.LogLine($"[ERROR] {ex.Message}");
        }
    }

    private async Task UploadToS3Async(string bucketName, string objectKey, byte[] fileBytes)
    {
        using var stream = new MemoryStream(fileBytes);
        var request = new PutObjectRequest
        {
            BucketName = bucketName,
            Key = objectKey,
            InputStream = stream,
            ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
        };
        await _s3Client.PutObjectAsync(request);
    }
}
using Amazon.Lambda.Core;
using Amazon.S3;
using Amazon.S3.Model;
using IronWord;
using IronWord.Models;
using System.Text;

// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace IronWordAwsLambda;

public class Function
{
    private static readonly IAmazonS3 _s3Client = new AmazonS3Client(Amazon.RegionEndpoint.APSoutheast1);

    public async Task FunctionHandler(ILambdaContext context)
    {
        var awsTmpPath = @"/tmp/";
        License.LicenseKey = "YOUR-LICENSE-KEY"; // Replace if you have one

        string filename = Guid.NewGuid() + ".docx";
        string localPath = Path.Combine(awsTmpPath, filename);
        string bucketName = "your-s3-bucket-name";
        string objectKey = $"IronWordAwsLambda/{filename}";

        try
        {
            // Create Word Document
            var doc = new WordDocument();
            Paragraph paragraph = new Paragraph(new TextContent("Hello from IronWord on AWS Lambda!"));
            doc.AddParagraph(paragraph);
            doc.SaveAs(localPath);

            context.Logger.LogLine("Word document created.");

            // Upload to S3
            byte[] fileBytes = await File.ReadAllBytesAsync(localPath);
            await UploadToS3Async(bucketName, objectKey, fileBytes);

            context.Logger.LogLine($"Document uploaded to S3: {bucketName}/{objectKey}");
        }
        catch (Exception ex)
        {
            context.Logger.LogLine($"[ERROR] {ex.Message}");
        }
    }

    private async Task UploadToS3Async(string bucketName, string objectKey, byte[] fileBytes)
    {
        using var stream = new MemoryStream(fileBytes);
        var request = new PutObjectRequest
        {
            BucketName = bucketName,
            Key = objectKey,
            InputStream = stream,
            ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
        };
        await _s3Client.PutObjectAsync(request);
    }
}
Imports Amazon.Lambda.Core
Imports Amazon.S3
Imports Amazon.S3.Model
Imports IronWord
Imports IronWord.Models
Imports System.Text

' Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
<Assembly: LambdaSerializer(GetType(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))>

Namespace IronWordAwsLambda

	Public Class [Function]
		Private Shared ReadOnly _s3Client As IAmazonS3 = New AmazonS3Client(Amazon.RegionEndpoint.APSoutheast1)

		Public Async Function FunctionHandler(ByVal context As ILambdaContext) As Task
			Dim awsTmpPath = "/tmp/"
			License.LicenseKey = "YOUR-LICENSE-KEY" ' Replace if you have one

			Dim filename As String = Guid.NewGuid().ToString() & ".docx"
			Dim localPath As String = Path.Combine(awsTmpPath, filename)
			Dim bucketName As String = "your-s3-bucket-name"
			Dim objectKey As String = $"IronWordAwsLambda/{filename}"

			Try
				' Create Word Document
				Dim doc = New WordDocument()
				Dim paragraph As New Paragraph(New TextContent("Hello from IronWord on AWS Lambda!"))
				doc.AddParagraph(paragraph)
				doc.SaveAs(localPath)

				context.Logger.LogLine("Word document created.")

				' Upload to S3
				Dim fileBytes() As Byte = Await File.ReadAllBytesAsync(localPath)
				Await UploadToS3Async(bucketName, objectKey, fileBytes)

				context.Logger.LogLine($"Document uploaded to S3: {bucketName}/{objectKey}")
			Catch ex As Exception
				context.Logger.LogLine($"[ERROR] {ex.Message}")
			End Try
		End Function

		Private Async Function UploadToS3Async(ByVal bucketName As String, ByVal objectKey As String, ByVal fileBytes() As Byte) As Task
			Dim stream = New MemoryStream(fileBytes)
			Dim request = New PutObjectRequest With {
				.BucketName = bucketName,
				.Key = objectKey,
				.InputStream = stream,
				.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
			}
			Await _s3Client.PutObjectAsync(request)
		End Function
	End Class
End Namespace
$vbLabelText   $csharpLabel

Increase Memory and Timeout

Since document processing can be memory-intensive, increase your Lambda function memory to at least 512 MB and timeout to 300 seconds in aws-lambda-tools-defaults.json:

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

If you encounter errors like Runtime exited with error: signal: killed, increase memory or optimize your code.

Publish

To publish your Lambda function:

  • Right-click your project in Visual Studio
  • Select Publish to AWS Lambda...
  • Follow the wizard and configure settings as needed

Try It Out!

Invoke the Lambda function through the AWS Lambda Console or Visual Studio. After execution, check your S3 bucket for the newly created Word document.

常見問題解答

IronWord是什麼?它如何增強AWS Lambda上的Word文件處理能力?

IronWord 是一款功能強大的 Word 文件處理工具,與 AWS Lambda 整合後,可提供可擴展且高效的文件管理,讓您能夠自動執行和簡化 Word 文件任務。

如何將 IronWord 與 AWS Lambda 整合?

要將 IronWord 與 AWS Lambda 集成,您需要在 AWS Lambda 環境中設定 IronWord 包,配置必要的權限,並部署利用 IronWord 的文件處理功能的功能程式碼。

在 AWS Lambda 上使用 IronWord 有哪些好處?

在 AWS Lambda 上使用 IronWord 可以利用無伺服器架構進行 Word 文件處理,從而提供可擴充性、成本效益和減少基礎架構管理。

我能否使用 AWS Lambda 上的 IronWord 來實現 Word 文件任務的自動化?

是的,您可以使用 AWS Lambda 函數中的 IronWord 來自動執行各種 Word 文件任務,例如建立、修改和轉換。

是否可以使用 AWS Lambda 上的 IronWord 處理大型 Word 文件?

IronWord 旨在有效處理大型 Word 文檔,當與 AWS Lambda 一起使用時,它可以根據您的 Lambda 配置以可擴展的方式處理文檔。

IronWord 可以在 AWS Lambda 上執行哪些 Word 文件操作?

IronWord 可以在 AWS Lambda 環境中對 Word 文件執行各種操作,包括編輯、格式化、提取文字以及將文件轉換為不同的格式。

在 AWS Lambda 上部署 IronWord 有什麼先決條件嗎?

在 AWS Lambda 上部署 IronWord 之前,請確保您擁有 AWS 帳戶、熟悉 AWS Lambda 設置,並配置了所有必要的 IAM 角色和權限。

IronWord 如何處理 AWS Lambda 上的更新與維護?

IronWord 會定期更新,以確保相容性並提升效能。在 AWS Lambda 環境中,您可以輕鬆地將部署套件更新至最新版本的 IronWord,從而保持最佳功能。

將 IronWord 與 AWS Lambda 結合使用可獲得哪些支援?

Iron Software 提供文件和支持,幫助使用者在 AWS Lambda 上整合和使用 IronWord,確保您能夠有效地利用其功能來滿足您的文件處理需求。

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'name'

Filename: sections/author_component.php

Line Number: 18

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 18
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/get-started/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Getstarted.php
Line: 25
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'title'

Filename: sections/author_component.php

Line Number: 38

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 38
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/get-started/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Getstarted.php
Line: 25
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'comment'

Filename: sections/author_component.php

Line Number: 48

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 48
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/get-started/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Getstarted.php
Line: 25
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

準備好開始了嗎?
Nuget 下載 25,807 | 版本: 2025.11 剛剛發布