如何切换"将相同的设置应用于全局二级索引"使用代码?

时间:2018-04-24 16:10:35

标签: amazon-web-services amazon-dynamodb amazon-cloudformation boto3 autoscaling

我正在使用Boto3在DynamoDB中创建表格。我已使用以下代码启用了自动缩放:

def conf_auto_scaling(self, table_name):
    print("INFO :: Creating auto scaling for Table [%s]" % table_name)

    # Read capacity
    self.as_c.register_scalable_target(ServiceNamespace='dynamodb',
                                       ResourceId='table/%s' % table_name,
                                       ScalableDimension='dynamodb:table:ReadCapacityUnits',
                                       MinCapacity=5,
                                       MaxCapacity=4000)
    # Write capacity
    self.as_c.register_scalable_target(ServiceNamespace='dynamodb',
                                       ResourceId='table/%s' % table_name,
                                       ScalableDimension='dynamodb:table:WriteCapacityUnits',
                                       MinCapacity=5,
                                       MaxCapacity=1000)

    percent_of_use_to_aim_for = 70.0
    scale_out_cooldown_in_seconds = 60
    scale_in_cooldown_in_seconds = 60
    self.as_c.put_scaling_policy(ServiceNamespace='dynamodb',
                                 ResourceId='table/%s' % table_name,
                                 PolicyType='TargetTrackingScaling',
                                 PolicyName='ScaleDynamoDBReadCapacityUtilization',
                                 ScalableDimension='dynamodb:table:ReadCapacityUnits',
                                 TargetTrackingScalingPolicyConfiguration={
                                     'TargetValue': percent_of_use_to_aim_for,
                                     'PredefinedMetricSpecification': {
                                         'PredefinedMetricType': 'DynamoDBReadCapacityUtilization'
                                     },
                                     'ScaleOutCooldown': scale_out_cooldown_in_seconds,
                                     'ScaleInCooldown': scale_in_cooldown_in_seconds
                                 })
    self.as_c.put_scaling_policy(ServiceNamespace='dynamodb',
                                 ResourceId='table/%s' % table_name,
                                 PolicyType='TargetTrackingScaling',
                                 PolicyName='ScaleDynamoDBWriteCapacityUtilization',
                                 ScalableDimension='dynamodb:table:WriteCapacityUnits',
                                 TargetTrackingScalingPolicyConfiguration={
                                     'TargetValue': percent_of_use_to_aim_for,
                                     'PredefinedMetricSpecification': {
                                         'PredefinedMetricType': 'DynamoDBWriteCapacityUtilization'
                                     },
                                     'ScaleOutCooldown': scale_out_cooldown_in_seconds,
                                     'ScaleInCooldown': scale_in_cooldown_in_seconds
                                 })

如何启用选项"将相同设置应用于全局二级索引"?

Apply same settings to global secondary indexes

1 个答案:

答案 0 :(得分:1)

除了在“表”级别设置扩展策略所需的代码之外,还有“索引”级别的代码。

因此,要将扩展策略设置为“索引”级别,您应该调用 put_scaling_policy ,但是 ResourceId ScalableDimension :

例如:

auto_scaling_client.put_scaling_policy(
            ServiceNamespace='dynamodb',
            ResourceId=f'table/{PUT-HERE-YOUR-TABLE NAME}/index/{YOUR-INDEX-NAME}',
            PolicyType='TargetTrackingScaling',
            PolicyName='ScaleDynamoDBWriteCapacityUtilization',
            ScalableDimension='dynamodb:index:WriteCapacityUnits',
            TargetTrackingScalingPolicyConfiguration={
                'TargetValue': percent_of_use_to_aim_for,
                'PredefinedMetricSpecification': {
                    'PredefinedMetricType': 'DynamoDBWriteCapacityUtilization'
                },
                'ScaleOutCooldown': scale_out_cooldown_in_seconds,
                'ScaleInCooldown': scale_in_cooldown_in_seconds
            }

请注意:

  • ResourceId 会收到完整索引名称
  • ScalableDimensions 包含 index 而不是 table ,即: dynamodb:索引:WriteCapacityUnits而不是dynamodb::WriteCapacityUnits