Localstack DynamoDB + AWS CDK on your local with NoSQL Workbench

Kobe
4 min readAug 7, 2021

--

This article shows you how to setup dynamo DB for local development using LocalStack & AWS CDK

This article is the next for the article setup local stack & AWS CDK repository in section #1 below continue we will set up a Dynamo DB for our development.

Requirement

We need a dynamo table with some configurations below

Table Definition

  • Table Name: test-development
  • Partition Key: (pk : String)
  • Sort Key: (sk : String )
  • Provisioned Throughput: Read/Write Capacity 5
  • Stream Specification: Both the new and the old item images of the item are written to the stream (NEW_AND_OLD_IMAGES)

Global Secondary Index

  • 3 GSI with name (pk2-sk2-index | pk3-sk3-index | pk4-sk4-index)
  • Partition Key & Sort Key : String
  • Default Provisoned Throughput: Read/Write = 5

Create a util method create a standard GSI

export const createStandardGSI = (indexName: string, pkName: string, skName: string) : GlobalSecondaryIndexProps => {
return {
indexName,
partitionKey: { name: pkName, type: AttributeType.STRING },
sortKey: { name: skName, type: AttributeType.STRING },
projectionType: ProjectionType.ALL,
readCapacity: 5,
writeCapacity: 5,
};
}

In the Application Stack, we will make a code change to generate the dynamo table.

import * as dynamodb from "@aws-cdk/aws-dynamodb";
import { GlobalSecondaryIndexProps, StreamViewType } from "@aws-cdk/aws-dynamodb/lib/table";
import { createStandardGSI } from "../utils/createGlobalSecondaryIndex";
...
...
const table = new dynamodb.Table(this, 'Table', {
tableName: 'test-development',
// partition key & sort key
partitionKey: { name: 'pk', type: dynamodb.AttributeType.STRING },
sortKey: {name : 'sk', type: dynamodb.AttributeType.STRING},
// ProvisionedThroughput
readCapacity: 5,
writeCapacity: 5,
// StreamSpecification
stream: StreamViewType.NEW_AND_OLD_IMAGES,
});

const pk2Sk2Index: GlobalSecondaryIndexProps = createStandardGSI('pk2-sk2-index', 'pk2', 'sk2');
const pk3Sk3Index: GlobalSecondaryIndexProps = createStandardGSI('pk3-sk3-index', 'pk3', 'sk3');
const pk4Sk4Index: GlobalSecondaryIndexProps = createStandardGSI('pk4-sk4-index', 'pk4', 'sk4');

table.addGlobalSecondaryIndex(pk2Sk2Index);
table.addGlobalSecondaryIndex(pk3Sk3Index);
table.addGlobalSecondaryIndex(pk4Sk4Index);
}

Deploy our Stack

cdklocal deploy — profile localstack

ApplicationStack: deploying...
ApplicationStack: creating CloudFormation changeset...
✅ ApplicationStackStack ARN:
arn:aws:cloudformation:us-east-1:000000000000:stack/ApplicationStack/e67d04e2

Clarify our dynamo table

aws — endpoint-url=http://localhost:4566 dynamodb list-tables

{
"TableNames": [
"test-development"
]
}

Verify our table setting

aws — endpoint-url=http://localhost:4566 dynamodb describe-table — table-name test-development > test-development.json

{
"Table": {
"AttributeDefinitions": [
{
"AttributeName": "pk",
"AttributeType": "S"
},
{
"AttributeName": "sk",
"AttributeType": "S"
},
{
"AttributeName": "pk2",
"AttributeType": "S"
},
{
"AttributeName": "sk2",
"AttributeType": "S"
},
{
"AttributeName": "pk3",
"AttributeType": "S"
},
{
"AttributeName": "sk3",
"AttributeType": "S"
},
{
"AttributeName": "pk4",
"AttributeType": "S"
},
{
"AttributeName": "sk4",
"AttributeType": "S"
}
]
,
"TableName": "test-development",
"KeySchema": [
{
"AttributeName": "pk",
"KeyType": "HASH"
},
{
"AttributeName": "sk",
"KeyType": "RANGE"
}
]
,
"TableStatus": "ACTIVE",
"CreationDateTime": "2021-08-07T19:23:03.601000+07:00",
"ProvisionedThroughput": {
"LastIncreaseDateTime": "1970-01-01T07:00:00+07:00",
"LastDecreaseDateTime": "1970-01-01T07:00:00+07:00",
"NumberOfDecreasesToday": 0,
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5
},
"TableSizeBytes": 0,
"ItemCount": 0,
"TableArn": "arn:aws:dynamodb:us-east-1:000000000000:table/test-development",
"GlobalSecondaryIndexes": [
{
"IndexName": "pk3-sk3-index",
"KeySchema": [
{
"AttributeName": "pk3",
"KeyType": "HASH"
},
{
"AttributeName": "sk3",
"KeyType": "RANGE"
}
]
,
"Projection": {
"ProjectionType": "ALL"
},
"IndexStatus": "ACTIVE",
"ProvisionedThroughput": {
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5
},
"IndexSizeBytes": 0,
"ItemCount": 0,
"IndexArn": "arn:aws:dynamodb:ddblocal:000000000000:table/test-development/index/pk3-sk3-index"
},
{
"IndexName": "pk2-sk2-index",
"KeySchema": [
{
"AttributeName": "pk2",
"KeyType": "HASH"
},
{
"AttributeName": "sk2",
"KeyType": "RANGE"
}
]
,
"Projection": {
"ProjectionType": "ALL"
},
"IndexStatus": "ACTIVE",
"ProvisionedThroughput": {
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5
},
"IndexSizeBytes": 0,
"ItemCount": 0,
"IndexArn": "arn:aws:dynamodb:ddblocal:000000000000:table/test-development/index/pk2-sk2-index"
},
{
"IndexName": "pk4-sk4-index",
"KeySchema": [
{
"AttributeName": "pk4",
"KeyType": "HASH"
},
{
"AttributeName": "sk4",
"KeyType": "RANGE"
}
]
,
"Projection": {
"ProjectionType": "ALL"
},
"IndexStatus": "ACTIVE",
"ProvisionedThroughput": {
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5
},
"IndexSizeBytes": 0,
"ItemCount": 0,
"IndexArn": "arn:aws:dynamodb:ddblocal:000000000000:table/test-development/index/pk4-sk4-index"
}
]
,
"StreamSpecification": {
"StreamEnabled": true,
"StreamViewType": "NEW_AND_OLD_IMAGES"
},
"LatestStreamLabel": "2021-08-07T12:23:03.601",
"LatestStreamArn": "arn:aws:dynamodb:us-east-1:000000000000:table/test-development/stream/2021-08-07T12:23:03.601"
}
}

Dynamo DB Viewer Tool

Install NoSQL Workbench

Please refer the Github for this section:

--

--

Kobe
Kobe

Written by Kobe

I’m working at KMS-Technology company. I love code (▀̿Ĺ̯▀̿ ̿) — Full Stack Software Engineer

Responses (1)