admin管理员组

文章数量:1318183

I am using AWS SDK for nodejs and creating a dynamodb table from the code. It all works fine but i need auto scaling to be enabled for provisioned read and write capacity. This is the code i am trying

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));
    }
});

This creates a table with read and write capacity as 5 but with auto scaling disabled. I have seen a java sample where auto scaling is being handled from code but nothing for java script. Any suggestions on how to enable auto scaling from NodeJS will be very helpful . Thanks

I am using AWS SDK for nodejs and creating a dynamodb table from the code. It all works fine but i need auto scaling to be enabled for provisioned read and write capacity. This is the code i am trying

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));
    }
});

This creates a table with read and write capacity as 5 but with auto scaling disabled. I have seen a java sample where auto scaling is being handled from code but nothing for java script. Any suggestions on how to enable auto scaling from NodeJS will be very helpful . Thanks

Share Improve this question edited Aug 23, 2017 at 10:24 Avijeet asked Aug 21, 2017 at 12:36 AvijeetAvijeet 3654 silver badges14 bronze badges 2
  • You can configure the auto scaling to be handled at database table side itself from AWS management console. – notionquest Commented Aug 21, 2017 at 12:42
  • Yeah i realize that, but thats not what i am looking for. It has to be done from code level as we are exposing some api's to clients and they will not have console access. – Avijeet Commented Aug 21, 2017 at 13:14
Add a ment  | 

2 Answers 2

Reset to default 6

You can enable the auto-scaling through a separate ApplicationAutoScaling call.

Here a Lambda code sample of how to enable auto-scaling for write units:

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');
    });
};

I followed jens walters solution and the UI for dynamodb looked broken--from overview it said scaling was enabled but when I viewed the capacity tab it didn't show that it was. This is because that solution is missing the second half--scaling policy.

The solution is a 2 step process:

  1. Register the scalable target
  2. Put the scaling policy in place on the target

Here's the code:

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);
   });
});

Then your role will need permissions something along the lines of:

{
  "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"
        }
      ]
    }
  }
}

Note: You should probably not use the wildcard for the role resource, but I haven't nailed down what those should be exactly

本文标签: