Scale DynamoDB Capacity using CloudWatch Metrics on Java

Thu, Jul 19, 2018 One-minute read

In this post i’ll show how to autoscale dynamodb using aws java sdk.

First of all we need to open our client and sessions and select our dynamodb table using aws java sdk.

AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().withRegion(Regions.US_WEST_2).build();
DynamoDB dynamoDB = new DynamoDB(client);
String tableName = "webischia-table";
Table table = dynamoDB.getTable(tableName);

Now we should get cloudwatch metrics. So that we have to create our request using last 10 minutes metrics and getting cloudwatch json response.

final AmazonCloudWatch cloudWatch =
Dimension instanceDimension = new Dimension();
        GetMetricStatisticsRequest request = new GetMetricStatisticsRequest()
                .withStartTime(new Date(new Date().getTime() - offsetInMilliseconds))
                .withEndTime(new Date());

        GetMetricStatisticsResult getMetricStatisticsResult = cloudWatch.getMetricStatistics(request);
        Double writeCapUnit = table.describe().getProvisionedThroughput().getWriteCapacityUnits().doubleValue();

After the request done and getting the response we taking last cumulative sum for write throttle

Double currentSum = getMetricStatisticsResult.getDatapoints().get(0).getSum();

After that its just our logic. So i set this to if cumulative sum is bigger than 100 i increase by %20.

if(currentSum >=100){
                System.out.println("Capacity increased");
                try {
                    table.updateTable(new ProvisionedThroughput((long) 1, (long)(writeCapUnit.longValue() *1.2 )));
                catch (Exception e) {

Thats it if you want to check this every 10minutes you should add your loop or use your thread.