AWS CloudFormation
User Guide (API Version 2010-05-15)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。请点击 Amazon AWS 入门,可查看中国地区的具体差异

AWS::DynamoDB::Table

AWS::DynamoDB::Table 资源创建 DynamoDB 表。

注意

AWS CloudFormation 通常并行创建 DynamoDB 表。但是,如果模板包含多个带索引的 DynamoDB 表,则必须声明依赖关系,才能按顺序创建表。Amazon DynamoDB 对二级索引处于创建状态的表的数量有限制。如果同时创建多个带索引的表,则 DynamoDB 会返回错误,堆栈运行失败。有关示例,请参阅具有 DependsOn 属性的 DynamoDB 表

语法

要在 AWS CloudFormation 模板中声明此实体,请使用以下语法:

JSON

{ "Type" : "AWS::DynamoDB::Table", "Properties" : { "AttributeDefinitions" : [ AttributeDefinitions, ... ], "GlobalSecondaryIndexes" : [ GlobalSecondaryIndexes, ... ], "KeySchema" : [ KeySchema, ... ], "LocalSecondaryIndexes" : [ LocalSecondaryIndexes, ... ], "ProvisionedThroughput" : ProvisionedThroughput, "StreamSpecification" : StreamSpecification, "TableName" : String, "Tags" : [ 资源标签, ... ], "TimeToLiveSpecification" : TimeToLiveSpecification } }

YAML

Type: "AWS::DynamoDB::Table" Properties: AttributeDefinitions: - AttributeDefinitions GlobalSecondaryIndexes: - GlobalSecondaryIndexes KeySchema: - KeySchema LocalSecondaryIndexes: - LocalSecondaryIndexes ProvisionedThroughput: ProvisionedThroughput StreamSpecification: StreamSpecification TableName: String Tags: - 资源标签 TimeToLiveSpecification: TimeToLiveSpecification

属性

AttributeDefinitions

说明表和索引的键架构的 AttributeNameAttributeType 对象列表。允许重复项。

必需:是

类型DynamoDB 表 AttributeDefinitions 的列表

更新要求无需中断

GlobalSecondaryIndexes

要对表创建的全局二级索引。可以创建最多 5 个全局二级索引。

重要

如果您更新表以包含新的全局二级索引,则 AWS CloudFormation 会启动索引创建,然后继续更新堆栈。AWS CloudFormation 不等待索引创建完成,因为回填阶段可能需要较长时间,具体视表的大小而定。在索引状态变成 ACTIVE 之前,您无法使用此索引或更新此表。您可以使用 DynamoDB DescribeTable 命令跟踪其状态。

如果您在更新期间添加或删除索引,我们建议您不要更新任何其他资源。如果在添加新索引期间堆栈更新失败并回滚,则必须手动删除此索引。

必需:否

类型DynamoDB 表 GlobalSecondaryIndexes 的列表

更新要求:不支持更新。 存在以下例外:

  • 如果只更新全局二级索引的预置吞吐量值,则可以在不中断的情况下更新表。

  • 您可以在不中断的情况下删除或添加全局二级索引。如果您在一次更新中同时执行删除和添加操作(例如:更改索引的逻辑 ID),则更新将失败。

KeySchema

指定组成表主键的属性。KeySchema 属性中的属性还必须在 AttributeDefinitions 属性中定义。

必需:是

类型DynamoDB 表 KeySchema 的列表

更新要求替换

LocalSecondaryIndexes

要对表创建的本地二级索引。可以创建最多 5 个本地二级索引。每个索引的范围都限定到给定哈希键值。每个哈希键的大小最大可以为 10 GB。

必需:否

类型DynamoDB 表 LocalSecondaryIndexes 的列表

更新要求替换

ProvisionedThroughput

指定的表的吞吐量,由 ReadCapacityUnitsWriteCapacityUnits 的值组成。有关预置吞吐量结构内容的更多信息,请参阅 Amazon DynamoDB 表 ProvisionedThroughput

必需:是

类型DynamoDB 表 ProvisionedThroughput

更新要求无需中断

StreamSpecification

DynamoDB 表流的设置,用于捕获对存储在表中的项目的变更。

必需:否

Type: DynamoDB 表 StreamSpecification

更新要求无需中断表;但流会被替换。

TableName

表的名称。如果不指定名称,则 AWS CloudFormation 将生成一个唯一物理 ID 并将该 ID 用作表名称。有关更多信息,请参阅 名称类型

重要

如果指定一个名称,您将无法执行需要替换此资源的更新。您可以执行不需要或者只需要部分中断的更新。如果必须替换资源,请指定新名称。

必需:否

Type: String

更新要求替换

Tags

指定要与该表关联的任意标签组 (键值对)。使用标签来管理您的资源。

Required: No

Type: AWS CloudFormation 资源标签

更新要求无需中断

TimeToLiveSpecification

为表指定生存时间 (TTL) 设置。

Required: No

Type: DynamoDB Table TimeToLiveSpecification

更新要求无需中断

注意

有关 DynamoDB 中的限制的详细信息,请参阅 Amazon DynamoDB 开发人员指南 中的 Amazon DynamoDB 中的限制

返回值

Ref

当该资源的逻辑 ID 提供给 Ref内部函数时,Ref 将返回资源名称。 示例:

{ "Ref": "MyResource" }

对于逻辑 ID 为 myDynamoDBTable 的资源,Ref 将返回 DynamoDB 表名称。

有关使用 Ref 功能的更多信息,请参阅参考

Fn::GetAtt

Fn::GetAtt 返回一个此类型指定属性的值。以下为可用属性和示例返回值。

Arn

DynamoDB 表的 Amazon 资源名称 (ARN),如 arn:aws:dynamodb:us-west-2:123456789012:table/myDynamoDBTable

StreamArn

DynamoDB 流的 ARN,例如 arn:aws:dynamodb:us-east-1:123456789012:table/testddbstack-myDynamoDBTable-012A1SL7SMP5Q/stream/2015-11-30T20:10:00.000

注意

您必须指定 StreamSpecification 属性才能使用此特性。

有关使用 Fn::GetAtt 的更多信息,请参见 Fn::GetAtt

示例

具有本地和二级索引的 DynamoDB 表

下面的示例创建一个包含 AlbumArtistSalesNumberOfSongs 作为属性的 DynamoDB 表。主键包含 Album 属性作为哈希键,包含 Artist 作为范围键。此表还包含两个全局索引和一个二级索引。查询给定艺术家的销量时,全局二级索引使用 Sales 属性作为哈希键,使用 Artist 属性作为范围键。

基于歌曲编号查询销量时,全局二级索引使用 NumberOfSongs 属性作为哈希键,使用 Sales 属性作为范围键。

查询专辑销量时,本地二级索引使用与表相同的哈希键,但是使用 Sales 属性作为范围键。

JSON

{ "AWSTemplateFormatVersion" : "2010-09-09", "Resources" : { "myDynamoDBTable" : { "Type" : "AWS::DynamoDB::Table", "Properties" : { "AttributeDefinitions" : [ { "AttributeName" : "Album", "AttributeType" : "S" }, { "AttributeName" : "Artist", "AttributeType" : "S" }, { "AttributeName" : "Sales", "AttributeType" : "N" }, { "AttributeName" : "NumberOfSongs", "AttributeType" : "N" } ], "KeySchema" : [ { "AttributeName" : "Album", "KeyType" : "HASH" }, { "AttributeName" : "Artist", "KeyType" : "RANGE" } ], "ProvisionedThroughput" : { "ReadCapacityUnits" : "5", "WriteCapacityUnits" : "5" }, "TableName" : "myTableName", "GlobalSecondaryIndexes" : [{ "IndexName" : "myGSI", "KeySchema" : [ { "AttributeName" : "Sales", "KeyType" : "HASH" }, { "AttributeName" : "Artist", "KeyType" : "RANGE" } ], "Projection" : { "NonKeyAttributes" : ["Album","NumberOfSongs"], "ProjectionType" : "INCLUDE" }, "ProvisionedThroughput" : { "ReadCapacityUnits" : "5", "WriteCapacityUnits" : "5" } }, { "IndexName" : "myGSI2", "KeySchema" : [ { "AttributeName" : "NumberOfSongs", "KeyType" : "HASH" }, { "AttributeName" : "Sales", "KeyType" : "RANGE" } ], "Projection" : { "NonKeyAttributes" : ["Album","Artist"], "ProjectionType" : "INCLUDE" }, "ProvisionedThroughput" : { "ReadCapacityUnits" : "5", "WriteCapacityUnits" : "5" } }], "LocalSecondaryIndexes" :[{ "IndexName" : "myLSI", "KeySchema" : [ { "AttributeName" : "Album", "KeyType" : "HASH" }, { "AttributeName" : "Sales", "KeyType" : "RANGE" } ], "Projection" : { "NonKeyAttributes" : ["Artist","NumberOfSongs"], "ProjectionType" : "INCLUDE" } }] } } } }

YAML

AWSTemplateFormatVersion: "2010-09-09" Resources: myDynamoDBTable: Type: "AWS::DynamoDB::Table" Properties: AttributeDefinitions: - AttributeName: "Album" AttributeType: "S" - AttributeName: "Artist" AttributeType: "S" - AttributeName: "Sales" AttributeType: "N" - AttributeName: "NumberOfSongs" AttributeType: "N" KeySchema: - AttributeName: "Album" KeyType: "HASH" - AttributeName: "Artist" KeyType: "RANGE" ProvisionedThroughput: ReadCapacityUnits: "5" WriteCapacityUnits: "5" TableName: "myTableName" GlobalSecondaryIndexes: - IndexName: "myGSI" KeySchema: - AttributeName: "Sales" KeyType: "HASH" - AttributeName: "Artist" KeyType: "RANGE" Projection: NonKeyAttributes: - "Album" - "NumberOfSongs" ProjectionType: "INCLUDE" ProvisionedThroughput: ReadCapacityUnits: "5" WriteCapacityUnits: "5" - IndexName: "myGSI2" KeySchema: - AttributeName: "NumberOfSongs" KeyType: "HASH" - AttributeName: "Sales" KeyType: "RANGE" Projection: NonKeyAttributes: - "Album" - "Artist" ProjectionType: "INCLUDE" ProvisionedThroughput: ReadCapacityUnits: "5" WriteCapacityUnits: "5" LocalSecondaryIndexes: - IndexName: "myLSI" KeySchema: - AttributeName: "Album" KeyType: "HASH" - AttributeName: "Sales" KeyType: "RANGE" Projection: NonKeyAttributes: - "Artist" - "NumberOfSongs" ProjectionType: "INCLUDE"

具有 DependsOn 属性的 DynamoDB 表

如果单个模板包含多个具有索引的 DynamoDB 表,则您必须包含依赖关系,以使表按顺序创建。DynamoDB 对二级索引处于创建状态的表的数量有限制。如果同时创建多个带索引的表,则 DynamoDB 会返回错误,堆栈运行失败。

以下示例假设 myFirstDDBTable 表在与 mySecondDDBTable 表相同的模板中声明,两个表都包含一个二级索引。mySecondDDBTable 表包含对 myFirstDDBTable 表的依赖关系,因此 AWS CloudFormation 一次创建一个表。

JSON

"mySecondDDBTable" : { "Type" : "AWS::DynamoDB::Table", "DependsOn" : "myFirstDDBTable" , "Properties" : { "AttributeDefinitions" : [ { "AttributeName" : "ArtistId", "AttributeType" : "S" }, { "AttributeName" : "Concert", "AttributeType" : "S" }, { "AttributeName" : "TicketSales", "AttributeType" : "S" } ], "KeySchema" : [ { "AttributeName" : "ArtistId", "KeyType" : "HASH" }, { "AttributeName" : "Concert", "KeyType" : "RANGE" } ], "ProvisionedThroughput" : { "ReadCapacityUnits" : {"Ref" : "ReadCapacityUnits"}, "WriteCapacityUnits" : {"Ref" : "WriteCapacityUnits"} }, "GlobalSecondaryIndexes" : [{ "IndexName" : "myGSI", "KeySchema" : [ { "AttributeName" : "TicketSales", "KeyType" : "HASH" } ], "Projection" : { "ProjectionType" : "KEYS_ONLY" }, "ProvisionedThroughput" : { "ReadCapacityUnits" : {"Ref" : "ReadCapacityUnits"}, "WriteCapacityUnits" : {"Ref" : "WriteCapacityUnits"} } }], "Tags": [ { "Key": "foo", "Value": "bar" } ] } }

YAML

mySecondDDBTable: Type: "AWS::DynamoDB::Table" DependsOn: "myFirstDDBTable" Properties: AttributeDefinitions: - AttributeName: "ArtistId" AttributeType: "S" - AttributeName: "Concert" AttributeType: "S" - AttributeName: "TicketSales" AttributeType: "S" KeySchema: - AttributeName: "ArtistId" KeyType: "HASH" - AttributeName: "Concert" KeyType: "RANGE" ProvisionedThroughput: ReadCapacityUnits: Ref: "ReadCapacityUnits" WriteCapacityUnits: Ref: "WriteCapacityUnits" GlobalSecondaryIndexes: - IndexName: "myGSI" KeySchema: - AttributeName: "TicketSales" KeyType: "HASH" Projection: ProjectionType: "KEYS_ONLY" ProvisionedThroughput: ReadCapacityUnits: Ref: "ReadCapacityUnits" WriteCapacityUnits: Ref: "WriteCapacityUnits" Tags: - Key: foo Value: bar

带 Application Auto Scaling 的 DynamoDB 表

此示例为 AWS::DynamoDB::Table 资源设置 Application Auto Scaling。此模板定义扩展表的 WriteCapacityUnits 吞吐量的 TargetTrackingScaling 扩展策略。

JSON

{ "Resources": { "DDBTable": { "Type": "AWS::DynamoDB::Table", "Properties": { "AttributeDefinitions": [ { "AttributeName": "ArtistId", "AttributeType": "S" }, { "AttributeName": "Concert", "AttributeType": "S" }, { "AttributeName": "TicketSales", "AttributeType": "S" } ], "KeySchema": [ { "AttributeName": "ArtistId", "KeyType": "HASH" }, { "AttributeName": "Concert", "KeyType": "RANGE" } ], "GlobalSecondaryIndexes": [ { "IndexName": "GSI", "KeySchema": [ { "AttributeName": "TicketSales", "KeyType": "HASH" } ], "Projection": { "ProjectionType": "KEYS_ONLY" }, "ProvisionedThroughput": { "ReadCapacityUnits": 5, "WriteCapacityUnits": 5 } } ], "ProvisionedThroughput": { "ReadCapacityUnits": 5, "WriteCapacityUnits": 5 } } }, "WriteCapacityScalableTarget": { "Type": "AWS::ApplicationAutoScaling::ScalableTarget", "Properties": { "MaxCapacity": 15, "MinCapacity": 5, "ResourceId": { "Fn::Join": [ "/", [ "table", { "Ref": "DDBTable" } ] ] }, "RoleARN": { "Fn::GetAtt": ["ScalingRole", "Arn"] }, "ScalableDimension": "dynamodb:table:WriteCapacityUnits", "ServiceNamespace": "dynamodb" } }, "ScalingRole": { "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": [ "application-autoscaling.amazonaws.com" ] }, "Action": [ "sts:AssumeRole" ] } ] }, "Path": "/", "Policies": [ { "PolicyName": "root", "PolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "dynamodb:DescribeTable", "dynamodb:UpdateTable", "cloudwatch:PutMetricAlarm", "cloudwatch:DescribeAlarms", "cloudwatch:GetMetricStatistics", "cloudwatch:SetAlarmState", "cloudwatch:DeleteAlarms" ], "Resource": "*" } ] } } ] } }, "WriteScalingPolicy": { "Type": "AWS::ApplicationAutoScaling::ScalingPolicy", "Properties": { "PolicyName": "WriteAutoScalingPolicy", "PolicyType": "TargetTrackingScaling", "ScalingTargetId": { "Ref": "WriteCapacityScalableTarget" }, "TargetTrackingScalingPolicyConfiguration": { "TargetValue": 50.0, "ScaleInCooldown": 60, "ScaleOutCooldown": 60, "PredefinedMetricSpecification": { "PredefinedMetricType": "DynamoDBWriteCapacityUtilization" } } } } } }

YAML

Resources: DDBTable: Type: "AWS::DynamoDB::Table" Properties: AttributeDefinitions: - AttributeName: "ArtistId" AttributeType: "S" - AttributeName: "Concert" AttributeType: "S" - AttributeName: "TicketSales" AttributeType: "S" KeySchema: - AttributeName: "ArtistId" KeyType: "HASH" - AttributeName: "Concert" KeyType: "RANGE" GlobalSecondaryIndexes: - IndexName: "GSI" KeySchema: - AttributeName: "TicketSales" KeyType: "HASH" Projection: ProjectionType: "KEYS_ONLY" ProvisionedThroughput: ReadCapacityUnits: 5 WriteCapacityUnits: 5 ProvisionedThroughput: ReadCapacityUnits: 5 WriteCapacityUnits: 5 WriteCapacityScalableTarget: Type: "AWS::ApplicationAutoScaling::ScalableTarget" Properties: MaxCapacity: 15 MinCapacity: 5 ResourceId: !Join - / - - table - !Ref DDBTable RoleARN: !GetAtt ScalingRole.Arn ScalableDimension: dynamodb:table:WriteCapacityUnits ServiceNamespace: dynamodb ScalingRole: Type: "AWS::IAM::Role" Properties: AssumeRolePolicyDocument: Version: "2012-10-17" Statement: - Effect: "Allow" Principal: Service: - application-autoscaling.amazonaws.com Action: - "sts:AssumeRole" Path: "/" Policies: - PolicyName: "root" PolicyDocument: Version: "2012-10-17" Statement: - Effect: "Allow" Action: - "dynamodb:DescribeTable" - "dynamodb:UpdateTable" - "cloudwatch:PutMetricAlarm" - "cloudwatch:DescribeAlarms" - "cloudwatch:GetMetricStatistics" - "cloudwatch:SetAlarmState" - "cloudwatch:DeleteAlarms" Resource: "*" WriteScalingPolicy: Type: "AWS::ApplicationAutoScaling::ScalingPolicy" Properties: PolicyName: WriteAutoScalingPolicy PolicyType: TargetTrackingScaling ScalingTargetId: !Ref WriteCapacityScalableTarget TargetTrackingScalingPolicyConfiguration: TargetValue: 50.0 ScaleInCooldown: 60 ScaleOutCooldown: 60 PredefinedMetricSpecification: PredefinedMetricType: DynamoDBWriteCapacityUtilization

本页内容: