Jon Gear
Jon Gear

Reputation: 1018

DIfficulty adding a Global Secondary Index to an existing table in DynamoDB

Table: EmailMessages

EntityId - HashKey : String

Id - RangeKey : String

Name - String

Status - String

I would like to make a GlobalSecondaryIndex for Status

var request = new UpdateTableRequest
        {
            TableName = "EmailMessages",
            GlobalSecondaryIndexUpdates = new List<GlobalSecondaryIndexUpdate>
            {
                new GlobalSecondaryIndexUpdate
                {
                    Create = new CreateGlobalSecondaryIndexAction
                    {
                        IndexName = "GSI_EmailMessages_Status",
                        KeySchema = new List<KeySchemaElement>
                        {
                            new KeySchemaElement("Status", KeyType.HASH)
                        }
                    }
                }
            }
        };

        var client = DynamoDBManager.DBFactory.GetClient();

        client.UpdateTable(request);

However the return error I get is a 500 error with no return text so I'm unsure on what I need to correct to make this work. I've dug through the documentation and I can't seem to find much help on creating a GSI for an existing table. Any help would be much appreciated

Upvotes: 1

Views: 511

Answers (1)

Jon Gear
Jon Gear

Reputation: 1018

I managed to solve my issue. Turns out I needed a lot more code. I'm going to post my solution in case someone else runs into this similar issue since there doesn't seem to be many resources out there for Dynamo. Please note my read and write capacities are very low because this is for a dev environment. You might want to consider upping yours depending on your needs

var request = new UpdateTableRequest
        {
            TableName = "EmailMessage",
            AttributeDefinitions = new List<AttributeDefinition>
            {
                new AttributeDefinition
                {
                    AttributeName = "Status",
                    AttributeType = ScalarAttributeType.S
                }
            },
            GlobalSecondaryIndexUpdates = new List<GlobalSecondaryIndexUpdate>
            {
                new GlobalSecondaryIndexUpdate
                {
                    Create = new CreateGlobalSecondaryIndexAction
                    {
                        IndexName = "GSI_EmailMessage_Status",
                        KeySchema = new List<KeySchemaElement>
                        {
                            new KeySchemaElement("Status", KeyType.HASH)
                        },
                        Projection = new Projection
                        {
                            ProjectionType = ProjectionType.ALL
                        },
                        ProvisionedThroughput = new ProvisionedThroughput
                        {
                            ReadCapacityUnits = 4,
                            WriteCapacityUnits = 1,
                        }
                    }
                }
            }
        };

        var client = DynamoDBManager.DBFactory.GetClient();

        client.UpdateTable(request);

Upvotes: 3

Related Questions