能否通过AWS CloudFormation模板一次性创建多个Amazon DynamoDB表?若可行,应采用多Properties还是多Resources方式?
Creating Multiple DynamoDB Tables in AWS CloudFormation
Absolutely, you can create multiple Amazon DynamoDB tables in a single CloudFormation template—this is a common use case and fully supported by AWS.
The Correct Approach: Multiple Resources Entries
To pull this off, you'll need to define separate Resources entries for each table, not multiple Properties within a single resource. Here's why:
- In CloudFormation, each
Resourceblock represents a distinct AWS resource. A DynamoDB table is its own standalone resource, so each table needs its own dedicatedAWS::DynamoDB::Tableresource definition. - The
Propertiessection is specific to one resource—it defines configuration details (like schema, throughput, or encryption) for that individual resource. You can't pack multiple table configurations into a singlePropertiesblock; CloudFormation won't interpret that correctly and will throw validation errors.
Example Template with Two DynamoDB Tables
Here's a concrete, working example to illustrate how this structure looks:
AWSTemplateFormatVersion: '2010-09-09' Resources: UsersTable: Type: AWS::DynamoDB::Table Properties: TableName: Users AttributeDefinitions: - AttributeName: UserID AttributeType: S KeySchema: - AttributeName: UserID KeyType: HASH ProvisionedThroughput: ReadCapacityUnits: 5 WriteCapacityUnits: 5 OrdersTable: Type: AWS::DynamoDB::Table Properties: TableName: Orders AttributeDefinitions: - AttributeName: OrderID AttributeType: S - AttributeName: UserID AttributeType: S KeySchema: - AttributeName: OrderID KeyType: HASH - AttributeName: UserID KeyType: RANGE ProvisionedThroughput: ReadCapacityUnits: 5 WriteCapacityUnits: 5
In this template:
UsersTableandOrdersTableare unique logical IDs for each resource (you can name these whatever makes sense for your use case).- Each table has its own
Propertiesblock, where you define its specific schema, key structure, throughput, and any other custom settings.
Quick Recap
- ✅ You can absolutely create multiple DynamoDB tables in one CloudFormation deployment.
- ✅ Use separate
Resourcesentries (one per table) with their own dedicatedPropertiesconfigurations. - ❌ Avoid trying to cram multiple table settings into a single
Resource'sProperties—this isn't supported and will break your template.
内容的提问来源于stack exchange,提问作者Abhijith Nagarjuna




