AWS LambdaのNode.js 24ランタイムを試してみた

AWS LambdaのNode.js 24ランタイムを試してみた

2025年11月にサポートされたAWS LambdaのNode.js 24ランタイムを検証。CLIおよびCloudFormationでのデプロイ手順に加え、廃止されたコールバック形式ハンドラー実行時に発生するRuntime.CallbackHandlerDeprecatedエラーの挙動を実機で確認しました
2025.11.28

2025年11月25日、AWS Lambda が Node.js 24 のサポートを開始しました。

AWS Lambda adds support for Node.js 24 | AWS What's New

Node.js 24 ランタイムでは、従来のコールバックベースの関数ハンドラーが廃止され、async/await パターンのみのサポートとなるなど、開発手法に関わる重要な変更が含まれています。本記事では、AWS CLI および CloudFormation を用いたデプロイ検証と、コールバック形式を使用した際のエラー挙動について確認した結果を紹介します。

検証環境

  • OS: Linux (Amazon Linux 2023)
  • AWS CLI: aws-cli/2.32.6
  • リージョン: us-east-1
  • デプロイ方法: AWS CLI、CloudFormation

1. AWS CLI による ES Modules 関数のデプロイ

まずは nodejs24.x ランタイムを指定し、ES Modules 形式(import/export)を用いた関数を作成しました。

index.js

export const handler = async (event) => {
  console.log('Event:', JSON.stringify(event, null, 2));

  return {
    statusCode: 200,
    body: JSON.stringify({
      message: 'Hello from Node.js 24!',
      timestamp: new Date().toISOString()
    })
  };
};

package.json
ES Modules を有効化するため、"type": "module" を指定しました。

{
  "type": "module"
}

以下のコマンドで zip ファイルを作成し、Lambda 関数の作成と実行を行いました。

# zip ファイル作成
zip function.zip index.js package.json

# Lambda 関数作成
aws lambda create-function \
  --function-name node24-hello-world \
  --runtime nodejs24.x \
  --handler index.handler \
  --role arn:aws:iam::123456789012:role/service-role/lambda-execution-role \
  --zip-file fileb://function.zip \
  --region us-east-1

# 実行
aws lambda invoke \
  --function-name node24-hello-world \
  --region us-east-1 \
  response.json

実行結果 (response.json)

{
  "statusCode": 200,
  "body": "{\"message\":\"Hello from Node.js 24!\",\"timestamp\":\"2025-11-28T10:00:00.000Z\"}"
}

CLI経由で nodejs24.x ランタイムへのデプロイおよび ES Modules の動作が正常に行われることを確認しました。

Webコンソール上でも、Node.js 24.x ランタイムのLambda関数の動作を確認できました。

nodejs24_Lambda画面

2. CloudFormation によるデプロイ

次に、CloudFormation テンプレートを用いたデプロイを検証しました。ZipFile プロパティを使用する場合、package.json を含めることができないため、ここでは CommonJS 形式で記述しました。

template.yaml

AWSTemplateFormatVersion: '2010-09-09'
Description: Node.js 24 Lambda Function Test

Resources:
  LambdaExecutionRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service: lambda.amazonaws.com
            Action: sts:AssumeRole
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

  Node24Function:
    Type: AWS::Lambda::Function
    Properties:
      FunctionName: node24-cfn-test
      Runtime: nodejs24.x
      Handler: index.handler
      Role: !GetAtt LambdaExecutionRole.Arn
      Code:
        ZipFile: |
          exports.handler = async (event) => {
            console.log('Event:', JSON.stringify(event, null, 2));
            return {
              statusCode: 200,
              body: JSON.stringify({
                message: 'Hello from Node.js 24 via CloudFormation!',
                timestamp: new Date().toISOString()
              })
            };
          };

デプロイ後、スタックステータスが CREATE_COMPLETE となることを確認しました。
CloudFormation においても Node.js 24.x が問題なく利用可能であることを確認しました。

3. コールバック形式廃止の挙動検証

Node.js 24 における最大の変更点である「コールバック形式の廃止」について、実際に廃止された形式のコードをデプロイし、どのようなエラーが発生するかログを確認しました。

検証コード (callback-handler.js)

exports.handler = (event, context, callback) => {
  console.log('Event:', JSON.stringify(event, null, 2));

  // Node.js 24ではサポートされない書き方
  callback(null, {
    statusCode: 200,
    body: JSON.stringify({
      message: 'Hello from callback handler'
    })
  });
};

実行結果 (Invocation Error)

aws lambda invoke を実行したところ、以下の Runtime.CallbackHandlerDeprecated エラーが返却されました。

{
  "StatusCode": 200,
  "FunctionError": "Unhandled",
  "ExecutedVersion": "$LATEST"
}

CloudWatch Logs 出力

{
  "errorType": "Runtime.CallbackHandlerDeprecated",
  "errorMessage": "ERROR: AWS Lambda has removed support for callback-based function handlers starting with Node.js 24. You need to modify this function to use a supported handler signature to use Node.js 24 or later. For more information see https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html."
}

エラーメッセージに明記されている通り、Node.js 24 以降ではコールバックベースのハンドラーを使用するとランタイムエラーとなり、実行が失敗することを確認しました。これにより、既存コードの移行時には async/await への書き換えが必須であることが実証されました。

4. エラーハンドリングの検証

async/await パターンにおけるエラーハンドリングの挙動についても確認しました。

検証コード (error-handler.js)

exports.handler = async (event) => {
  // try-catch でのエラーハンドリング
  try {
    if (event.tryCatchError) {
      throw new Error('Error caught in try-catch');
    }
    return { statusCode: 200, body: 'Success' };
  } catch (error) {
    console.error('Caught error:', error);
    return {
      statusCode: 500,
      body: JSON.stringify({ error: error.message })
    };
  }
};

実行結果

ペイロード {"tryCatchError":true} を送信した際の結果です。

{
  "statusCode": 500,
  "body": "{\"error\":\"Error caught in try-catch\"}"
}

意図通り catch ブロックでエラーが捕捉され、ステータスコード 500 のレスポンスが返却されました。従来の callback(error) に代わり、標準的な JavaScript の try-catch 構文で制御が可能であることを確認しました。

まとめ

今回の確認で、AWS Lambda の Node.js 24 ランタイムにおける以下の挙動が確認できました。

  • AWS CLI および CloudFormation で nodejs24.x へのデプロイが正常に行えること。
  • ES Modules (type: module) がサポートされていること。
  • コールバック形式のハンドラーは Runtime.CallbackHandlerDeprecated エラーとなり、完全に実行不可であること。
  • エラーハンドリングは async/awaittry-catch を用いた標準的な手法に統一されたこと。

Node.js 24 は 2028年4月までの長期サポート(LTS) が予定されています。最新機能と長いサポート期間を享受できるため、新規開発においては本バージョンを採用することをお勧めします。

Node.js 24 への移行は、長年親しまれてきたコールバック形式の廃止という大きな変更を伴います。既存の資産によってはコード改修のコストが発生しますが、これを機に async/await パターンへの完全移行を行うことで、可読性の向上や標準的なエラーハンドリングへの統一といったモダン化をご検討ください。

この記事をシェアする

FacebookHatena blogX

関連記事