如何使用 Cloud Formation 模板自动扩展 DynamoDB?

Posted

技术标签:

【中文标题】如何使用 Cloud Formation 模板自动扩展 DynamoDB?【英文标题】:How Do I autoscale DynamoDB with a Cloud Formation Template? 【发布时间】:2017-12-13 14:54:48 【问题描述】:

创建 DynamoDB CloudFormation 模板时,需要指定 ProvisionedThroughput:

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

但我也看到这个文档说,当您通过控制台创建表时,默认情况下会设置 Auto Scaling。

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

我的问题是:为了让我的 DynamoDB 表自动缩放,我需要对 Cloudformation 做些什么特别的事情吗?

【问题讨论】:

【参考方案1】:

尚无法使用 CloudFormation 配置 Auto Scaling DynamoDB。


CloudFormation 不立即支持 AWS 服务的新功能是很常见的。添加支持的时间延迟可能会有所不同,但通常需要几个月的时间。


编辑

Amazon 刚刚宣布了 Target Tracking Policies,这就是在 CloudFormation 中使用单独的资源类型 AWS::ApplicationAutoScaling::ScalingPolicy 来实现的。

您仍需要为 DynamoDB 表配置特定的读写吞吐量,但需要单独配置读取扩展策略和写入另一个策略。

亚马逊在其文档中提供了以下示例。


  "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"
          
        
      
    
  

【讨论】:

【参考方案2】:

可以使用 CloudFormation 完成自动缩放。

请参阅此answer。

【讨论】:

【参考方案3】:

这似乎有效:

"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"
,

有关 IAM 角色的规范,请参阅 http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/AutoScaling.CLI.html

【讨论】:

以上是关于如何使用 Cloud Formation 模板自动扩展 DynamoDB?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Elastic Beanstalk Cloud Formation 脚本中强制 ELB 配置

如何使用 Cloud Formation 模板在 S3 存储桶上设置 SSE-S3 或 SSE-KMS 加密?

Cloud Formation 模板将入口规则添加到现有安全组

我能否在 SAM 模板中使用 AWS Cloud Formation 资源语法,反之亦然?

如何从 Cloud Formation 获取 Elastic Container Repository URI?

AWS:Cloud Formation:是不是可以使用多个“DependsOn”?