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.
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.
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.
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"
}
}
'
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.
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"),
})
)
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