Specify autoscaling of dynamodb with boto3 client

Viewed 1750

We've been using boto3 to create and write to dynamodb tables. It's working well, but we also want to specify autoscaling properties when creating tables since this is now an option . I know this can be done manually, but we want to automate it as we create various tables with boto3 via our python backend. Has anyone had success doing this? If so, is it possible with the boto3 dynamodb client, with a higher-level boto3 client option, or otherwise?

2 Answers

Here is the documentation for autoscaling,

http://boto3.readthedocs.io/en/latest/reference/services/application-autoscaling.html

You need to use the namespace 'dynamodb' to autoscale dynamodb.

You can also do via cli it will also give you an idea on parameters,

aws application-autoscaling register-scalable-target \
    --service-namespace dynamodb \
    --resource-id "table/TestTable" \
    --scalable-dimension "dynamodb:table:WriteCapacityUnits" \
    --min-capacity 5 \
    --max-capacity 10 \
    --role-arn roleARN

Reference:

http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/AutoScaling.CLI.html

Hope it helps.

You need to register_scalable_target like this:

import boto3
auto_scaling_client = boto3.client('application-autoscaling')
# Read capacity
auto_scaling_client.register_scalable_target(ServiceNamespace='dynamodb',
                                             ResourceId='table/foo_table',
                                             ScalableDimension='dynamodb:table:ReadCapacityUnits',
                                             MinCapacity=1,
                                             MaxCapacity=10)
# Write capacity
auto_scaling_client.register_scalable_target(ServiceNamespace='dynamodb',
                                             ResourceId='table/foo_table',
                                             ScalableDimension='dynamodb:table:WriteCapacityUnits',
                                             MinCapacity=1,
                                             MaxCapacity=10)

If you want to adjust the target percentage, you'll have to use put_scaling_policy like this (only the write capacity units shown here because I got lazy):

target_percentage = 70.0
cooldown_in_seconds = 30
auto_scaling_client.put_scaling_policy(ServiceNamespace='dynamodb',
                                       ResourceId='table/foo_table',
                                       PolicyType='TargetTrackingScaling',
                                       PolicyName='ScaleDynamoDBWriteCapacityUtilization',
                                       ScalableDimension='dynamodb:table:WriteCapacityUnits',
                                       TargetTrackingScalingPolicyConfiguration={
                                           'TargetValue': target_percentage,
                                           'PredefinedMetricSpecification': {
                                               'PredefinedMetricType': 'DynamoDBWriteCapacityUtilization'
                                            },
                                           'ScaleOutCooldown': cooldown_in_seconds,
                                           'ScaleInCooldown': cooldown_in_seconds
                                       })
Related