如何在AWS SAM模板中创建带自动扩缩容吞吐量的DynamoDB表
DynamoDB 预置吞吐量自动扩缩容的SAM模板配置实现
现有配置的前置缺失项
- 未明确声明DynamoDB表的计费模式为
PROVISIONED,若不指定该参数,模板默认创建为按需计费模式,无法挂载预置吞吐的自动扩缩容能力 - 未为表本身、关联的全局二级索引定义基础预置吞吐值,这是自动扩缩容服务的挂载基础
- 未注册应用自动扩缩容的关联资源,完成DynamoDB表和自动扩缩容服务的权限、资源关联,否则配置不会实际生效
- 原配置存在部署级硬伤:全局二级索引用到的
es、ts字段未在AttributeDefinitions中声明,会直接导致栈创建失败
完整可部署的模板配置
Resources: # DynamoDB表基础定义,补充缺失的必要配置 myDB: Type: AWS::DynamoDB::Table Properties: TableName: my-awesome-database # 明确指定为预置吞吐量计费模式 BillingMode: PROVISIONED # 表级基础预置吞吐值,作为自动扩缩容的初始基准 ProvisionedThroughput: ReadCapacityUnits: 5 WriteCapacityUnits: 5 AttributeDefinitions: - AttributeName: e_id AttributeType: S # 补全全局二级索引依赖的属性定义 - AttributeName: es AttributeType: S - AttributeName: ts AttributeType: N KeySchema: - AttributeName: e_id KeyType: HASH GlobalSecondaryIndexes: - IndexName: my-awesome-database-index KeySchema: - AttributeName: es KeyType: HASH - AttributeName: ts KeyType: RANGE Projection: ProjectionType: ALL # GSI独立预置吞吐值,作为GSI自动扩缩容的初始基准 ProvisionedThroughput: ReadCapacityUnits: 5 WriteCapacityUnits: 5 # 自动扩缩容服务关联的IAM角色,授予最小操作权限 DynamoDBAutoScalingRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: "2012-10-17" Statement: - Effect: Allow Principal: Service: application-autoscaling.amazonaws.com Action: sts:AssumeRole Policies: - PolicyName: DynamoDBAutoScalingAccess PolicyDocument: Version: "2012-10-17" Statement: - Effect: Allow Action: - dynamodb:DescribeTable - dynamodb:UpdateTable - cloudwatch:PutMetricAlarm - cloudwatch:DescribeAlarms - cloudwatch:GetMetricStatistics - cloudwatch:SetAlarmState Resource: "*" # 表级写入容量的自动扩缩容目标注册 TableWriteScalableTarget: Type: AWS::ApplicationAutoScaling::ScalableTarget Properties: RoleArn: !GetAtt DynamoDBAutoScalingRole.Arn ServiceNamespace: dynamodb ResourceId: !Sub "table/${myDB.TableName}" ScalableDimension: dynamodb:table:WriteCapacityUnits # 定义扩缩容的容量上下限 MinCapacity: 5 MaxCapacity: 100 # 表级写入容量的自动扩缩容策略:基于利用率的目标跟踪模式 TableWriteScalingPolicy: Type: AWS::ApplicationAutoScaling::ScalingPolicy Properties: ScalingTargetId: !Ref TableWriteScalableTarget PolicyName: DynamoDBTableWriteAutoScalingPolicy PolicyType: TargetTrackingScaling TargetTrackingScalingPolicyConfiguration: # 触发伸缩的利用率阈值,与控制台默认逻辑一致 TargetValue: 40.0 # 缩容冷却时间,避免频繁波动 ScaleInCooldown: 300 # 扩容冷却时间 ScaleOutCooldown: 60 DisableScaleIn: false # 全局二级索引写入容量的自动扩缩容目标注册 GSIWriteScalableTarget: Type: AWS::ApplicationAutoScaling::ScalableTarget Properties: RoleArn: !GetAtt DynamoDBAutoScalingRole.Arn ServiceNamespace: dynamodb ResourceId: !Sub "table/${myDB.TableName}/index/${myDB.GlobalSecondaryIndexes[0].IndexName}" ScalableDimension: dynamodb:index:WriteCapacityUnits MinCapacity: 5 MaxCapacity: 100 # 全局二级索引写入容量的自动扩缩容策略 GSIWriteScalingPolicy: Type: AWS::ApplicationAutoScaling::ScalingPolicy Properties: ScalingTargetId: !Ref GSIWriteScalableTarget PolicyName: DynamoDBGSIWriteAutoScalingPolicy PolicyType: TargetTrackingScaling TargetTrackingScalingPolicyConfiguration: TargetValue: 40.0 ScaleInCooldown: 300 ScaleOutCooldown: 60 DisableScaleIn: false
关键配置注意事项
- 若需要为读取能力开启自动扩缩容,仅需将对应ScalableTarget的
ScalableDimension替换为dynamodb:table:ReadCapacityUnits、dynamodb:index:ReadCapacityUnits,其余参数逻辑保持不变即可 MaxCapacity参数需根据自身月度账单上限设置,避免突发流量下自动扩缩容到过高容量产生非预期成本- 后续若计划将表切换为按需计费模式,需先删除所有上述自动扩缩容关联资源,否则会因资源模式冲突导致部署失败
内容的提问来源于stack exchange,提问作者Crazy Cat
相关产品推荐
相关产品推荐

