How can dynamodb secondary indexes be autoscaled with Cloudformation templates?

Viewed 3735

I've seen the examples for adding autoscaling to tables with cloudformation, but other than the "Apply same settings to global secondary indexes" checkbox in the dynamodb console, I don't see a way to define this with cloudformation templates.

2 Answers

It is possible to define the secondary index as an autoscaling target.

You need just to specify the index as the resourceId using /index/**YOUR_INDEX_NAME** as a suffix and using dynamodb:index:WriteCapacityUnits and dynamodb:index:ReadCapacityUnits as ScalableDimension.

E.g.

  SecIndexWriteCapacity:
    Type: AWS::ApplicationAutoScaling::ScalableTarget
    Properties:
      MaxCapacity: 1000
      MinCapacity: 15
      ResourceId: !Sub "table/${MY_MAIN_TABLE}/index/${MY_SECONDARY_INDEX_NAME}"
      RoleARN: !GetAtt ScalingRole.Arn
      ScalableDimension: dynamodb:index:WriteCapacityUnits
      ServiceNamespace: dynamodb
  SecIndexReadCapacity:
    Type: AWS::ApplicationAutoScaling::ScalableTarget
    Properties:
      MaxCapacity: 1000
      MinCapacity: 15
      ResourceId: !Sub "table/${MY_MAIN_TABLE}/index/MY_SECONDARY_INDEX_NAME"
      RoleARN: !GetAtt ScalingRole.Arn
      ScalableDimension: dynamodb:index:ReadCapacityUnits
      ServiceNamespace: dynamodb
  SecIndexWriteScalingPolicy:
    Type: AWS::ApplicationAutoScaling::ScalingPolicy
    Properties:
      PolicyName: SecIndexWriteScalingPolicy
      PolicyType: TargetTrackingScaling
      ScalingTargetId: !Ref SecIndexWriteCapacity
      TargetTrackingScalingPolicyConfiguration:
        TargetValue: 50.0
        ScaleInCooldown: 30
        ScaleOutCooldown: 1
        PredefinedMetricSpecification:
          PredefinedMetricType: DynamoDBWriteCapacityUtilization
  SecIndexReadScalingPolicy:
    Type: AWS::ApplicationAutoScaling::ScalingPolicy
    Properties:
      PolicyName: SecIndexReadScalingPolicy
      PolicyType: TargetTrackingScaling
      ScalingTargetId: !Ref SecIndexReadCapacity
      TargetTrackingScalingPolicyConfiguration:
        TargetValue: 50.0
        ScaleInCooldown: 30
        ScaleOutCooldown: 0
        PredefinedMetricSpecification:
          PredefinedMetricType: DynamoDBReadCapacityUtilization

See also https://aws.amazon.com/blogs/database/how-to-use-aws-cloudformation-to-configure-auto-scaling-for-amazon-dynamodb-tables-and-indexes/

Actually it is possible.

Follow the official AWS example for the DynamoDB/CloudFormation, but for each GSI you would need to create a separate ScalableTarget and a Scalable Policy. In the ScalableTarget properties use table/my-table/index/my-table-index notation for the ResourceId.

Related