$npx -y skills add giuseppe-trisciuoglio/developer-kit --skill aws-cloudformation-dynamodbProvides AWS CloudFormation patterns for DynamoDB tables, GSIs, LSIs, auto-scaling, and streams. Use when creating DynamoDB tables with CloudFormation, configuring primary keys, local/global secondary indexes, capacity modes (on-demand/provisioned), point-in-time recovery, encryp
| 1 | # AWS CloudFormation DynamoDB Patterns |
| 2 | |
| 3 | Provides production-ready NoSQL database infrastructure patterns using AWS CloudFormation templates with DynamoDB tables, GSIs, LSIs, auto-scaling, encryption, TTL, and streams. |
| 4 | |
| 5 | ## Overview |
| 6 | |
| 7 | Covers DynamoDB tables, primary keys, secondary indexes (GSI/LSI), capacity modes, auto-scaling, encryption, TTL, streams, and best practices for parameters, outputs, and cross-stack references. |
| 8 | |
| 9 | ## When to Use |
| 10 | |
| 11 | Creating DynamoDB tables, configuring keys and indexes, setting capacity modes, implementing auto-scaling, enabling encryption/TTL/streams, and organizing CloudFormation templates. |
| 12 | |
| 13 | ## Instructions |
| 14 | |
| 15 | Follow these steps to create DynamoDB tables with CloudFormation: |
| 16 | |
| 17 | 1. **Define Table Parameters**: Specify table name and billing mode |
| 18 | 2. **Configure Primary Key**: Set partition key and optional sort key |
| 19 | 3. **Add Secondary Indexes**: Create GSIs for alternative access patterns |
| 20 | 4. **Configure Encryption**: Enable encryption using KMS keys |
| 21 | 5. **Set Up TTL**: Define timestamp attribute for automatic deletion |
| 22 | 6. **Enable Streams**: Configure stream for change data capture |
| 23 | 7. **Add Auto Scaling**: Implement Application Auto Scaling for provisioned capacity |
| 24 | 8. **Create Backup**: Enable point-in-time recovery |
| 25 | 9. **Validate Template**: Run `aws cloudformation validate-template` before deployment |
| 26 | 10. **Deploy Stack**: Use `aws cloudformation create-stack` or `update-stack` |
| 27 | 11. **Monitor Events**: Check `aws cloudformation describe-stack-events` for failures or `ROLLBACK` status |
| 28 | 12. **Handle Rollback**: On failure, review events for resource errors, fix the template, and re-deploy |
| 29 | |
| 30 | ## Quick Reference |
| 31 | |
| 32 | | Resource Type | Purpose | |
| 33 | |---------------|---------| |
| 34 | | `AWS::DynamoDB::Table` | Create DynamoDB table | |
| 35 | | `AWS::ApplicationAutoScaling::ScalableTarget` | Auto scaling configuration | |
| 36 | | `AWS::ApplicationAutoScaling::ScalingPolicy` | Scaling policies | |
| 37 | | `AWS::KMS::Key` | KMS key for encryption | |
| 38 | | `AWS::IAM::Role` | IAM roles for auto scaling | |
| 39 | | BillingMode | `PAY_PER_REQUEST` or `PROVISIONED` | |
| 40 | | SSESpecification | Server-side encryption | |
| 41 | |
| 42 | ## Examples |
| 43 | |
| 44 | ### Basic Table with On-Demand Capacity |
| 45 | |
| 46 | ```yaml |
| 47 | DynamoDBTable: |
| 48 | Type: AWS::DynamoDB::Table |
| 49 | Properties: |
| 50 | TableName: !Sub "${AWS::StackName}-table" |
| 51 | BillingMode: PAY_PER_REQUEST |
| 52 | AttributeDefinitions: |
| 53 | - AttributeName: pk |
| 54 | AttributeType: S |
| 55 | KeySchema: |
| 56 | - AttributeName: pk |
| 57 | KeyType: HASH |
| 58 | ``` |
| 59 | |
| 60 | ### Table with Global Secondary Index |
| 61 | |
| 62 | ```yaml |
| 63 | DynamoDBTable: |
| 64 | Type: AWS::DynamoDB::Table |
| 65 | Properties: |
| 66 | TableName: !Sub "${AWS::StackName}-table" |
| 67 | BillingMode: PAY_PER_REQUEST |
| 68 | AttributeDefinitions: |
| 69 | - AttributeName: pk |
| 70 | AttributeType: S |
| 71 | - AttributeName: gsi-pk |
| 72 | AttributeType: S |
| 73 | KeySchema: |
| 74 | - AttributeName: pk |
| 75 | KeyType: HASH |
| 76 | GlobalSecondaryIndexes: |
| 77 | - IndexName: gsi-index |
| 78 | KeySchema: |
| 79 | - AttributeName: gsi-pk |
| 80 | KeyType: HASH |
| 81 | Projection: |
| 82 | ProjectionType: ALL |
| 83 | ``` |
| 84 | |
| 85 | ### Table with TTL |
| 86 | |
| 87 | ```yaml |
| 88 | SessionTable: |
| 89 | Type: AWS::DynamoDB::Table |
| 90 | Properties: |
| 91 | TableName: !Sub "${AWS::StackName}-sessions" |
| 92 | BillingMode: PAY_PER_REQUEST |
| 93 | AttributeDefinitions: |
| 94 | - AttributeName: sessionId |
| 95 | AttributeType: S |
| 96 | KeySchema: |
| 97 | - AttributeName: sessionId |
| 98 | KeyType: HASH |
| 99 | TimeToLiveSpecification: |
| 100 | AttributeName: expiresAt |
| 101 | Enabled: true |
| 102 | ``` |
| 103 | |
| 104 | ### Table with Auto Scaling |
| 105 | |
| 106 | ```yaml |
| 107 | ScalableTarget: |
| 108 | Type: AWS::ApplicationAutoScaling::ScalableTarget |
| 109 | Properties: |
| 110 | MaxCapacity: 100 |
| 111 | MinCapacity: 5 |
| 112 | ResourceId: !Sub "table/${DynamoDBTable}" |
| 113 | RoleARN: !GetAtt AutoScalingRole.Arn |
| 114 | ScalableDimension: dynamodb:table:ReadCapacityUnits |
| 115 | ServiceNamespace: dynamodb |
| 116 | ``` |
| 117 | |
| 118 | See [references/complete-examples.md](references/complete-examples.md) for more complete examples including encryption, streams, auto scaling, and production tables. |
| 119 | |
| 120 | ## Template Structure |
| 121 | |
| 122 | ### Base Template |
| 123 | |
| 124 | ```yaml |
| 125 | AWSTemplateFormatVersion: 2010-09-09 |
| 126 | Description: DynamoDB table with GSI and auto-scaling |
| 127 | |
| 128 | Parameters: |
| 129 | TableName: |
| 130 | Type: String |
| 131 | Default: my-table |
| 132 | BillingMode: |
| 133 | Type: String |
| 134 | Default: PAY_PER_REQUEST |
| 135 | |
| 136 | Resources: |
| 137 | DynamoDBTable: |
| 138 | Type: AWS::DynamoDB::Table |
| 139 | Properties: |
| 140 | TableName: !Ref TableName |
| 141 | BillingMode: !Ref BillingMode |
| 142 | |
| 143 | Outputs: |
| 144 | TableName: |
| 145 | Value: !Ref DynamoDBTable |
| 146 | TableArn: |
| 147 | Value: !GetAtt DynamoDBTable.Arn |
| 148 | ``` |
| 149 | |
| 150 | See [references/advanced-configuration.md](references/advanced-config |