如何通过 AWS SAM 模板创建具有自动扩展吞吐量的 DynamoDB 表

问题描述

我正在尝试创建一个 AWS SAM。我的 Lambda 对 DynamoDB 表执行了一些写操作,并且表配置的吞吐量应该是自动缩放的。我如何在 template.yml 文件中提及?

这是我的表定义

Resources:
  myDB:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: my-awesome-database
      AttributeDeFinitions:
        - AttributeName: e_id
          AttributeType: S
      KeySchema:
        - AttributeName: e_id
          KeyType: HASH
      GlobalSecondaryIndexes:
        - IndexName: my-awesome-database-index
          KeySchema:
            - AttributeName: es
              KeyType: HASH
            - AttributeName: ts
              KeyType: RANGE
          Projection:
            ProjectionType: ALL

解决方法

DynamoDB 的自动缩放不是 DynamoD 的属性B。相反,它是 Application Auto Scaling 的一个属性,您应该使用它的资源来定义表格的缩放比例。

使用固定表定义(您的 DynamoDB 表不正确)自动缩放读取容量的示例如下。要自动扩展写入容量,您必须添加类似的资源。

Resources:
  myDB:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: my-awesome-database
      ProvisionedThroughput:
        ReadCapacityUnits: 2
        WriteCapacityUnits: 2      
      AttributeDefinitions:
        - AttributeName: e_id
          AttributeType: S
        - AttributeName: es
          AttributeType: S          
        - AttributeName: ts
          AttributeType: S          
      KeySchema:
        - AttributeName: e_id
          KeyType: HASH
      GlobalSecondaryIndexes:
        - IndexName: my-awesome-database-index
          KeySchema:
            - AttributeName: es
              KeyType: HASH
            - AttributeName: ts
              KeyType: RANGE
          Projection:
            ProjectionType: ALL
          ProvisionedThroughput:
            ReadCapacityUnits: 2
            WriteCapacityUnits: 2  

  MyScalableTarget:
    Type: AWS::ApplicationAutoScaling::ScalableTarget
    Properties: 
      MaxCapacity: 10
      MinCapacity: 1
      ResourceId: !Sub "table/${myDB}"
      RoleARN: !Sub "arn:aws:iam::${AWS::AccountId}:role/aws-service-role/dynamodb.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_DynamoDBTable"
      ScalableDimension: dynamodb:table:ReadCapacityUnits
      ServiceNamespace: dynamodb

  MyScalableTargetGSI:
    Type: AWS::ApplicationAutoScaling::ScalableTarget
    Properties: 
      MaxCapacity: 10
      MinCapacity: 1
      ResourceId: !Sub "table/${myDB}/index/my-awesome-database-index"
      RoleARN: !Sub "arn:aws:iam::${AWS::AccountId}:role/aws-service-role/dynamodb.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_DynamoDBTable"
      ScalableDimension: dynamodb:index:ReadCapacityUnits
      ServiceNamespace: dynamodb    

  MyTargetTracking:
    Type: AWS::ApplicationAutoScaling::ScalingPolicy
    Properties: 
      PolicyName: my-scaling-policy
      PolicyType: TargetTrackingScaling
      ScalingTargetId: !Ref MyScalableTarget
      TargetTrackingScalingPolicyConfiguration: 
        PredefinedMetricSpecification: 
          PredefinedMetricType: DynamoDBReadCapacityUtilization
        TargetValue: 70

  MyTargetTrackingGSI:
    Type: AWS::ApplicationAutoScaling::ScalingPolicy
    Properties: 
      PolicyName: my-scaling-policy
      PolicyType: TargetTrackingScaling
      ScalingTargetId: !Ref MyScalableTargetGSI
      TargetTrackingScalingPolicyConfiguration: 
        PredefinedMetricSpecification: 
          PredefinedMetricType: DynamoDBReadCapacityUtilization
        TargetValue: 70