关于 aws-cloudformation-dynamodb
面向使用 AWS CloudFormation 的开发者,提供创建和配置 DynamoDB 表的基础设施模式,涵盖索引、容量、安全与生命周期设置,并附有部署和验证指引。
主要功能
- 01配置表键与 GSI、LSI
- 02设置容量模式与自动扩缩
- 03启用加密、TTL 与 Streams
- 04配置时间点恢复和跨堆栈引用
适用场景
- 01为应用创建 DynamoDB 表
- 02按访问模式设计二级索引
- 03为会话数据配置 TTL
- 04为预置容量表设置自动扩缩
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ giuseppe-trisciuoglio/ developer-kit --skill aws-cloudformation-dynamodb安装后在 Claude Code 中输入/aws-cloudformation-dynamodb确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
references/advanced-configuration.mdcomplete-examples.mdexamples.mdreference.md详细介绍
AWS CloudFormation DynamoDB 模式
使用 AWS CloudFormation 模板提供可用于生产环境的 NoSQL 数据库基础设施模式,涵盖 DynamoDB 表、GSI、LSI、自动扩缩、加密、TTL 和流。
概述
涵盖 DynamoDB 表、主键、二级索引(GSI/LSI)、容量模式、自动扩缩、加密、TTL、流,以及参数、输出和跨堆栈引用的最佳实践。
适用场景
创建 DynamoDB 表、配置键和索引、设置容量模式、实现自动扩缩、启用加密/TTL/流,以及组织 CloudFormation 模板。
操作说明
按照以下步骤使用 CloudFormation 创建 DynamoDB 表:
- 定义表参数:指定表名和计费模式
- 配置主键:设置分区键和可选的排序键
- 添加二级索引:为其他访问模式创建 GSI
- 配置加密:使用 KMS 密钥启用加密
- 设置 TTL:定义用于自动删除的时间戳属性
- 启用流:配置用于捕获数据变更的流
- 添加自动扩缩:为预置容量实现 Application Auto Scaling
- 创建备份:启用时间点恢复
- 验证模板:在部署前运行
aws cloudformation validate-template - 部署堆栈:使用
aws cloudformation create-stack或update-stack - 监控事件:检查
aws cloudformation describe-stack-events,查看是否存在失败或ROLLBACK状态 - 处理回滚:失败时,检查事件以查找资源错误,修复模板并重新部署
快速参考
| 资源类型 | 用途 |
|---|---|
AWS::DynamoDB::Table | 创建 DynamoDB 表 |
AWS::ApplicationAutoScaling::ScalableTarget | 自动扩缩配置 |
AWS::ApplicationAutoScaling::ScalingPolicy | 扩缩策略 |
AWS::KMS::Key | 用于加密的 KMS 密钥 |
AWS::IAM::Role | 用于自动扩缩的 IAM 角色 |
| BillingMode | PAY_PER_REQUEST 或 PROVISIONED |
| SSESpecification | 服务端加密 |
示例
按需容量的基本表
DynamoDBTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Sub "${AWS::StackName}-table"
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: pk
AttributeType: S
KeySchema:
- AttributeName: pk
KeyType: HASH
带全局二级索引的表
DynamoDBTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Sub "${AWS::StackName}-table"
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: pk
AttributeType: S
- AttributeName: gsi-pk
AttributeType: S
KeySchema:
- AttributeName: pk
KeyType: HASH
GlobalSecondaryIndexes:
- IndexName: gsi-index
KeySchema:
- AttributeName: gsi-pk
KeyType: HASH
Projection:
ProjectionType: ALL
带 TTL 的表
SessionTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Sub "${AWS::StackName}-sessions"
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: sessionId
AttributeType: S
KeySchema:
- AttributeName: sessionId
KeyType: HASH
TimeToLiveSpecification:
AttributeName: expiresAt
Enabled: true
带自动扩缩的表
ScalableTarget:
Type: AWS::ApplicationAutoScaling::ScalableTarget
Properties:
MaxCapacity: 100
MinCapacity: 5
ResourceId: !Sub "table/${DynamoDBTable}"
RoleARN: !GetAtt AutoScalingRole.Arn
ScalableDimension: dynamodb:table:ReadCapacityUnits
ServiceNamespace: dynamodb
请参阅 references/complete-examples.md,了解更多完整示例,包括加密、流、自动扩缩和生产环境表。
模板结构
基础模板
AWSTemplateFormatVersion: 2010-09-09
Description: DynamoDB table with GSI and auto-scaling
Parameters:
TableName:
Type: String
Default: my-table
BillingMode:
Type: String
Default: PAY_PER_REQUEST
Resources:
DynamoDBTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Ref TableName
BillingMode: !Ref BillingMode
Outputs:
TableName:
Value: !Ref DynamoDBTable
TableArn:
Value: !GetAtt DynamoDBTable.Arn
请参阅 references/advanced-configuration.md,了解参数、映射、条件、输出、IAM 角色和跨堆栈引用的详细信息。
最佳实践
- 开发/测试和工作负载不可预测时使用 PAY_PER_REQUEST
- 为生产环境的表启用时间点恢复
- 对敏感数据使用 KMS 加密(SSE-KMS)
- 为预置容量表配置自动扩缩
- 谨慎设计 GSI——每个 GSI 都会消耗容量
- 使用 TTL 自动使数据过期(会话、缓存)
- 启用流以捕获数据变更并进行分析
- 为资源添加标签,以便分配成本和进行组织管理
- 导出输出,以供跨堆栈引用
- 使用条件进行特定于环境的配置
常见故障排查
表已存在:使用唯一的表名或堆栈删除策略
GSI 创建失败:确认属性定义中包含 GSI 属性
自动扩缩不起作用:检查 IAM 角色权限和服务关联角色
TTL 未使数据过期:确保 TTL 属性的类型为 Number,而不是 String
未启用流:只能在创建表时启用流
加密错误:确认 KMS 密钥与表位于同一区域
相关技能
- aws-cloudformation-security - DynamoDB 安全最佳实践
- aws-cloudformation-lambda - DynamoDB Streams 的 Lambda 触发器
- aws-cloudformation-iam - 用于访问 DynamoDB 的 IAM 角色
参考资料
完整示例
- references/complete-examples.md - 基础表、GSI、LSI、TTL、加密、PITR、自动扩缩、生产环境表、全局表、流
高级配置
- references/advanced-configuration.md - 参数、映射、条件、自动扩缩策略、流、TTL、全局表、输出、IAM 角色、删除策略
限制和警告
- 表名:每个区域内必须唯一(全局表必须全局唯一)
- GSI 限制:每张表最多 5 个 GSI(适用于单区域表)
- LSI 限制:每张表最多 5 个 LSI,且必须与表使用相同的分区键
- 容量限制:按需模式有默认账户限额(40,000 个 RCU、40,000 个 WCU)
- 自动扩缩:需要 PROVISIONED 计费模式(不能使用 PAY_PER_REQUEST)
- 时间点恢复:只能在创建表时启用
- 流:只能在创建表时启用
- 加密:KMS 密钥必须与表位于同一区域
- TTL:属性必须为 Number 类型,单位为秒
- 吞吐量:每个 GSI 与表共用预置吞吐量
- 项目大小限制:每个项目最多 400 KB
- 热点分区:设计键时应避免热点分区问题
评论
支持 Markdown