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動かせるのか! ~ テーブルを作成するためのスクリプト付き ~

Last updated at Posted at 2025-05-07

DynamoDBをローカル環境で動かす理由

サーバレスアプリケーションを構築するためのAWSサービスで、AWS SAMというサービスがあります。これを使用すると、Lambda関数やDynamoDB, API Gatewayなどのサービスを一括でビルド・デプロイができるのですが、これをデプロイする前にローカル環境でテストをしたい場合があります。

AWSマネジメントコンソール上でLambda関数の作成する時に、json形式でパラメータを渡してテストを実行できる機能があります。これをローカル環境でやってしまうこともできます。

Lambda関数だけで処理が完結する場合は、以降の記事は必要ないです。

ローカル環境でLambda関数からDynamoDBへデータの閲覧・登録・更新・削除などを行う場合に、DynamoDBをローカル環境で動かしたくなります。

Docker使います!

あらかじめDocker Desktopをインストールしておいてください。

コマンドを叩くだけ!

以下のコマンドを実行するだけで、DynamoDB Local用のコンテナが起動してくれます。

$ docker run -d -p 8000:8000 --name dynamodb-local amazon/dynamodb-local

以下のコマンドが実行できれば、コンテナが起動しています。

$ aws dynamodb list-tables --endpoint-url http://localhost:8000 --output json

おまけ1: DynamoDB Localにテーブルを作成するスクリプト

1. 任意のディレクトリにスクリプトファイルを作成

今回は、ルートディレクトリに create-table-to-dynamodb.shとして作成します。

create-table-to-dynamodb.sh
#!/bin/bash

# DynamoDBのエンドポイントを指定
ENDPOINT_URL="http://localhost:8000"

# 同名のテーブルの存在チェック
create_table_if_not_exists() {
  local table_name=$1
  local create_command=$2

  echo "🚧 Creating $table_name..."

  # 既にテーブルが存在するか確認
  if aws dynamodb describe-table --table-name "$table_name" --endpoint-url "$ENDPOINT_URL" &>/dev/null; then
    echo "✅ $table_name already exists. Skipping."
  else
    eval "$create_command"
    echo "✅ Created $table_name."
  fi
}

# 各テーブルの作成コマンドを定義
create_table_if_not_exists "user-groups-dev" "
aws dynamodb create-table \
  --table-name user-groups-dev \
  --attribute-definitions AttributeName=group_id,AttributeType=S \
  --key-schema AttributeName=group_id,KeyType=HASH \
  --billing-mode PAY_PER_REQUEST \
  --endpoint-url $ENDPOINT_URL
"

create_table_if_not_exists "users-dev" "
aws dynamodb create-table \
  --table-name users-dev \
  --attribute-definitions AttributeName=group_id,AttributeType=S AttributeName=user_id,AttributeType=S \
  --key-schema AttributeName=group_id,KeyType=HASH AttributeName=user_id,KeyType=RANGE \
  --billing-mode PAY_PER_REQUEST \
  --endpoint-url $ENDPOINT_URL
"

create_table_if_not_exists "tasks-dev" "
aws dynamodb create-table \
  --table-name tasks-dev \
  --attribute-definitions AttributeName=user_id,AttributeType=S AttributeName=task_id,AttributeType=S \
  --key-schema AttributeName=user_id,KeyType=HASH AttributeName=task_id,KeyType=RANGE \
  --billing-mode PAY_PER_REQUEST \
  --endpoint-url $ENDPOINT_URL
"

2. スクリプトを実行

以下のコマンドを叩きます。

./create-table-to-dynamodb.sh

おまけ2: Dockerコンテナの止め方

$ docker stop dynamodb-local
$ docker rm -f dynamodb-local
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?