3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C#のランタイムを設定し、Lambda関数をデプロイする手順について

Posted at

はじめに

本記事について

本記事では、C#を使用してLambda関数を作成し、デプロイする方法について詳しく解説します。
AWS Lambdaは、サーバーレスコンピューティングを提供するサービスで、サーバー管理不要でコードを実行できます。

C#を用いたLambda関数の作成は、API GatewayやDynamoDBとの統合、バックグラウンド処理、イベント駆動アーキテクチャなどに活用できます。

この記事では、以下の内容を説明します。

  • 必要なツールと環境構築
  • C# Lambdaプロジェクトの作成
  • コードの記述とビルド
  • Lambda関数のデプロイ
  • AWS CLIを用いたLambda関数のテスト

前提条件

作業を始める前に、以下を準備してください。

必要なツール

ツール 説明 インストール方法
.NET 8 SDK C#のLambda関数を開発するために必要 .NET8 SDK公式ダウンロードサイト
AWS CLI AWSと通信し、Lambda関数をデプロイ・管理するために必要 AWS CLI公式ダウンロードサイト
Amazon.Lambda.Tools Lambda関数のビルド・デプロイを支援するコマンドラインツール dotnet tool install -g Amazon.Lambda.Tools

環境設定

1. NET SDK のインストール確認

dotnet --version  # バージョンが表示されればOK

2.AWS CLI の設定

aws configure

設定時に以下の情報を入力します。

  • AWS Access Key ID IAMユーザーのアクセスキー
  • AWS Secret Access Key IAMユーザーのシークレットキー
  • Default region name: ap-northeast-1(東京リージョンなど)
  • Default output format: json(tableやtextも選択可能)

お好みの方法で、Lambdaを構築するAWS環境にアクセスしてください。

AWS CLIの設定が完了すれば、aws s3 ls などのコマンドが正常に動作することを確認してください。

Lambdaプロジェクトの作成

1. 作業ディレクトリの作成 (省略可)

mkdir workspace
cd workspace

2. Lambdaプロジェクトの作成

dotnet new lambda.EmptyFunction --name ExampleLambda

3. 必要なパッケージを追加

cd src/ExampleLambda

dotnet add package Amazon.Lambda.APIGatewayEvents  # API Gatewayと連携する場合

dotnet add package Amazon.Lambda.Core  # Lambdaの基本機能

dotnet add package Amazon.Lambda.Serialization.SystemTextJson  # JSONシリアライズ

4. コードの編集
Function.csを以下のように編集します。

using System.Text.Json;
using Amazon.Lambda.Core;
using Amazon.Lambda.APIGatewayEvents;

[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace ExampleLambda
{
    public class Function
    {
        public APIGatewayProxyResponse FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context)
        {
            context.Logger.LogLine("ExampleLambda invoked via API Gateway");

            return new APIGatewayProxyResponse
            {
                StatusCode = 200,
                Body = JsonSerializer.Serialize(new { message = "ExampleLambda executed successfully" }),
                Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } }
            };
        }
    }
}

5. ビルドの実行

dotnet build

Lambda関数のデプロイ

1. Lambda関数をデプロイ

dotnet lambda deploy-function --function-name ExampleLambda --profile default --region ap-northeast-1

パラメータ説明

  • --function-name 作成するLambda関数の名前
  • --profile AWS CLIのプロファイル(デフォルトは default
  • --region デプロイ先のリージョン

2. デプロイ成功の確認

AWSコンソールのLambdaセクションで関数が表示されており、
ランタイムが.NET8になっていれば成功です。

Lambda関数のテスト

1. AWS CLIを利用したテスト

aws lambda invoke --function-name ExampleLambda --payload '{"test": "value"}' --region ap-northeast-1 --cli-binary-format raw-in-base64-out response.json

2. 実行結果の確認

CLIの出力

StatusCode: 200

レスポンスファイル (response.json)

{
    "statusCode": 200,
    "body": "{\"message\":\"ExampleLambda executed successfully\"}"
}

まとめ

本記事では、C#を使用してAWS Lambda関数を作成し、デプロイして実行するまでの手順を詳しく解説しました。

学んだこと

✅ .NETを使ったLambda関数の作成
✅ 必要なパッケージの追加とコード編集
✅ AWS CLIを使用したデプロイとテスト

今後は、DynamoDBとの連携やStep Functionsを活用したワークフロー構築など、より高度なサーバーレスアーキテクチャの構築も試してみてください!

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?