📝
CloudFormation から .NET の Lambda 関数をデプロイしてみた
前提
- コマンド実行環境: Cloud9
- プラットフォーム: Amazon Linux 2023
01. Lambda 関数コードの作成
任意のディレクトリを作成し、ディレクトリ内にソースコードを作成します。
test/
├── Function.cs
└── HelloWorldFunction.csproj
Function.cs
using Amazon.Lambda.Core;
using System.Text.Json;
// 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 HelloWorldFunction;
public class Function
{
public string FunctionHandler(object input, ILambdaContext context)
{
context.Logger.LogInformation("Hello from .NET 8 Lambda!");
var response = new
{
statusCode = 200,
body = JsonSerializer.Serialize(new {
message = "Hello from .NET 8 Lambda function!",
timestamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss UTC")
})
};
return JsonSerializer.Serialize(response);
}
}
HelloWorldFunction.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AWSProjectType>Lambda</AWSProjectType>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<PublishReadyToRun>false</PublishReadyToRun>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Amazon.Lambda.Core" Version="2.2.0" />
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.4.0" />
</ItemGroup>
</Project>
02. ビルド
.NET SDK のインストールから行います。
$ sudo dnf install -y dotnet-sdk-8.0
$ dotnet --version
8.0.116
$ dotnet build
$ dotnet publish -c Release -o publish
$ cd publish && zip -r ../lambda-function.zip . && cd ..
03. zip ファイルを S3 バケットにアップロード
任意の名称の S3 バケットを作成し、lambda-function.zip をアップロードします。

04. CloudFormation テンプレート作成
以下のテンプレートファイルを作成します。
AWSTemplateFormatVersion: "2010-09-09"
Resources:
HelloWorldLambdaFunction:
Type: AWS::Lambda::Function
Properties:
FunctionName: !Sub "${AWS::StackName}-hello-world-function"
Runtime: dotnet8
Handler: HelloWorldFunction::HelloWorldFunction.Function::FunctionHandler
Role: your-lambda-role
Code:
S3Bucket: your-bucket-name
S3Key: lambda-function.zip
Timeout: 30
MemorySize: 128
- Role: 既存の Lambda 実行ロールを指定
- S3Bucket: 手順 03 で作成した S3 バケット名を指定
05. デプロイ
CloudFormation コンソールから上記テンプレートファイルをアップロードしてスタックを作成します。

06 動作確認
Lambda コンソールからテスト実行して以下のログが出力されれば成功です。
Hello from .NET 8 Lambda!
まとめ
今回は CloudFormation から .NET の Lambda 関数をデプロイしてみました。
どなたかの参考になれば幸いです。
Discussion