简体   繁体   中英

How Do I autoscale DynamoDB with a Cloud Formation Template?

When creating a DynamoDB CloudFormation template, you need to specify ProvisionedThroughput:

http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-dynamodb-table.html#cfn-dynamodb-table-provisionedthroughput

But I also see this document that says when you create a table via the console, that Auto Scaling is set up by default.

http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.ProvisionedThroughput.html#HowItWorks.ProvisionedThroughput.AutoScaling

My question is: Is there something special I need to do with Cloudformation to get my DynamoDB table to autoscale?

Configuring Auto Scaling DynamoDB is not yet available using CloudFormation.


It is common for new features to AWS services to not be immediately supported by CloudFormation. The delay in when the support is added can vary but often takes many months.


EDIT

Amazon just announced Target Tracking Policies which is how this is accomplished in CloudFormation using a separate resource type of AWS::ApplicationAutoScaling::ScalingPolicy .

You will have to still configure the DynamoDB table with a specific read and write throughput but separately configure the scaling policy for reads and another policy for writes.

Amazon provides the following example in their documentation.

{
  "Resources": {
    "DDBTable": {
      "Type": "AWS::DynamoDB::Table",
      "Properties": {
        "AttributeDefinitions": [
          {
            "AttributeName": "ArtistId",
            "AttributeType": "S"
          },
          {
            "AttributeName": "Concert",
            "AttributeType": "S"
          },
          {
            "AttributeName": "TicketSales",
            "AttributeType": "S"
          }
        ],
        "KeySchema": [
          {
            "AttributeName": "ArtistId",
            "KeyType": "HASH"
          },
          {
            "AttributeName": "Concert",
            "KeyType": "RANGE"
          }
        ],
        "GlobalSecondaryIndexes": [
          {
            "IndexName": "GSI",
            "KeySchema": [
              {
                "AttributeName": "TicketSales",
                "KeyType": "HASH"
              }
            ],
            "Projection": {
              "ProjectionType": "KEYS_ONLY"
            },
            "ProvisionedThroughput": {
              "ReadCapacityUnits": 5,
              "WriteCapacityUnits": 5
            }
          }
        ],
        "ProvisionedThroughput": {
          "ReadCapacityUnits": 5,
          "WriteCapacityUnits": 5
        }
      }
    },
    "WriteCapacityScalableTarget": {
      "Type": "AWS::ApplicationAutoScaling::ScalableTarget",
      "Properties": {
        "MaxCapacity": 15,
        "MinCapacity": 5,
        "ResourceId": { "Fn::Join": [
          "/",
          [
            "table",
            { "Ref": "DDBTable" }
          ]
        ] },
        "RoleARN": {
          "Fn::GetAtt": ["ScalingRole", "Arn"]
        },
        "ScalableDimension": "dynamodb:table:WriteCapacityUnits",
        "ServiceNamespace": "dynamodb"
      }
    },
    "ScalingRole": {
      "Type": "AWS::IAM::Role",
      "Properties": {
        "AssumeRolePolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Principal": {
                "Service": [
                  "application-autoscaling.amazonaws.com"
                ]
              },
              "Action": [
                "sts:AssumeRole"
              ]
            }
          ]
        },
        "Path": "/",
        "Policies": [
          {
            "PolicyName": "root",
            "PolicyDocument": {
              "Version": "2012-10-17",
              "Statement": [
                {
                  "Effect": "Allow",
                  "Action": [
                    "dynamodb:DescribeTable",
                    "dynamodb:UpdateTable",
                    "cloudwatch:PutMetricAlarm",
                    "cloudwatch:DescribeAlarms",
                    "cloudwatch:GetMetricStatistics",
                    "cloudwatch:SetAlarmState",
                    "cloudwatch:DeleteAlarms"
                  ],
                  "Resource": "*"
                }
              ]
            }
          }
        ]
      }
    },
    "WriteScalingPolicy": {
      "Type": "AWS::ApplicationAutoScaling::ScalingPolicy",
      "Properties": {
        "PolicyName": "WriteAutoScalingPolicy",
        "PolicyType": "TargetTrackingScaling",
        "ScalingTargetId": {
          "Ref": "WriteCapacityScalableTarget"
        },
        "TargetTrackingScalingPolicyConfiguration": {
          "TargetValue": 50.0,
          "ScaleInCooldown": 60,
          "ScaleOutCooldown": 60,
          "PredefinedMetricSpecification": {
            "PredefinedMetricType": "DynamoDBWriteCapacityUtilization"
          }
        }
      }
    }
  }
}

The autoscaling can be done using CloudFormation.

Refer this answer .

This appears to work:

"ReadScaling" : {
  "Type" : "AWS::ApplicationAutoScaling::ScalableTarget",
  "Properties" : {
    "MaxCapacity" : "<MAX CAPACITY>,
    "MinCapacity" : "<MIN CAPACITY>,
    "ResourceId" : "table/<TABLE NAME>",
    "RoleARN" : "<IAM ROLE ARN>",
    "ScalableDimension" : "dynamodb:table:ReadCapacityUnits",
    "ServiceNamespace" : "dynamodb"
  }
},

"ReadScalingPolicy" : {
  "Type" : "AWS::ApplicationAutoScaling::ScalingPolicy",
  "Properties" : {
    "PolicyName" : "ReadScalingPolicy",
    "PolicyType" : "TargetTrackingScaling",
    "ResourceId" : "table/<TABLE NAME>",
    "ScalableDimension" : "dynamodb:table:ReadCapacityUnits",
    "ServiceNamespace" : "dynamodb",
    "TargetTrackingScalingPolicyConfiguration" : {
      "PredefinedMetricSpecification": {
        "PredefinedMetricType": "DynamoDBReadCapacityUtilization"
      },
      "ScaleInCooldown" : "60",
      "ScaleOutCooldown" : "60",
      "TargetValue" : "70"
    }
  },
  "DependsOn" : "ReadScaling"
},

See http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/AutoScaling.CLI.html for specifications for the IAM Role

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM