Amazon DynamoDB is a fully managed non-relational (NoSQL) database service that provides fast and predictable performance with seamless scalability.
In other words - DynamoDB is entirely managed by AWS (so it's scaled for us, even if our startup has millions of users), it's a NoSQL database so we don't have to start by defining a detailed schema like in a relational database and it's used by companies like AirBnB, Lyft and Samsung in production.
In this quick lesson we're going to learn how to create a brand new DynamoDB table from scratch with AWS CDK.
Instructor: [0:00] In order to store the todos in our app, we are going to need a database. For that, we are going to use Amazon DynamoDB. Amazon DynamoDB is a secure, serverless, NoSQL database with limited scalability and consistent single-digit millisecond performance. There are two important factors here.
[0:15] First of all, this is a NoSQL database, so we can start putting in data right away without having to think about SQL schema and things like that. Secondly, this is a fully managed service. AWS is responsible for delivering a single-digit millisecond performance at any scale. We are going to be responsible for putting in data in our database. Let me go back to our stack.
[0:38] I'm going to start by importing the dynamodb module from "aws-cdk-lib/aws-dynamodb". Secondly, I'm going to go over here and create a new todosTable, which is going to be a new dynamodb.Table(). This is a Construct, which as usual, takes three arguments. The first argument is going to be this, the second one, I'm going to call it a "TodoDatabase", and the third argument are the props.
[1:02] The only prop that is necessary in order to create a new DynamoDB table is the partitionKey. A partitionKey is a property that needs to be present in every single item that we are going to store in our DynamoDB table, and it has to uniquely identify this particular item. A partitionKey in CDK is an object with a name and a type.
[1:22] Name, I'm going to choose the "id", and for the type, I'm going to do dynamodb.AttributeType.STRING. This is saying that we are going to create a new todosTable, and each item in this table is going to have an ID which is going to be unique for this particular item. With all of that in place, we are ready to deploy our DynamoDB table to AWS.
[1:45] Let me go ahead, open up the terminal, and run cdk diff. We can see that the only thing that's going to change is that we are going to deploy a new DynamoDB table. Let me go ahead and run cdk deploy. Our todosTable has been deployed to AWS. Let me go to AWS Console, and I'm going to go to CloudFormation. Here I'm going to visit my TodoAppStack. Let me close that, expand that a little.
[2:09] Here in the Resources section, we can see our TodoBackend construct. If I expand that, we can see the TodoDatabase. Let me click over here to go to our first created DynamoDB table. Here it is, our brand new DynamoDB table with a partition key of id, which is of type String, exactly the same as we have defined in our CDK code. I can also go ahead and click on Explore table items.
[2:34] Here, I can click on Create item. I need to provide the id, so I'm going to do 123, and also going to add a new attribute of type String, so, todo, Learn CDK. Now I can go ahead, Create an item. Here I can see that I do have a single item in our todo table. Now we are ready to continue building our serverless backend.
Member comments are a way for members to communicate, interact, and ask questions about a lesson.
The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io
Be on-Topic
Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.
Avoid meta-discussion
Code Problems?
Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context
Details and Context
Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!