Use AWS CDK to Deploy a DynamoDB Table

Lee Robinson
InstructorLee Robinson
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 4 years ago

We'll create a new CDK Stack, defining a DynamoDB table called "Items" that is configured to pay per request with a partition key of ID.

Lecturer: [0:00] With the CDK CLI installed, let's make a new directory for our DynamoDB project. Then, let's cd into that directory. Now, we can use the CDK CLI to initialize a new application in JavaScript. It's going to apply a project template for JavaScript and run us through this interactive prompt, where it will install everything we need, to hit the ground running.

[0:35] You'll see it also outputs a few useful commands for us -- cdk deploy that will allow you to deploy this stack, cdk diff which will show what has changed, and then cdk synth if you want to see the actual underlying cloud formation template.

[0:53] Next, let's open up our newly created project inside of VS Code and take a look at the CDK stack.

[1:01] Now there's nothing here yet. Let's go down to our terminal and install a new package for DynamoDB that will allow us to setup resources inside of our stack to create a new DynamoDB Table. This installed successfully, so we can now close out our terminal and focus on this stack.

[1:26] First, let's import Dynamo from the package that we just installed. Next, let's go inside of our stack. We can remove this comment and say that we want to create a new DynamoDB Table, and we're going to call these items.

[1:53] This also takes in some optional parameters that we can use to customize our table. One of those options is the billing mode, which we can specify as PAY_PER_REQUEST, which we'll use on-demand billing.

[2:10] The other option we want is the partition key, which is the primary key for our table. We're going to say this is called a name of ID and a type of a string. We can save, and this should be all that we need to set up a new DynamoDB Table named Items that has on-demand billing with a primary key of ID. I just noticed that this should be a capitalized T.

[2:49] We can save and open up our terminal. Now, we should be able to run cdk deploy to deploy our new DynamoDB Table called Items. It's going to create a CloudFormation changeset for us and abstract away us having to manage CloudFormation ourselves. Also, provide a repeatable codified infrastructure, so that we don't have to go into the interface and create this ourselves.

[3:25] This saves a lot of time if you're trying to spin up new environments frequently or if you're pushing this code up and someone else is going to be creating this example themselves or picking up where you left of. Just like that, our DynamoDB stack was successfully created.

[3:46] If we navigate back to our AWS console, we can see that the brand new DynamoDB Table named Items was created. We can click inside here, see our partition key of ID, our on-demand capacity, and this table is now active.

[4:04] Now, you want to take note of this table name because we'll need it as an environment variable in the application that we create here very shortly.

nicoandresr rodriguez
nicoandresr rodriguez
~ 4 years ago

Here a typo in min 1:45 is dynamo.Table instead of dynamo.table

~ 3 years ago

I keep getting this error when running "cdk deploy"

throw new Error('construct does not have an associated node. All constructs must extend the "Construct" base class');

anyone else having this issue?

~ 3 years ago

I am also having that issue

Lucas Minter
Lucas Minter
~ 2 years ago

Hey, here is an issue that should hopefully solve your problem. We will look into the validity of the course with the mismatched versions in mind. https://github.com/aws/aws-cdk/issues/20370

James
James
~ 2 years ago

Yeah this tutorial is broken. I'm unable to do a 'cdk deploy' as well. This tutorial needs to be updated to be compatible with aws-cdk v2.

James
James
~ 2 years ago

Solution I found was to do the following:

  1. cdk init <app-name> --language typescript This init builds with v2 of the aws-cdk, while the --language javascript in this tutorial defaults to v1.
  2. Modify nextjs-dynamodb-stack.js to be... import { Stack, StackProps } from 'aws-cdk-lib'; import { Construct } from 'constructs';

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

export class NextjsDynamodbStack extends Stack { constructor(scope: Construct, id: string, props?: StackProps) { super(scope, id, props);

new dynamodb.Table(this, 'Items', {
  partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
  billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
});

} } 3. cdk bootstrap This is needed if it's your first time to try and do cdk deploy 4. cdk deploy

Markdown supported.
Become a member to join the discussionEnroll Today