Reusable C# DynamoDB Table Creator

This post assumes that you are comfortable using C#, installing Nuget packages and using tools like Visual Studio or Visual Studio code.

To programmatically create DynamoDB tables, you will need the the following libraries

  • Amazon.DynamoDBv2
  • Amazon.DynamoDBv2.Model
  • Amazon.Runtime

We will be creating a generic DynamoDB table creator. Here is the code:

    public class TableCreator
    {
        private readonly AmazonDynamoDBClient _client;
        private readonly string _tableName;
        private readonly Dictionary<string, ScalarAttributeType> _attributes;
        private readonly Dictionary<string, KeyType> _keys;
        private readonly List<LocalSecondaryIndex> _localSecondaryIndices;
        private readonly List<GlobalSecondaryIndex> _globalSecondaryIndices;
        private readonly ProvisionedThroughput _provisionedThroughput;

        public TableCreator(AmazonDynamoDBClient client, string tableName,
            Dictionary<string, ScalarAttributeType> attributes,
            Dictionary<string, KeyType> keys,
            ProvisionedThroughput provisionedThroughput,
            List<LocalSecondaryIndex> localSecondaryIndices = null,
            List<GlobalSecondaryIndex> globalSecondaryIndices = null
            )
        {
            if (string.IsNullOrEmpty(tableName))
            {
                throw new System.ArgumentException($"'{nameof(tableName)}' cannot be null or empty.", nameof(tableName));
            }

            _client = client ?? throw new System.ArgumentNullException(nameof(client));
            _tableName = tableName;
            _attributes = attributes ?? throw new System.ArgumentNullException(nameof(attributes));
            _keys = keys ?? throw new System.ArgumentNullException(nameof(keys));
            _localSecondaryIndices = localSecondaryIndices;
            _globalSecondaryIndices = globalSecondaryIndices;
            _provisionedThroughput = provisionedThroughput;
        }


        public async Task<CreateTableResponse> Create()
        {
            var request = new CreateTableRequest();
            request.AttributeDefinitions.AddRange(_attributes.Select(x => new AttributeDefinition(x.Key, x.Value)));
            request.KeySchema.AddRange(_keys.Select(x => new KeySchemaElement(x.Key, x.Value)));
            if (_localSecondaryIndices != null) request.LocalSecondaryIndexes.AddRange(_localSecondaryIndices);
            if(_globalSecondaryIndices != null)request.GlobalSecondaryIndexes.AddRange(_globalSecondaryIndices);
            request.ProvisionedThroughput = _provisionedThroughput;
            return await _client.CreateTableAsync(request);
        }
    }

Table Creator Constructor

The table creator requires an instance of the AmazonDynamoDBClient, the desired table name, a list of attributes, which ones are part of the primary or composite key, the read and write capacity units encased in the ProvisionedThroughput entity and any local and global secondary indices you might want to create. Here is a brief description of some of the constructor parameters.

AmazonDynamoDBClient

Provides access to Amazons DynamoDB service.

ScalarAttributeType

There are three primary scalar attribute types N, S and B where N represents number, S string and B boolean.

KeyType

When creating a key schema element, you need to specify both the attribute name as string and the key type which can either be Hash or Range. Hash for partition key and Range for sort key

LocalSecondaryIndex

These are indexes that share partition key with the table but use a different sort key

GlobalSecondaryIndex

Unlike local secondary indices, global secondary indices have different partition and sort keys

ProvisionedThroughput

The number of read and write capacity units for the table

In the next post I will explain how this tool can be used to create custom/specific DynamoDB tables.