title: Adding autoscaling to AWS DynamoDB via CloudFormation for already existing table
body: <p>Our team was quite excited to see autoscaling feature announced for AWS DynamoDB, and when trying it out by adding configuration in web interface we've discovered that's a good match four our applications' needs.</p>
  
  <p>However, attempts to add the same configuration via CF have proved to be a bit more complicated. Following example as provided in this article - <a href="http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-dynamodb-table.html#cfn-dynamodb-table-examples-application-autoscaling" rel="nofollow noreferrer">http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-dynamodb-table.html#cfn-dynamodb-table-examples-application-autoscaling</a> - below is the result, which does not work. Stack detail:</p>
  
  <p><code>
  17:02:25 UTC+0300   UPDATE_ROLLBACK_IN_PROGRESS     AWS::CloudFormation::Stack  my-stack    The following resource(s) failed to create: [WriteCapacityScalableTarget].
      17:02:24 UTC+0300   CREATE_FAILED   AWS::ApplicationAutoScaling::ScalableTarget     WriteCapacityScalableTarget     table/TableName|dynamodb:table:WriteCapacityUnits|dynamodb already exists
      17:02:18 UTC+0300   CREATE_IN_PROGRESS  AWS::ApplicationAutoScaling::ScalableTarget     WriteCapacityScalableTarget<br>
      17:02:15 UTC+0300   CREATE_COMPLETE     AWS::IAM::Role  ScalingRole<br>
      17:01:42 UTC+0300   CREATE_IN_PROGRESS  AWS::IAM::Role  ScalingRole     Resource creation Initiated
      17:01:42 UTC+0300   CREATE_IN_PROGRESS  AWS::IAM::Role  ScalingRole<br>
      17:01:37 UTC+0300   UPDATE_IN_PROGRESS  AWS::CloudFormation::Stack  my-stack    User Initiated
  </code></p>
  
  <p>My CF script is as follows:</p>
  
  <pre><code>{
    "Resources": {
      "MyCustomTableName": {
        "Type": "AWS::DynamoDB::Table",
        "Properties": {
          "TableName": "TableName",
          "AttributeDefinitions": [
            {
              "AttributeName": "someAttribute1:someAttribute2",
              "AttributeType": "S"
            }
          ],
          "KeySchema": [
            {
              "AttributeName": "someAttribute1:someAttribute2",
              "KeyType": "HASH"
            }
          ],
          "ProvisionedThroughput": {
            "ReadCapacityUnits": 1,
            "WriteCapacityUnits": 1
          },
          "StreamSpecification": {
            "StreamViewType": "NEW_AND_OLD_IMAGES"
          }
        }
      },
      "WriteCapacityScalableTarget": {
        "Type": "AWS::ApplicationAutoScaling::ScalableTarget",
        "Properties": {
          "MaxCapacity": 30,
          "MinCapacity": 1,
          "ResourceId": {
            "Fn::Join": [
              "/",
              [
                "table",
                {
                  "Ref": "MyCustomTableName"
                }
              ]
            ]
          },
          "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": 70.0,
            "ScaleInCooldown": 60,
            "ScaleOutCooldown": 60,
            "PredefinedMetricSpecification": {
              "PredefinedMetricType": "DynamoDBWriteCapacityUtilization"
            }
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>If anyone can shed some light as to why this is happening, I'd be much obliged :)</p>