如何通过 java 脚本为 DynamoDB 中的预配置读取容量启用自动缩放

How to enable Auto Scaling for Provisioned read capacity in DynamoDB from java script

我正在使用适用于 nodejs 的 AWS SDK 并从代码创建一个 dynamodb table。一切正常,但我需要为配置的读写容量启用自动缩放。这是我正在尝试的代码

var params = {
    TableName : "MyTable",
    KeySchema: [       
        { AttributeName: "Name", KeyType: "HASH"},  //Partition key
        { AttributeName: "time", KeyType: "RANGE" }  //Sort key
    ],
    AttributeDefinitions: [     

        { AttributeName: "Name", AttributeType: "S" },
        { AttributeName: "time", AttributeType: "N" }
    ],
    ProvisionedThroughput: {       
        ReadCapacityUnits: 5, 
        WriteCapacityUnits: 5
    }        
    ]
};

dynamodb.createTable(params, function(err, data) {
    if (err) {
        console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2));
    } else {
        console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2));
    }
});

这将创建一个 table,读写容量为 5,但禁用自动缩放。我看到了一个 java 示例,其中自动缩放是通过代码处理的,但没有用于 java 脚本。关于如何从 NodeJS 启用自动缩放的任何建议都将非常有帮助。 谢谢

您可以通过单独的 ApplicationAutoScaling 调用启用自动缩放。

这里是 Lambda 代码示例,说明如何为写入单元启用自动缩放:

const AWS = require("aws-sdk");
var applicationautoscaling = new AWS.ApplicationAutoScaling({
    apiVersion: '2016-02-06'
});

exports.handler = (event, context, callback) => {
    var params = {
        MaxCapacity: 10,
        MinCapacity: 2,
        ResourceId: "table/MyTable",
        RoleARN: "arn:aws:iam::111111111:role/lambda_s3_exec_role",
        ScalableDimension: "dynamodb:table:WriteCapacityUnits",
        ServiceNamespace: "dynamodb"
    };
    applicationautoscaling.registerScalableTarget(params, function(err, data) {
        if (err) console.log(err, err.stack); // an error occurred
        else console.log(data); // successful response
        callback(null, 'write capacity adjusted');
    });
};

我遵循了 jens walters 的解决方案,dynamodb 的 UI 看起来坏了——从概览上看它说缩放已启用,但当我查看容量选项卡时它没有显示它是。这是因为该解决方案缺少后半部分——扩展策略。

解决方案分为两步:

  1. 注册可扩展目标
  2. 在目标上实施扩展策略

代码如下:

var tableName = "your-table-name"

var autoscalingParams = {
   MaxCapacity: 15,
   MinCapacity: 1,
   ResourceId: "table/" + tableName,
   RoleARN: "arn:aws:iam::" + accountId + ":role/current-lambdaRole",
   ScalableDimension: "dynamodb:table:WriteCapacityUnits",
   ServiceNamespace: "dynamodb"
};

autoscaling.registerScalableTarget(autoscalingParams, function(err, data) {
   if(err) {
       console.log('error');
       console.log(JSON.stringify(err));
   } else {
       console.log(data);
   }

   var scalingPolicy = {
       ServiceNamespace: "dynamodb",
       ResourceId: "table/" + tableName,
       ScalableDimension: "dynamodb:table:WriteCapacityUnits",
       PolicyName: tableName + "-scaling-policy",
       PolicyType: "TargetTrackingScaling",
       TargetTrackingScalingPolicyConfiguration: {
           PredefinedMetricSpecification: {
               PredefinedMetricType: "DynamoDBWriteCapacityUtilization"
           },
           ScaleOutCooldown: 60,
           ScaleInCooldown: 60,
           TargetValue: 70.0
       }
   };

   autoscaling.putScalingPolicy(scalingPolicy, function(err, data) {
       if(err) {
           console.log('error');
           console.log(JSON.stringify(err));
       } else {
           console.log('success!');
           console.log(data);
       }
       cb(err);
   });
});

那么您的角色将需要以下权限:

{
  "IamPolicyLambdaManageScalingDynamoDbTables": {
    "Type": "AWS::IAM::Policy",
    "Properties": {
      "PolicyName": "someName",
      "PolicyDocument": {
        "Version": "2012-10-17",
        "Statement": [
          {
            "Effect": "Allow",
            "Action": [
              "application-autoscaling:RegisterScalableTarget",
              "application-autoscaling:PutScalingPolicy",
              "iam:CreateServiceLinkedRole",
              "iam:PassRole"
            ],
            "Resource": "*"
          }
        ]
      },
      "Roles": [
        {
          "Ref": "IamRoleLambdaExecution"
        }
      ]
    }
  }
}

注意:您可能不应该对角色资源使用通配符,但我还没有确定那些应该是什么