📝

[小ネタ] AWS SDK for JavaScript V3 では AWS SDK 全体をロードできない話

2025/02/04に公開

aws/aws-sdk-js-v3: Modularized AWS SDK for JavaScript.

If you want to use non-modular (v2-like) interfaces, you can import client with only the service name (e.g DynamoDB), and call the operation name directly from the client:

Migrate from version 2.x to 3.x of the AWS SDK for JavaScript - AWS SDK for JavaScript

Loading the entire SDK isn’t an issue if your application is using many AWS services. However, if you need to use only a few AWS services, it means increasing the size of your application with code you don't need or use.

In v3, you can load and use only the individual AWS Services you need. This is shown in the following example, which gives you access to Amazon DynamoDB (DynamoDB).

上述の通り、V3 で AWS SDK 全体をロードすることはできない仕様でした。

試したこと

Node.js 22.x の Lambda 関数で試してみました。

  • V2 と同様の方法でロード
const AWS = require("aws-sdk");

以下のエラーが発生しました。

エラー
{
  "errorType": "Runtime.ImportModuleError",
  "errorMessage": "Error: Cannot find module 'aws-sdk'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/index.mjs",
  "trace": [
    "Runtime.ImportModuleError: Error: Cannot find module 'aws-sdk'",
    "Require stack:",
    "- /var/task/index.js",
    "- /var/runtime/index.mjs",
    "    at _loadUserApp (file:///var/runtime/index.mjs:1087:17)",
    "    at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1119:21)",
    "    at async start (file:///var/runtime/index.mjs:1282:23)",
    "    at async file:///var/runtime/index.mjs:1288:1"
  ]
}
  • サービス名を指定せずにロード
const { AWS } = require("@aws-sdk");
エラー
{
  "errorType": "Runtime.ImportModuleError",
  "errorMessage": "Error: Cannot find module '@aws-sdk'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/index.mjs",
  "trace": [
    "Runtime.ImportModuleError: Error: Cannot find module '@aws-sdk'",
    "Require stack:",
    "- /var/task/index.js",
    "- /var/runtime/index.mjs",
    "    at _loadUserApp (file:///var/runtime/index.mjs:1087:17)",
    "    at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1119:21)",
    "    at async start (file:///var/runtime/index.mjs:1282:23)",
    "    at async file:///var/runtime/index.mjs:1288:1"
  ]
}

サービスごとでのロード方法

Migrate from version 2.x to 3.x of the AWS SDK for JavaScript - AWS SDK for JavaScript
ドキュメントに記載の通り、以下のような方法でサービスごとにロードすることは可能です。

import { DynamoDB } from "@aws-sdk/client-dynamodb";

まとめ

今回は AWS SDK for JavaScript V3 では AWS SDK 全体をロードできない話を紹介しました。
どなたかの参考になれば幸いです。

参考資料

Discussion