通过 CloudFormation 为现有表添加自动扩展到 AWS DynamoDB

Ale*_*lov 2 amazon-web-services aws-cloudformation amazon-dynamodb autoscaling

我们的团队很高兴看到为 AWS DynamoDB 宣布了自动扩展功能,并且在通过在 Web 界面中添加配置进行尝试时,我们发现这非常符合我们应用程序的四个需求。

然而,事实证明,通过 CF 添加相同配置的尝试要复杂一些。本文中提供的以下示例 - http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-dynamodb-table.html#cfn-dynamodb-table-examples-application-autoscaling - 下面是结果,这是行不通的。堆栈详细信息:

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
17:02:15 UTC+0300 CREATE_COMPLETE AWS::IAM::Role ScalingRole
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
17:01:37 UTC+0300 UPDATE_IN_PROGRESS AWS::CloudFormation::Stack my-stack User Initiated

我的CF脚本如下:

{
  "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"
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

如果有人能解释为什么会发生这种情况,我将非常感激:)

kri*_*nik 5

按照错误消息进行

CREATE_FAILED AWS::ApplicationAutoScaling::ScalableTarget WriteCapacityScalableTarget table/TableName|dynamodb:table:WriteCapacityUnits|dynamodb already exists

您正在创建一个名为 的 DynamoDB 资源"TableName": "TableName"。一个区域内不能有两个同名的 dynamoDB 表。

转到 DynamoDB 控制台并检查您是否有任何此类表并将其删除。发布模板应该可以正常工作。

选项 2:如果您想继续使用现有表,则可以从 CF 模板中删除 AWS::DynamoDB::Table 资源。