DynamoDB - Creating Tables Using the AWS SDK for .NET in .NET Core

This blog post is the first part of a series where I will go through how to interact with different components that AWS offer.

The first part of the series will look at DynamoDB, this series will also be broken into separate parts so each blog post will focus on one or two pieces of functionality from each feature.

In this blog we will be looking at creating a DynamoDB client, then using that client to create a DynamoDB Table.

What is DynamoDB

DynamoDB is a fully managed NoSQL database, that provides fast performance, the data is stored on a solid state drive, this gives a predictable performance. DynamoDB is scalable, meaning that it can be scaled either up or down as needed.

Rather than purchasing storage, when you create a table you specify a provisioned throughput. Throughput is the amount of read and write activity that the table will be able to support.

Setting up an AWS Account.

In order to interact with AWS components and features, we first need to create an AWS account.

If you are unsure on how to do this, there is a great tutorial by AWS. This takes you through the steps needed to create an account.

It is worth pointing out, that AWS offer free tier services for the first year.

Creating a Project

Let's get started by creating a simple .NET Core 2.0 Web API. I will be using this dummy project as a way to show the code needed to create the AWS DynamoDB client and then how to use the client to create the DynamoDB table.

Create the client

In order to be able to interact with any AWS service, we need to create and setup a client. Our client will contain the following information, AWS region, AccessKey and SecretKey. This is the minimum information we need to interact with DynamoDB.

While there are a few ways to create the client, we will be setting it up using .NET Cores 2.0 Dependency Injection.

We first need to install the following two nuget package

AWSSDK.Extensions.NETCore.Setup

AWSSDK.DynamoDBv2

Once we have our NuGet packages installed, we can then add the following to our ConfigureServices method inside the startup.cs file. This will allow us to dependency inject the IAmazonDynamoDB client into the classes that need to interact with it.

Once we have our "AddDefaultAWSOptions()" set, this will first look for the credentials inside the .aws folder, that is stored in your user profile on your drive. For example on my computer it is store in the following location "C:\Users\username.aws" the filename where the credentials are store is "credentials" below is an example of what the credentials file looks like.

Another option instead of using the credentials file would be to store them in the appsettings.json file.

This isn’t the normal way of doing things, when storing keys inside our application, we run the risk of exposing these to the outside world, by accidentally pushing them up to your repository.

Below is a bit of a hack, but it's a way to use the credentials in your appsettings.json if that is what you are after. Add the following Environment.SetEnvironmentVariable to the ConfigureServices inside the startup.cs file.

DynamoDBController

Now that we have our client setup. We can start to create the controller that we are going to use to create a DynamoDB table.

Note: I am using a Web API to show how we can write code that will allow us to create a table, but you can add the CreateDynamoDbTable() method to any .NET Core application.

Let's create a DynamoDB Controller. Our controller will have the following endpoint
api/DynamoDb/createtable and it will call off to a ICreateTable interface. We will create the logic of adding the DynamoDB table inside the CreateTable class.

That's it, we now have the code that will allow us to create a DynamoDB Table in AWS.

Summary

To summarize we created a simple .NET Core Web API with an endpoint that when hit, will create us a DynamoDB Table. The DynamoDB table name is hard coded in this example, but if we wanted we could have added a query to our endpoint, that would allow us to enter a table name defined by the user interacting with our endpoint.

I'm using a Web API to show you a way to create a DynamoDB table, this is only an example, it's very likely that you wouldn't actually want to setup an endpoint to create a DynamoDB table. You should be able to copy and paste the code above and add it to your project where it makes sense.

Coming up in the next blog post we look at "putting items" onto our newly created DynamoDB table.