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.