将自动缩放策略应用于 DynamoDB 时出现 ObjectNotFoundException table

ObjectNotFoundException when applying autoscaling policy to DynamoDB table

我是 运行 使用 boto3 SDK 的 lambda 函数,以便将自动缩放策略添加到许多 dynamoDB 表和索引中,但是它一直抛出此错误:

An error occurred (ObjectNotFoundException) when calling the PutScalingPolicy operation: No scalable target registered for service namespace: dynamodb, resource ID: table/tableName, scalable dimension: dynamodb:table:ReadCapacityUnits: ObjectNotFoundException

相关代码在这里:

def set_scaling_policy(resource_type, capacity_type, resource_id):
  dbClient = boto3.client('application-autoscaling')
  response = dbClient.put_scaling_policy(
    PolicyName= 'dynamoDBScaling',
    ServiceNamespace= 'dynamodb',
    ResourceId= resource_id,
    ScalableDimension= 'dynamodb:{0}:{1}CapacityUnits'.format(resource_type,capacity_type),
    PolicyType='TargetTrackingScaling',
    TargetTrackingScalingPolicyConfiguration={
        'TargetValue': 50.0,
        'PredefinedMetricSpecification': {
            'PredefinedMetricType': 'DynamoDB{0}CapacityUtilization'.format(capacity_type)
        }
    }
  )

(resource_type 是 'table' 或 'index';capacity_type 是 'Read' 或 'Write')

我考虑过的一些解决方案:

put_scaling_policy

http://boto3.readthedocs.io/en/latest/reference/services/application-autoscaling.html#ApplicationAutoScaling.Client.put_scaling_policy

You cannot create a scaling policy until you register the scalable target using RegisterScalableTarget

register_scalable_target

http://boto3.readthedocs.io/en/latest/reference/services/application-autoscaling.html#ApplicationAutoScaling.Client.register_scalable_target

Registers or updates a scalable target. A scalable target is a resource that Application Auto Scaling can scale out or scale in. After you have registered a scalable target, you can use this operation to update the minimum and maximum values for its scalable dimension.