[アップデート] AWS Lambda のマネージドランタイムで .NET 8 が使えるようになりました

2024.02.21

いわさです。

今朝の AWS CLI v1.32.46 の次のコミットで「Add .NET 8 (dotnet8) Runtime support to AWS Lambda.」という内容が追加されていました。

AWS Lambda のマネージドランタイムは LTS のみがサポートされます。
そのため、これまで Lambda .NET の最新版は 2022 年 2 月にサポートされた .NET 6 が最新の状態でした。

.NET 7 も使おうと思えばコンテナイメージあるいはカスタムランタイムであれば使うことが出来たのですが、.NET 7 は STS でありマネージドランタイムでのサポートがされない状態でした。
また、2023 年 11 月に登場した最新の LTS である.NET 8 に関しては、なかなか Lambda マネージドラインタイムでのサポートがされず、さらに当初予定されていた 2024 年 1 月も途中で 2 月に変更される形となっていました。

.NET 8 - February 2024

AWS CLI で使えるということで実際にコンソール上でも使えるのか確認してみましたので紹介します。

コンソール

まず東京リージョンではコンソールから関数を新規作成する際に .NET 8 の選択は出来ませんでした。
後述しますが、東京リージョンコンソールへの反映が遅れているのかなと思います。

しかし、バージニア北部リージョンに関しては最新のサポート対象から次のように「.NET 8 (C#/F#/PowerShell)」が追加されていました。

エッ、F# !?

関数作成してみる

.NET Lambda をコンソールからポチポチ作成するケースは少ないと思います。
CUI であれば .NET Lanmbda Global CLI あるいは SAM、GUI であれば Visual Studio や Visual Studio Code を使うことが多いと思います。

今回は .NET Lambda Global CLI を使って関数を作成してみました。
Amazon.Lambda.Toolsのバージョンがちょっと古かったので最新の 5.10.0 をインストールしておきます。
さらにAmazon.Lambda.Templatesのバージョンも最新化しておきます。

% dotnet tool uninstall -g Amazon.Lambda.Tools
Tool 'amazon.lambda.tools' (version '5.7.2') was successfully uninstalled.

% dotnet tool install -g Amazon.Lambda.Tools  
Skipping NuGet package signature verification.
You can invoke the tool using the following command: dotnet-lambda
Tool 'amazon.lambda.tools' (version '5.10.0') was successfully installed.

% dotnet new install Amazon.Lambda.Templates::6.15.1

The following template packages will be installed:
   Amazon.Lambda.Templates::6.15.1

Amazon.Lambda.Templates (version 6.14.0) is already installed, it will be replaced with version 6.15.1.
Amazon.Lambda.Templates::6.14.0 was successfully uninstalled.
Success: Amazon.Lambda.Templates::6.15.1 installed the following templates:

続いて最小の Lambda 関数をテンプレートから作成してみます。

% dotnet new lambda.EmptyFunction             
The template "Lambda Empty Function" was created successfully.

本日時点で確認した限りでは Amazon.Lambda.Tools 側で .NET 8 のテンプレートをまだ生成してくれないようで、.NET 6 のプロジェクトを .NET 6 の Lambda 関数として作成する構成になっていました。
そこで、次のようにプロジェクトファイルと AWS Lambda Tool の構成ファイルに手を加えます。

hoge0221dotnet.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
    <AWSProjectType>Lambda</AWSProjectType>
    <!-- This property makes the build directory similar to a publish directory and helps the AWS .NET Lambda Mock Test Tool find project dependencies. -->
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
    <!-- Generate ready to run images during publishing to improve cold start time. -->
    <PublishReadyToRun>true</PublishReadyToRun>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
    <PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.1" />
  </ItemGroup>
</Project>

aws-lambda-tools-defaults.json

{
  "Information": [
    "This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.",
    "To learn more about the Lambda commands with the .NET Core CLI execute the following command at the command line in the project root directory.",
    "dotnet lambda help",
    "All the command line options for the Lambda command can be specified in this file."
  ],
  "profile": "",
  "region": "",
  "configuration": "Release",
  "function-architecture": "x86_64",
  "function-runtime": "dotnet8",
  "function-memory-size": 256,
  "function-timeout": 30,
  "function-handler": "hoge0221dotnet::hoge0221dotnet.Function::FunctionHandler"
}

念の為、関数実行時に .NET 実行バージョンを標準出力させておきます。
CloudWatch Logs で確認出来るようになるはず。

Function.cs

using Amazon.Lambda.Core;

// 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 hoge0221dotnet;

public class Function
{
    
    /// <summary>
    /// A simple function that takes a string and does a ToUpper
    /// </summary>
    /// <param name="input"></param>
    /// <param name="context"></param>
    /// <returns></returns>
    public string FunctionHandler(string input, ILambdaContext context)
    {
        Console.WriteLine("Version: {0}", Environment.Version.ToString());
        return input.ToUpper();
    }
}

では関数をビルド&デプロイします。

% dotnet lambda deploy-function hoge0221dotnet
Amazon Lambda Tools for .NET Core applications (5.10.0)
Project Home: https://github.com/aws/aws-extensions-for-dotnet-cli, https://github.com/aws/aws-lambda-dotnet

Executing publish command
... invoking 'dotnet publish', working folder '/Users/iwasa.takahito/work/hoge0221dotnet/src/hoge0221dotnet/bin/Release/net8.0/publish'
... dotnet publish "/Users/iwasa.takahito/work/hoge0221dotnet/src/hoge0221dotnet" --output "/Users/iwasa.takahito/work/hoge0221dotnet/src/hoge0221dotnet/bin/Release/net8.0/publish" --configuration "Release" --framework "net8.0" /p:GenerateRuntimeConfigurationFiles=true --runtime linux-x64 --self-contained False 
... publish: MSBuild version 17.8.3+195e7f5a3 for .NET
... publish:   Determining projects to restore...
... publish:   Restored /Users/iwasa.takahito/work/hoge0221dotnet/src/hoge0221dotnet/hoge0221dotnet.csproj (in 7.03 sec).
... publish:   hoge0221dotnet -> /Users/iwasa.takahito/work/hoge0221dotnet/src/hoge0221dotnet/bin/Release/net8.0/linux-x64/hoge0221dotnet.dll
... publish:   hoge0221dotnet -> /Users/iwasa.takahito/work/hoge0221dotnet/src/hoge0221dotnet/bin/Release/net8.0/publish/
Changed permissions on published file (chmod +rx hoge0221dotnet.runtimeconfig.json).
Changed permissions on published file (chmod +rx hoge0221dotnet.deps.json).
Changed permissions on published file (chmod +rx Amazon.Lambda.Serialization.SystemTextJson.dll).
Changed permissions on published file (chmod +rx hoge0221dotnet.dll).
Changed permissions on published file (chmod +rx Amazon.Lambda.Core.dll).
Changed permissions on published file (chmod +rx hoge0221dotnet.pdb).
Zipping publish folder /Users/iwasa.takahito/work/hoge0221dotnet/src/hoge0221dotnet/bin/Release/net8.0/publish to /Users/iwasa.takahito/work/hoge0221dotnet/src/hoge0221dotnet/bin/Release/net8.0/hoge0221dotnet.zip
... zipping:   adding: hoge0221dotnet.runtimeconfig.json (deflated 38%)
... zipping:   adding: hoge0221dotnet.deps.json (deflated 68%)
... zipping:   adding: Amazon.Lambda.Serialization.SystemTextJson.dll (deflated 51%)
... zipping:   adding: hoge0221dotnet.dll (deflated 58%)
... zipping:   adding: Amazon.Lambda.Core.dll (deflated 54%)
... zipping:   adding: hoge0221dotnet.pdb (deflated 42%)
Created publish archive (/Users/iwasa.takahito/work/hoge0221dotnet/src/hoge0221dotnet/bin/Release/net8.0/hoge0221dotnet.zip).

:

お、失敗するかもと少し思ったのですがうまくデプロイ出来ましたね。
ちなみに特にリージョンを指定していないので私の環境のデフォルトプロファイルに従って東京リージョンにデプロイされているはず。

コンソールを確認してみましょう。

おお、東京リージョンでも .NET 8 として関数が作成出来ていますね。
作成機能のところだけまだロールアウトされてない感じだろうか。すぐに使えるようになりそうな気がします。

関数を実行してみると、問題なく実行出来ました。
実行バージョンも出力されていますね。8.0.1で実行されています。

つい先日 2 月 15 日に8.0.2がリリースされていたのですが、そちらはまだランタイムに含まれていないようです。

さいごに

本日は AWS Lambda のマネージドランタイムで .NET 8 が使えるようになっていたので確認してみました。

.NET 8 使えましたね。
.NET 8 では C# 12 や .NET Aspire などの新機能が使えるようになっています。使っていきましょう。
また、一部破壊的変更も含まれていますのでアップデート検討される際にはご注意ください。

ちなみに、.NET 6 自体の Lambda でのサポート予定は次のようになっています。

  • 廃止日:2024 年 11 月 12 日
  • 新規関数が作成できなくなる:2025 年 1 月 11 日
  • 既存関数の更新ができなくなる:2025 年 2 月 11 日

計画的にアップデートしましょう。
AWS 公式のアップデートアナウンスがまだ出ていないですがそちらもすぐに出るのではと思います。