Build DynamoDB local with Docker

background

My current product uses AppSync, and I often use Lambda as the backend. At that time, I wrote a unit test for Lambda's logic, but I put a hold on the interaction with DynamoDB. As the development progressed, various processes came in, and in the situation where "I do not know until I actually access it", various inconveniences appeared, so I decided to put in a test and investigated it. I will.

The entire code in this article has been uploaded to yu-croco / DynamoDB-local-Sample, so feel free to Please use it.

Postscript

I noticed it after posting, so I added it. It seems impossible to access the Docker container built on Circle CI from the local Circle CI (example: http: // localhost: 8000), so I made it Docker on Circle CI as in this article. When using DynamoDB local, please also convert Golang resources to Docker.

DynamoDB local DynamoDB local is a DynamoDB provided by AWS that can be started locally. According to the AWS official Setting Up DynamoDB Local

The downloadable version of Amazon DynamoDB allows you to develop and test your application without accessing the DynamoDB web service. Instead, the database is self-contained on the computer. When you're ready to deploy your application to production, delete the local endpoint in your code. Then this refers to the DynamoDB web service.

Using this local version makes it easier to save throughput, data storage and data transfer charges. Also, you don't need to be connected to the internet while developing your application.

... apparently ... "It's a hassle to hit DynamoDB one by one," so I feel that they are offering something more handy.

This time I will use this.

Try to make it Docker

I don't want to install DynamoDB local directly every time because I want to use it on CI. Fortunately, DynamoDB local uses this because AWS officially publishes the image on Docker Hub. amazon/dynamodb-local - Docker Hub

The Dockerfile looks like this. I think you should refer to the official for options etc. DynamoDB Local Usage Notes

FROM amazon/dynamodb-local

CMD ["-jar", "DynamoDBLocal.jar", "-sharedDb", "-dbPath", ".", "-optimizeDbBeforeStartup"]
$ docker build -t dynamodb_sample .
$ docker run -p 8000:8000 -v dynamodb:/home/dynamodblocal dynamodb_sample

If you do, it will move roughly.

Put data in DynamoDB

Create a table and input data to the started DynamoDB local. It's a hassle to write code for this, so this time I'll put the data via the AWS CLI. Start the container for AWS CLI and execute the following script to perform table operations on the container of DynamoDB local.

** For table creation **

#!/usr/bin/env bash

aws dynamodb \
  --region ap-northeast-1 \
  --endpoint-url http://dynamodb:8000 \
    create-table \
  --table-name SampleTable \
  --attribute-definitions \
    AttributeName=userId,AttributeType=N \
    AttributeName=userName,AttributeType=S \
  --key-schema \
    AttributeName=userId,KeyType=HASH AttributeName=userName,KeyType=RANGE \
  --billing-mode PAY_PER_REQUEST

** For data input **

#!/usr/bin/env bash

aws dynamodb \
  --region ap-northeast-1 \
  --endpoint-url http://dynamodb:8000 \
    put-item \
  --table-name SampleTable \
  --item '
    {
      "userId": {
        "N": "1"
       },
      "userName": {
        "S": "Yamada Taro"
      },
      "age": {
        "N": "29"
      },
      "contactNumber": {
        "S": "080-1234-5678"
      }
    }
  '

Approximate overview

In the end, it looks like the following.

** Dockerfile (for AWS CLI) **

FROM amazon/aws-cli:2.0.56

ENV AWS_ACCESS_KEY_ID=fake_access_key\
    AWS_SECRET_ACCESS_KEY=fake_secret_access_key\
    DYNAMODB_REGION=ap-northeast-1
#Create the process you want to execute from the AWS CLI with shellscript under bin
COPY bin bin

docker-compose.yml

version: '3'
services:
  dynamodb:
    image: amazon/dynamodb-local
    command: -jar DynamoDBLocal.jar -sharedDb -dbPath . -optimizeDbBeforeStartup
    volumes:
      - dynamodb:/home/dynamodblocal
    ports:
      - 8000:8000
  awscli:
    build: .
    entrypoint: [""]
    tty: true
    command:
      - /bin/sh
volumes:
  dynamodb:
    driver: local

This will docker-compose up and launch the DynamoDB local and AWS CLI containers respectively. After that, hit the shell script contained in the bin file to create a table.

It would be nice if the table could be created automatically after the container was started, but that would be another opportunity.

Session settings

In the case of Golang, if you set Endpoint to http://0.0.0.0:8000 in the process of defining a session to DynamoDB, the existing DynamoDB process will work.

const region = "ap-northeast-1"
var svc = dynamodb.New(session.New(&aws.Config{
		Region:   aws.String(region),
		Endpoint: aws.String("http://0.0.0.0:8000"),
	})
)

Summary

I used DynamoDB local to test the process of accessing DynamoDB. Since it is troublesome to input test data to DynamoDB local, it is recommended to start Docker for AWS CLI and input data to DynamoDB local from there (because it can also be used on CI).

Let's have a happy DynamoDB life!

Recommended Posts

Build DynamoDB local with Docker
Build Couchbase local environment with Docker
Get started with DynamoDB with docker
Build docker environment with WSL
Build WordPress environment with Docker (Local) and AWS (Production)
Build a Node.js environment with Docker
Build environment with vue.js + rails + docker
Build Rails environment with Docker Compose
Build docker + laravel environment with laradock
Build WebRTC Janus with Docker container
Spring Boot gradle build with Docker
Build a PureScript development environment with Docker
Build a Wordpress development environment with Docker
[Docker] Build Jupyter Lab execution environment with Docker
Build an environment with Docker on AWS
Build TensorFlow operation check environment with Docker
How to build Rails 6 environment with Docker
Build a local development environment for Rails tutorials with Docker (Rails 6 + PostgreSQL + Webpack)
Docker + DynamoDB local + C ++ environment construction and practice
Build a Laravel / Docker environment with VSCode devcontainer
Build a WordPress development environment quickly with Docker
Build mecab (NEologd dictionary) environment with Docker (ubuntu)
[Rails] How to build an environment with Docker
[First team development ②] Build an environment with Docker
Wordpress local environment construction & development procedure with Docker
Launch MariaDB with Docker
Rails deploy with Docker
Run Pico with docker
Explode Docker with WSL2
Use Puphpeteer with Docker
Operate Emby with Docker
Try WildFly with Docker
Run Payara with Docker
Build Growai with Centos7
[Docker] Connection with MySQL
Try local development of AWS (S3, DynamoDB) with AWS SDK for JavaScript and Docker
Php settings with Docker
Getting Started with Docker
Build Java with Wercker
Disposable PHP with Docker
Build bazel with alpine
Install Composer with Docker
Build Elastic Stack with Docker and analyze IIS logs
How to build docker environment with Gradle for intelliJ
Build an environment of Ruby2.7.x + Rails6.0.x + MySQL8.0.x with Docker
[Note] Build a Python3 environment with Docker in EC2
[Note] I suddenly can't build with Docker for windows.
(For myself) Build gitlab with ubuntu18.04 + docker for home (Note)
Build Java development environment with WSL2 Docker VS Code
Build Rails (API) x MySQL x Nuxt.js environment with Docker
Build an Android image for Orange Pi 4 with Docker
Build a local development environment for Open Distro for Elasticsearch with multiple nodes using Docker
Pytorch execution environment with Docker
Use GDAL with Python with Docker
Build GitLab / Mattermost with DockerForWindows
Build softether VPN with Centos7.
Deploy with EC2 / Docker / Laravel
Run TAO Core with Docker
Docker management with VS Code
Set up GitLab with docker
Build Docker Image lightweight and fast with CodeBuild with Santa Banner