3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

AWS SDK V3 + Lambda を SAM deploy した際の "Could not resolve" の対処法

Posted at

背景

SAM で EventBridge Trigger + Lambda を Deploy しようとしてる

その際に発生したエラーの対処法の記録

エラー内容

最初は npm install かと思ったけど、Esbuild の話でそこそこ悩んだ

Esbuild error
Error: NodejsNpmEsbuildBuilder:EsbuildBundle - Esbuild Failed: X [ERROR] Could not resolve "@aws-sdk/lib-dynamodb"
    app.ts:2:27:
      2  import { PutCommand } from "@aws-sdk/lib-dynamodb";
                                    ~~~~~~~~~~~~~~~~~~~~~~~
  You can mark the path "@aws-sdk/lib-dynamodb" as external to exclude it from the bundle, which will remove this error. 

解決策

エラーメッセージ通りで、Esbuild の Bundle 対象から外せばいいだけでした。

具体的には、template.yaml の BuildProperties に、以下のように External: を追加すればOKでした。

template.yaml
    Metadata: # Manage esbuild properties
      BuildMethod: esbuild
      BuildProperties:
        Minify: true
        Target: "es2020"
        Sourcemap: true
        EntryPoints: 
          - app.ts
        # ★ここを追加★
        External:
          - "@aws-sdk/lib-dynamodb"
          - "@aws-sdk/client-dynamodb"

Document

説明は以下
image.png

日本語版は機械翻訳で、Properties も一緒に翻訳されてるので注意
image.png

その他の確認内容

解決に至るまでの調査内容

インストールし忘れ?かと思い試すがダメ

インストール
npm install @aws-sdk/lib-dynamodb

dependencies のせい?でもなし

stackoverflow で検索したらV2 の頃の同じエラーがあったので、以下二点試すもダメ
※対象 URL 逸失

  • Dev Install を試すがダメ(uninstall 後)
    • dependencies > devDependencies と理解
Dev Install
npm install -D @aws-sdk/lib-dynamodb
  • Optional Install & Omit を試すがダメ
    • optionalDependencies & npmrc で、omit[]=optional 追加 と認識
check
npm install -O @aws-sdk/lib-dynamodb
npmrc の設定
omit[]=optional

npmrc の現状動作の確認と、全オプションの初期値付き編集

現在の npmrc の読み込み状況を調べる
npm publish --dry-run --verbose

npm の config 設定を編集

  • これで開くと、option default が列挙されるのでわかりやすくて Good :heart_eyes:
    npm config -g edit
  • -g を付けるかは状況次第

SAM validate してみたら、region が抜けてたけど、region 追加してもダメ

確認して

sam validation
sam validate

.aws/config に region 追加

error 内容に従って、build --exclude

変わらず

exclude
sam build --exclude @aws-sdk/lib-dynamodb

あとがき

AWS-SDK V2 の情報はたくさん見つかるけど、V3 用を探すのが面倒・・
少し慣れて V2/V3 を見分けられるようになったけど、最初はきつかった

V2/V3 の見分け方

SDK import 名 補足
V2 aws-sdk import * as AWS from 'aws-sdk';
V3 @aws-sdk/{service} {import { S3Client } from '@aws-sdk/client-s3'; サービス単位
3
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?