简体   繁体   English

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

[英]How to create DynamoDB table with autoscaled throughput via AWS SAM template

I am trying to create a AWS SAM.我正在尝试创建一个 AWS SAM。 My Lambda does some write operation on DynamoDB table and the table provisioned throughput should be Autoscaled.我的 Lambda 对 DynamoDB 表进行了一些写入操作,并且表配置的吞吐量应该是自动缩放的。 How can I mention in the template.yml file?如何在template.yml文件中提及?

here is my table definition这是我的表定义

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

Autoscaling of DynamoDB is not a property of DynamoD B. Instead, it is a property of Application Auto Scaling and you should use its resources to define scaling for your table. DynamoDB 的自动缩放不是DynamoD B 的属性。相反,它是Application Auto Scaling的属性,您应该使用它的资源来定义表的缩放。

An example for read-capacity auto-scaling with fixed table definition (your DynamoDB table is incorrect) is below.下面是使用固定表定义(您的 DynamoDB 表不正确)进行读取容量自动扩展的示例。 For auto-scaling write capacity you have to add similar resources.对于自动扩展写入容量,您必须添加类似的资源。

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        

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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