使用基于资源的策略创建一个表 - Amazon DynamoDB
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

使用基于资源的策略创建一个表

在创建表时,您可以使用 DynamoDB 控制台、CreateTable API、Amazon CLI、Amazon SDK 或 Amazon CloudFormation 模板添加基于资源的策略。

以下示例将使用 create-tableAmazon CLI 命令创建一个名为 MusicCollection 的表。此命令还包括向表中添加基于资源的策略的 resource-policy 参数。此策略允许用户 John 对表执行 RestoreTableToPointInTimeGetItemPutItem API 操作。

切记用特定资源信息替换斜体文本。

aws dynamodb create-table \ --table-name MusicCollection \ --attribute-definitions AttributeName=Artist,AttributeType=S AttributeName=SongTitle,AttributeType=S \ --key-schema AttributeName=Artist,KeyType=HASH AttributeName=SongTitle,KeyType=RANGE \ --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \ --resource-policy \ "{ \"Version\": \"2012-10-17\", \"Statement\": [ { \"Effect\": \"Allow\", \"Principal\": { \"AWS\": \"arn:aws:iam::123456789012:user/John\" }, \"Action\": [ \"dynamodb:RestoreTableToPointInTime\", \"dynamodb:GetItem\", \"dynamodb:DescribeTable\" ], \"Resource\": \"arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection\" } ] }"
  1. 登录 Amazon Web Services Management Console,并打开 DynamoDB 控制台:https://console.aws.amazon.com/dynamodb/

  2. 在控制面板上,选择创建表

  3. 表详细信息中,输入表名、分区键和排序键的详细信息。

  4. 表设置部分,选择自定义设置

  5. (可选)为表类容量计算器读/写容量设置二级索引静态加密删除保护指定选项。

  6. 基于资源的策略中,添加一个策略来定义表及其索引的访问权限。在此策略中,您可以指定谁有权访问这些资源,以及允许他们对每个资源执行的操作。要添加策略,请执行以下操作之一:

    • 键入或粘贴一个 JSON 策略文档。有关 IAM 策略语言的详细信息,请参阅《IAM 用户指南》中的使用 JSON 编辑器创建策略

      提示

      要查看《Amazon DynamoDB 开发人员指南》中基于资源的策略示例,请选择策略示例

    • 选择添加语句来添加新的语句并在提供的字段中输入信息。对所有您想添加的语句重复执行此步骤。

    重要

    确保在保存策略之前解决任何安全警告、错误或建议。

    以下 IAM 策略允许用户 John 对表 MusicCollection 执行 RestoreTableToPointInTimeGetItemPutItem API 操作。

    切记用特定资源信息替换斜体文本。

    { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::123456789012:user/John" }, "Action": [ "dynamodb:RestoreTableToPointInTime", "dynamodb:GetItem", "dynamodb:PutItem" ], "Resource": "arn:aws:dynamodb:us-east-1:123456789012:table/MusicCollection" } ] }
  7. (可选)选择右下角的预览外部访问,以预览新策略如何影响对资源的公有和跨账户访问。在保存策略之前,您可以检查策略是引入了新的 IAM Access Analyzer 发现结果还是解析了现有的发现结果。如果您没有看到活动的分析器,请在 IAM Access Analyzer 中选择转至 Access Analyzer创建账户分析器。有关更多信息,请参阅预览访问

  8. 选择创建表

Using the Amazon::DynamoDB::Table resource

以下 CloudFormation 模板使用 Amazon::DynamoDB::Table 资源创建带有流的表。此模板还包括附加到表和流的基于资源的策略。

{ "AWSTemplateFormatVersion": "2010-09-09", "Resources": { "MusicCollectionTable": { "Type": "AWS::DynamoDB::Table", "Properties": { "AttributeDefinitions": [ { "AttributeName": "Artist", "AttributeType": "S" } ], "KeySchema": [ { "AttributeName": "Artist", "KeyType": "HASH" } ], "BillingMode": "PROVISIONED", "ProvisionedThroughput": { "ReadCapacityUnits": 5, "WriteCapacityUnits": 5 }, "StreamSpecification": { "StreamViewType": "OLD_IMAGE", "ResourcePolicy": { "PolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Principal": { "AWS": "arn:aws:iam::111122223333:user/John" }, "Effect": "Allow", "Action": [ "dynamodb:GetRecords", "dynamodb:GetShardIterator", "dynamodb:DescribeStream" ], "Resource": "*" } ] } } }, "TableName": "MusicCollection", "ResourcePolicy": { "PolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Principal": { "AWS": [ "arn:aws:iam::111122223333:user/John" ] }, "Effect": "Allow", "Action": "dynamodb:GetItem", "Resource": "*" } ] } } } } } }
Using the Amazon::DynamoDB::GlobalTable resource

以下 CloudFormation 模板使用 Amazon::DynamoDB::GlobalTable 创建一个表,并将基于资源的策略附加到该表及其流。

{ "AWSTemplateFormatVersion": "2010-09-09", "Resources": { "GlobalMusicCollection": { "Type": "AWS::DynamoDB::GlobalTable", "Properties": { "TableName": "MusicCollection", "AttributeDefinitions": [{ "AttributeName": "Artist", "AttributeType": "S" }], "KeySchema": [{ "AttributeName": "Artist", "KeyType": "HASH" }], "BillingMode": "PAY_PER_REQUEST", "StreamSpecification": { "StreamViewType": "NEW_AND_OLD_IMAGES" }, "Replicas": [ { "Region": "us-east-1", "ResourcePolicy": { "PolicyDocument": { "Version": "2012-10-17", "Statement": [{ "Principal": { "AWS": [ "arn:aws:iam::111122223333:user/John" ] }, "Effect": "Allow", "Action": "dynamodb:GetItem", "Resource": "*" }] } }, "ReplicaStreamSpecification": { "ResourcePolicy": { "PolicyDocument": { "Version": "2012-10-17", "Statement": [{ "Principal": { "AWS": "arn:aws:iam::111122223333:user/John" }, "Effect": "Allow", "Action": [ "dynamodb:GetRecords", "dynamodb:GetShardIterator", "dynamodb:DescribeStream" ], "Resource": "*" }] } } } } ] } } } }