Download DynamoDB

wget https://s3.ap-southeast-1.amazonaws.com/dynamodb-local-singapore/dynamodb_local_latest.tar.gz

Extract file

tar zxvf dynamodb_local_latest.tar.gz

Start Dynamo DB locally

We can start Dynamo DB service with docker or with JAVA.

  1. start with docker

edit docker-compose.yml

version: '3.8'
services:
  dynamodb-local:
    command: "-jar DynamoDBLocal.jar -sharedDb -optimizeDbBeforeStartup -dbPath ./data"
    image: "amazon/dynamodb-local:latest"
    container_name: dynamodb-local
    ports:
      - "8000:8000"
    volumes:
      - "./docker/dynamodb:/home/dynamodblocal/data"
    working_dir: /home/dynamodblocal

start container

docker-compose up -d
  1. start with JAVA directly
java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb

Create fake aws credential

  1. edit .aws/config
[profile default]
output = text
region = ap-southeast-1
  1. edit .aws/credentials
[default]
aws_access_key_id = fakeMyKeyId
aws_secret_access_key = fakeSecretAccessKey

Create demo table

  1. create table json file, user_table.json
{
  "TableName": "user-demo-table",
  "AttributeDefinitions": [
    {
      "AttributeName": "user_name",
      "AttributeType": "S"
    }],
  "KeySchema": [{
    "KeyType": "HASH",
    "AttributeName": "user_name"
  }],
  "ProvisionedThroughput": {
    "WriteCapacityUnits": 5,
    "ReadCapacityUnits": 5
  }
}
  1. create table
aws dynamodb create-table --region ap-southeast-1 --cli-input-json file://user_table.json --endpoint-url http://localhost:8000

Connect DB via golang

	const (
	REGION     = "ap-southeast-1"
	EndPoint = "http://localhost:8000"
)
	sess, err := session.NewSession(&aws.Config{
		Region: aws.String(REGION),
		Endpoint: aws.String(EndPoint),
	})
    db := dynamodb.New(sess)

Integration with lambda locally

  1. edit tempalte.yaml and samconfig.toml for lambda delopment with SAM
  2. change DB endpoint to
http://docker.for.mac.localhost:8000
  1. start local lambda api
sam local start-api -p 3000
  1. test api with cURL
curl -X POST http://127.0.0.1:3000/demo