Hands-on DynamoDB


RDS: Relational Database Service
Going NoSQL with DynamoDB
Start course

Databases are among the most used applications in the cloud (or anywhere else, for that matter). Managing data is exactly what computers were invented for, so it should come as no surprise that a great deal of attention is focused on the many different Database Management Systems and Data Management tools that are available.

This course will cover AWS's database solutions. It is split in two parts, the first is dedicated to the two most important Database services in the Amazon family: RDS, a relational database supporting DBMS like MySQL, PostgreSQL, Oracle, and MSSQL; and DynamoDB, a powerful NoSQL DBMS adopting the key-value data model. The second part is about the more advanced AWS database systems like Elasticache, RedShift, and SimpleDB.


Who should take this course

For this beginner course, you'll require no special prerequisites. Nevertheless, some experience with Databases and at least a basic knowledge of the related jargon might be helpful. If you are completely new to the cloud, you might benefit from our Introduction to Cloud Computing course. You might also find the AWS general introduction course interesting interesting if you are not yet that familiar with the AWS cloud platform.

If you want to test your knowledge of the basic topics covered by this course, we strongly suggest you take our quiz questions. Another nice follow up to this course is our RDS lab, where you can get your hands dirty with a real RDS instance in the cloud, and Databases on AWS - part 2.


To work with Amazon DynamoDB you must determine the programming language you want to use and download the appropriate AWS Software Development Kit, SDK. Java, .

NET and PHP are available. To learn how to work with Amazon DynamoDB we demonstrate one of the sample use cases, which we presented in AWS websites for DynamoDB.

Amazon Web Services maintains several forums for customers to engage with the developer community, ask questions or reply to other customer queries. AWS maintains one or more forums for each of its services. Customers go to a forum, start a thread by posting a message. Over time each thread receives one or more replies. In this exercise, we model this application by creating the three different tables, as you can see on the screen. Now, we want to use DynamoDB to make these tables, so sign into the AWS management console and open the Amazon DynamoDB console. First of all, we have to choose our preferred region where we want to create our database from the region panel. Click on Create Table to create the first table. The Create Table wizard opens. We set the table name and its primary key according to the example which we had, and then click Continue. In the next page of Create Table wizard you can add indexes on your table. In our example, we need to set this feature for the reply table. A local secondary index allows you to perform queries against an attribute that is not a part of the primary key. So we'll create a local secondary index on the Posted By attribute of the reply table.

But now we have to finish the procedure of creating the first table, so just click on Continue. In the next page we have to specify the provisioned throughput capacity. Amazon DynamoDB lets you specify how much read and write throughput capacity you wish to provision for your table. Using this information, Amazon will provision the appropriate resources to meet your throughput needs. There is a cost associated with the provisioned throughput. For guidance in determining values, check the box for help estimating provision throughput. You'll see the provision throughput calculator section.

This tool will help you determine your read and write capacity requirements. Before continuing this procedure, let's have a deeper look at these features.

DynamoDB allocates and reserves resources to handle your throughput requirements with sustained low latency and you pay for the hourly reservation of these resources.

During the table creation, you specify your throughput requirements in terms of the read and write capacity units.

DynamoDB uses these capacity units to provision sufficient resources to provide the requested throughput. Read capacity units, the number of strongly consistent reads per second of items up to 4 KB in size per second. For eventually consistent reads, one capacity unit is two reads per second for items up to 4 KB. White capacity units, the number of 1 KB writes per second. For example, when you request 10 write capacity units, you are requesting a throughput of 10 writes per second of 1 KB size per second for that table. When deciding the capacity units for your table, you must take some factors into consideration. Item size. DynamoDB allocates resources for your table according to the number of read or write capacity units that you specify. These capacity units are based on a data item size of 4 KB per read, or 1 KB per write.

For example, if the items in your table are 4 KB or smaller, each item read operation will consume one read capacity unit. If your items are larger than 4 KB, each read operation consumes additional capacity units, in which case you can perform fewer database read operations per second than the number of read capacity units you have provisioned. For example, if you request 10 capacity units for a table but your items are 8 KB in size, then you will get a maximum of five strongly consistent reads per second on that table. Expected read and write request rates. You must also determine the expected number of read and write operations your application will perform against the table per second. This along with the estimated item size helps you to determine the read and write capacity unit values. Consistency. Read capacity units are based on strongly consistent read operations, which require more effort and consume twice as many database resources as eventually consistent reads. For example, a table that has 10 read capacity units of provision throughput would provide either 10 strongly consistent reads per second of 4 KB items or 20 eventually consistent reads per second of the same items. Whether your application requires strongly or eventually consistent reads is a factor in determining how many read capacity units you need to provision for your table. By default, DynamoDB read operations are eventually consistent. Some of these operations allow you to specify strongly consistent reads. As per the mentioned information, if you want to use the provision throughput calculator application, first select your average item size and make sure it's less than 1 KB or more. In case of more, put your estimation in the box. Our estimation about the average item size in this example is less than 1 KB. Then we should estimate the number of items which will be read from the database per second and the type of read consistency.

Also we have to estimate the number of items which will be written to the database per second. As you can see on screen, our estimate for items read per second is ten, and for items written per second is five.

Also we choose strongly consistent for read consistency. By clicking on Calculate, the application calculates the number of read and write capacity units.

These throughput values allow you up to ten 4 KB read operations and up to five 1 KB write operations per second. By checking the Override These Values box, these values we're using, and we can go to the next step for Continue. Note that it is not an obligation to use the provision throughput calculator application for estimating the read and write items units.

In this case, you can leave the Help Me Calculate How Much Throughput Capacity I Need provision option unchecked and write your estimation in read and write capacity unit boxes directly. In this page, you can set throughput alarm for your table.

Select the Use Basic Alarms checkbox, which automatically configures Cloud Watch alarms to notify you when your consumption reaches 80% of the table's provision throughput. By default, the alarm is set to send an email to the AWS account used when creating the table. You can edit the send notification text box and specify additional email addresses that are separated by commas. By clicking on Continue you can review the specifications for the table. Remember that the hash key, range key, and local secondary index details cannot be changed after the table is created, so check them carefully if they're correct. Then the table will be created by clicking on Create.

Follow the screen about creating and the rest of our examples tables. The console shows the list of tables we created. You must wait for the status of all the tables to become active. The console also shows the details, indexes monitoring, and alarm setup tabs that provide additional information about the selected table. Now we want to upload sample data to the tables we've created.

In this case we need to choose the application development platform we want to use to explore DynamoDB. For this example, we use PHP code to do the procedure. But just a reminder, you should do some pre-steps to create and run the PHP code on AWS. First you need to download and install the AWS SDK for PHP, and then verify that your environment meets the minimum requirements for that.

Depending on the installation method you use, you might have to modify your code to resolve dependencies among the PHP extensions.

Then you should configure your credentials provider for the AWS SDK for PHP. For this reason, you should create the credentials file in ~/.aws/credentials, where ~ represents your HOME directory. Inside the file you'll write your AWS access key ID and secret access key, like this. Now you can use your credentials profile to instantiate a new client in your code and you can set the default region for your client. Okay, with this short introduction you're ready to write your code to upload your data inside the tables in DynamoDB.

The PHP code example adds items to your existing tables using the PHP command put item. By this code example, we put two items in the form table and four items in the reply table. After running this program, you can use the AWS management console to view the data that you loaded in the tables. To view table data, sign into the AWS management console and open the Amazon DynamoDB console. In the tables pane, select the one table which you created before. For example, we choose reply table. Then click Explore Table to view the items which we've uploaded.

The Browse Items tab lists the items in the table. Now in this step we want to query the reply table for all replies posted less than fourteen days ago for a forum thread.

The request specifies the table name, the primary key hash attribute value, and a condition on the range attribute, reply, date, time, to retrieve replies posted after a specific date. As you can see, the items which meet this condition have been printed.

About the Author
Mohammad Ali Tabibi
Software Engineer

Computer Engineer and Cloud Expert