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?

docker-composeでdynamoDB-localをGithub Actions上で実行する際に注意すること

Posted at

概要

業務でdynamodb-localをローカル環境上でデプロイし、デプロイしたdynamodb-localをGithub Actions上で実行しようとしたが、DynamoDBをローカル環境上で単一のデータベースファイル作成にした上で、volumeで単一データベースファイルをマウントしていなかった影響でGithub ActionsのCI上でのテストコードが失敗してしまった。

対応

docker-compose.yamlにデプロイされた際、作成される単一データベースファイルをvolumeでコンテナ内の/home/dynamodblocal/dataにマウントすることによって、単一データベースがローカル環境に直接作成されることなく、コンテナ内で作成されるようになり、Github Actionsからテスト実行時に指定されたエンドポイントへアクセスすることが可能である。

以下のコンテナ構成は例である

docker-compose.yaml
version: "3.8"
  dynamodb-local:
    command: "-jar DynamoDBLocal.jar -sharedDb ./data"
    image: amazon/dynamodb-local:latest
    container_name: dynamodb-local
    ports:
      - "8000:8000"
    working_dir: /home/dynamodblocal
    restart: always
    volumes:
      - dynamodb-local:/home/dynamodblocal/data

  aws-cli:
    image: amazon/aws-cli:latest
    container_name: aws-cli
    ports:
      - "8080:8080"
    depends_on:
      - dynamodb-local
    entrypoint: "sh -c '/home/awscli/init-dynamodb-local.sh; tail -f /dev/null'"
    environment:
      - AWS_ACCESS_KEY_ID=dummy
      - AWS_SECRET_ACCESS_KEY=dummy
      - AWS_DEFAULT_REGION=ap-northeast-1
    volumes:
      - ./docker/local/dynamodb:/home/awscli
    working_dir: /home/awscli

  dynamodb-admin:
    container_name: dynamodb_admin
    image: aaronshaf/dynamodb-admin
    ports:
      - "8001:8001"
    environment:
      DYNAMO_ENDPOINT: http://dynamodb-local:8000
    depends_on:
      - dynamodb-local

# DynamoDB永続化ボリューム(コンテナ停止してもdynamodb-local内のデータが消えないようにする)
volumes:
  dynamodb-local:

後は、Github Actionsからaws-cliコンテナを経由してDBデータを作成する。
作成する際は、entrypointを利用した上で、シェルスクリプトから作成すると、自動で作成される。

以下は、DBデータ作成例のシェルである。

init-dynamodb-local.sh
#!/bin/bash
set -e
sleep 5

# テーブル作成
aws dynamodb create-table --table-name First_Term_GAT-X_Series \
  --region ap-northeast-1 \
  --endpoint-url http://dynamodb-local:8000 \
  --attribute-definitions AttributeName=Model_Number,AttributeType=N \
  --key-schema AttributeName=Model_Number,KeyType=HASH \
  --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \
  --no-cli-pager;
  
aws dynamodb create-table --table-name Later_Term_GAT-X_Series \
  --region ap-northeast-1 \
  --endpoint-url http://dynamodb-local:8000 \
  --attribute-definitions AttributeName=Model_Number,AttributeType=N \
  --key-schema AttributeName=Model_Number,KeyType=HASH \
  --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \
  --no-cli-pager;

# データ追加
aws dynamodb batch-write-item \
  --region ap-northeast-1 \
  --endpoint-url http://dynamodb-local:8000 \
  --request-items '{
    "First_Term_GAT-X_Series": [
      {
        "PutRequest": {
          "Item": {
            "Model_Number": {"N": "102"},
            "Mobile_Suit_Name": {"S": "DUEL"}
          }
        }
      },
      {
        "PutRequest": {
          "Item": {
            "Model_Number": {"N": "103"},
            "Mobile_Suit_Name": {"S": "BUSTER"}
          }
        }
      },
      {
        "PutRequest": {
          "Item": {
            "Model_Number": {"N": "105"},
            "Mobile_Suit_Name": {"S": "STRIKE"}
          }
        }
      },
      {
        "PutRequest": {
          "Item": {
            "Model_Number": {"N": "207"},
            "Mobile_Suit_Name": {"S": "BLITZ"}
          }
        }
      },
      {
        "PutRequest": {
          "Item": {
            "Model_Number": {"N": "303"},
            "Mobile_Suit_Name": {"S": "AEGIS"}
          }
        }
      }
    ]
  }';

aws dynamodb batch-write-item \
  --region ap-northeast-1 \
  --endpoint-url http://dynamodb-local:8000 \
  --request-items '{
    "Later_Term_GAT-X_Series": [
      {
        "PutRequest": {
          "Item": {
            "Model_Number": {"N": "131"},
            "Mobile_Suit_Name": {"S": "CALAMITY"}
          }
        }
      },
      {
        "PutRequest": {
          "Item": {
            "Model_Number": {"N": "252"},
            "Mobile_Suit_Name": {"S": "FORBIDDEN"}
          }
        }
      },
      {
        "PutRequest": {
          "Item": {
            "Model_Number": {"N": "370"},
            "Mobile_Suit_Name": {"S": "RAIDER"}
          }
        }
      }
    ]
  }';

参考サイト

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?