Recently, I wanted to bake my DynamoDB table setup into a CloudFormation template. The table contained a global secondary index and is an autoscaling target for read and write capacity scaling. Registering autoscaling with a DynamoDB table in Cloudformation isn’t hard at all. Yet it is not immediately obvious how to register the index as scalable target. The documentation did not go into detail about index autoscaling, so how do we do it?
First, we will register the table itself as scalable target for write capacity and then attach a policy to it. This isn’t strictly necessary to enable scaling for the index, but why would you want to autoscale the index and not the table itself? The following CloudFormation snipped will do the trick:
That will scale the write capacity between two to ten units targeting a value of 50% consumption. But what about the global secondary index? The given template snipped won’t register it as scalable target. Two questions arise: what is the scalable dimension of a global secondary index, and what is its resource identifier? The answers are in the following snippet:
The resource identifier has the format table/${Table}/index/MyIndex, the scalable dimension is dynamodb:index:WriteCapacityUnits for write capacity scaling and dynamodb:index:ReadCapacityUnits for read capacity scaling. You can find the full template here, where the ScalingRole needed for the scaling to work is defined as well.