Creating an Amazon DynamoDB Table

3.1. In lib/cdk-msg-app-backend-stack.ts, add the following below the last import.

import * as dynamodb from '@aws-cdk/aws-dynamodb';

3.2. In lib/cdk-msg-app-backend-stack.ts, replace the comment “The code that defines your stack goes here” at the end of the constructor with the following code.

    const table = new dynamodb.Table(this, 'Messages', {
      partitionKey: {
        name: 'app_id',
        type: dynamodb.AttributeType.STRING
      sortKey: {
        name: 'created_at',
        type: dynamodb.AttributeType.NUMBER
      billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
      removalPolicy: cdk.RemovalPolicy.DESTROY, // NOT recommended for production code
    new cdk.CfnOutput(this, 'TableName', { value: table.tableName });

First CDK code

3.3. Save it and make sure it builds and creates a stack.

npm run build
cdk synth

3.4. Deploy the stack.

cdk deploy

CDK First Deploy

3.5. Open the Amazon DynamoDB console in Tables section, you will see the messages table.

3.6 Go to your Node.js project folder.

cd ~/environment/msg-app-backend/

3.7. Obtain the DynamoDB Table name created and export it as an environment variable with the following command.

export MY_TABLE_NAME=`aws cloudformation describe-stacks --stack-name CdkMsgAppBackendStack | jq '.Stacks[0].Outputs[0].OutputValue' | tr -d \"`

3.8. Feed the DynamoDB Table with some messages.

python db/

3.9. Open the Amazon DynamoDB console in Tables section, select your table and explore the Items inserted.

DynamoDb Table