How to Use IronWord on AWS Lambda

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

この記事は、IronWordを使用してAWS Lambda関数を設定するための包括的なガイドを提供します。 AWS Lambda環境内でWord文書を作成および操作するためにIronWordを設定する方法を学びます。

class="hsg-featured-snippet">

AWS LambdaでのIronWordの使用方法

  1. IronWordをダウンロードしてプロジェクトに追加
  2. Visual Studioを使用してAWS Lambdaプロジェクトを作成する
  3. FunctionHandlerコードを修正してWord文書を生成する
  4. Dockerコンテナでプロジェクトを構成および展開する
  5. Lambda関数を呼び出し、S3の出力ファイルを確認する

インストール

この例ではWord文書をS3バケットに読み書きするため、AWSSDK.S3 NuGetパッケージが必要です。

AWS LambdaでのIronWord ZIPパッケージの使用

IronWord ZIPパッケージを使用する際、AWS Lambdaは読み取り専用ファイルシステム(/tmp/フォルダを除く)であるため、一時的な展開パスを設定することが重要です。 ランタイムファイルにこのフォルダを使用するようにIronWordを設定する必要があります:

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

IronWordとAWSの統合

AWS Lambdaプロジェクトの作成

コンテナ化されたAWS Lambdaプロジェクトを作成するにはVisual Studioを使用します:

  1. Visual Studio用AWS Toolkitをインストールする
  2. AWS Lambdaプロジェクト(.NET Core - C#)を選択する
  3. .NET 8(Container Image)ブループリントを選択して設定を完了する
  4. デプロイメントタイプとしてコンテナイメージを選択する

パッケージ依存関係の追加

IronWordとAWSSDK.S3パッケージをNuGet経由でプロジェクトに追加します。 正しい設定を行えば、IronWordライブラリはAWS Lambdaでスムーズに動作します。以下のコマンドを実行してIronWordをAWSプロジェクトにシームレスにインストールします:

Install-Package IronWord

プロジェクトのDockerfileを更新して.NET 8 Lambdaベースイメージを使用し、ビルド成果物をコピーします:

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

RUN dnf update -y

WORKDIR /var/task

COPY "bin/Release/lambda-publish"  .

FunctionHandlerコードの修正

以下に示すのは、シンプルなWord文書を作成し、.docxファイルとして保存し、S3バケットにアップロードする例のLambda関数です。

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

メモリとタイムアウトを増やす

ドキュメント処理はメモリを多く消費する可能性があるため、aws-lambda-tools-defaults.jsonでLambda関数のメモリを少なくとも512 MB、タイムアウトを300秒に増やします:

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

Runtime exited with error: signal: killedのようなエラーが発生した場合、メモリを増やすかコードを最適化します。

公開

Lambda関数を公開するには:

  • Visual Studioでプロジェクトを右クリック
  • AWS Lambdaに公開...を選択
  • ウィザードに従って必要に応じて設定を構成します

試してみよう!

AWS LambdaコンソールまたはVisual Studioを通じてLambda関数を呼び出します。 実行後、新しく作成されたWord文書をS3バケットで確認します。

よくある質問

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ドキュメント処理に活用することができます。

IronWordをAWS Lambdaで使用してWordドキュメントタスクを自動化できますか?

はい、IronWordをAWS Lambda機能内で使用することで、作成、修正、変換などのさまざまなWordドキュメントタスクを自動化することができます。

IronWordでAWS Lambdaを使用して大規模なWordドキュメントを処理することは可能ですか?

IronWordは大規模なWordドキュメントを効率的に処理するように設計されており、AWS Lambdaと併用することで、Lambdaの構成に応じてスケーラブルにドキュメントを処理することができます。

IronWordはAWS Lambda上でどのようなWordドキュメント操作を実行できますか?

IronWordは、編集、フォーマット、テキストの抽出、ドキュメントの他の形式への変換など、さまざまな操作をAWS Lambda環境内で実行できます。

AWS LambdaにIronWordをデプロイするための前提条件はありますか?

AWS LambdaにIronWordをデプロイする前に、AWSアカウントがあり、AWS Lambdaのセットアップに精通しており、必要なIAMロールと権限が構成されていることを確認してください。

IronWordはAWS Lambda上での更新とメンテナンスをどのように処理しますか?

IronWordは互換性およびパフォーマンスの向上を確保するために定期的に更新されています。AWS Lambda環境では、最新バージョンのIronWordでデプロイメントパッケージを簡単に更新し、最適な機能を維持することができます。

IronWordをAWS Lambdaと共に使用する際のサポートは何がありますか?

Iron Softwareは、IronWordをAWS Lambdaと統合および使用する際にユーザーを支援するためのドキュメントとサポートを提供し、ドキュメント処理のニーズに効果的に利用できるようにしています。

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 ただ今リリースされました