0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

# DynamoDBに一気にアイテムを書き込みたい!!

0
Last updated at Posted at 2026-02-02

💻今回は自身のPCからDynamoDBに、一気にアイテムをPUTする方法を忘備録として書き残しておきます。

検証環境や初期データ投入・マスタ情報の投入など、「とりあえずまとめて入れたい」場面で使えます。

特にまとめて入れようにもCSVでは、テーブルの新規作成時点でしかインポートできないため、難儀しました。

環境

  • AWS CLIが使えること 
  • DynamoDBに batch-write-item できる Profile があること 

① Profileの設定

デフォルト値の変更

aws configure

デフォルト値の確認

aws configure list

プロファイルの一覧を確認

aws configure list-profiles

プロファイルを指定して確認

aws configure list --profile your-profile-name

プロファイルを指定して変更

aws configure --profile your-profile-name

② Yamlを作成

今回は Yaml 形式で書いていきます。

以下のように PutRequest を複数書いていきます。

RequestItems:
  {テーブル名}:
    - PutRequest:
        Item:
          PK:
            S: "{任意の値}"
          SK:
            S: "{任意の値}"
          {列名}:
            {型}: "{任意の値}"
    - PutRequest:
        Item:
          PK:
            S: "{任意の値}"
          SK:
            S: "{任意の値}"
          {列名}:
            {型}: "{任意の値}"

③ Yaml実際の値(例)

今回は SampleTable というテーブルに対して、複数のユーザー情報を一気に登録する例です。

Upload.yaml

RequestItems:
  SampleTable:
    - PutRequest:
        Item:
          PK:
            S: "USER#001"
          SK:
            S: "PROFILE"
          UserName:
            S: "Taro Yamada"
          Age:
            N: "30"
    - PutRequest:
        Item:
          PK:
            S: "USER#002"
          SK:
            S: "PROFILE"
          UserName:
            S: "Hanako Suzuki"
          Age:
            N: "25"

※ 数値型(N)は文字列として指定する点に注意。

④ Yamlを使用してDynamoDBに書き込む

作成したYamlを使って、以下のコマンドを実行します。

aws dynamodb batch-write-item \
  --cli-input-yaml file://Upload.yaml \
  --profile your-profile-name

成功すると特に出力はなく、エラーが出なければ正常終了です。

補足・注意点

  • batch-write-item は一度に 最大25件まで 
  • 1アイテムのサイズは 最大400KB 
  • 失敗したアイテムは UnprocessedItems として返ることがある 
    • 本番用途ではリトライ処理を入れるのが安全 

PS

Jsonで書いた場合は、--cli-input-yaml オプションは外して問題ありません。

aws dynamodb batch-write-item \
  --cli-input-json file://Upload.json \
  --profile your-profile-name

型には以下のようなものを指定できます。

  • 文字列:S 
  • 数値:N 
  • 真偽値:BOOL 
  • NULL:NULL 
  • リスト:L 
  • マップ:M 
  • 文字列セット:SS 
  • 数値セット:NS 

少量データの初期投入や検証用途ではかなり便利です。


参考

profile

dynamodb cmd

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?