

# 使用基于资源的策略创建一个表
<a name="rbac-create-table"></a>

当使用 DynamoDB 控制台、[CreateTable](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_CreateTable.html) API、AWS CLI、[AWS SDK](rbac-attach-resource-based-policy.md#rbac-attach-policy-java-sdk) 或 CloudFormation 模板创建表时，您可以添加基于资源的策略。

## AWS CLI
<a name="rbac-create-table-CLI"></a>

以下示例将使用 `create-table`AWS CLI 命令创建一个名为 *MusicCollection* 的表。此命令还包括向表中添加基于资源的策略的 `resource-policy` 参数。此策略支持用户 *John* 对表执行 [RestoreTableToPointInTime](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_RestoreTableToPointInTime.html)、[GetItem](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_GetItem.html) 和 [PutItem](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_PutItem.html) 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\"
                }
            ]
        }"
```

## AWS 管理控制台
<a name="rbac-create-table-console"></a>

1. 登录 AWS 管理控制台，并打开 DynamoDB 控制台：[https://console.aws.amazon.com/dynamodb/](https://console.aws.amazon.com/dynamodb/)。

1. 在控制面板上，选择**创建表**。

1. 在**表详细信息**中，输入表名、分区键和排序键的详细信息。

1. 在**表设置**部分，选择**自定义设置**。

1. （可选）为**表类**、**容量计算器**、**读/写容量设置**、**二级索引**、**静态加密**和**删除保护**指定选项。

1. 在**基于资源的策略**中，添加一个策略来定义表及其索引的访问权限。在此策略中，您可以指定谁有权访问这些资源，以及允许他们对每个资源执行的操作。要添加策略，请执行以下操作之一：
   + 键入或粘贴一个 JSON 策略文档。有关 IAM 策略语言的详细信息，请参阅《IAM 用户指南》中的**[使用 JSON 编辑器创建策略](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_create-console.html#access_policies_create-json-editor)。
**提示**  
要查看《Amazon DynamoDB 开发人员指南》中基于资源的策略示例，请选择**策略示例**。
   + 选择**添加语句**来添加新的语句并在提供的字段中输入信息。对所有您想添加的语句重复执行此步骤。
**重要**  
确保在保存策略之前解决任何安全警告、错误或建议。

   以下 IAM 策略允许用户 *John* 对表 *MusicCollection* 执行 [RestoreTableToPointInTime](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_RestoreTableToPointInTime.html)、[GetItem](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_GetItem.html) 和 [PutItem](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_PutItem.html) API 操作。

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

------
#### [ JSON ]

****  

   ```
   {
     "Version":"2012-10-17",		 	 	 
     "Statement": [
       {
         "Effect": "Allow",
         "Principal": {
           "AWS": "arn:aws:iam::123456789012:user/username"
         },
         "Action": [
           "dynamodb:RestoreTableToPointInTime",
           "dynamodb:GetItem",
           "dynamodb:PutItem"
         ],
         "Resource": "arn:aws:dynamodb:us-east-1:123456789012:table/MusicCollection"
       }
     ]
   }
   ```

------

1. （可选）选择右下角的**预览外部访问**，以预览新策略如何影响对资源的公有和跨账户访问。在保存策略之前，您可以检查策略是引入了新的 IAM Access Analyzer 发现结果还是解析了现有的发现结果。如果您没有看到活动的分析器，请在 IAM Access Analyzer 中选择**转至 Access Analyzer** 以[创建账户分析器](https://docs.aws.amazon.com/IAM/latest/UserGuide/access-analyzer-getting-started.html#access-analyzer-enabling)。有关更多信息，请参阅[预览访问](https://docs.aws.amazon.com/IAM/latest/UserGuide/access-analyzer-access-preview.html)。

1. 选择**创建表**。

## AWS CloudFormation 模板
<a name="rbac-create-table-cfn"></a>

------
#### [ Using the AWS::DynamoDB::Table resource ]

以下 CloudFormation 模板使用 [AWS::DynamoDB::Table](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-dynamodb-table.html) 资源创建带有流的表。此模板还包括附加到表和流的基于资源的策略。

```
{
    "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 AWS::DynamoDB::GlobalTable resource ]

以下 CloudFormation 模板使用 [AWS::DynamoDB::GlobalTable](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-dynamodb-globaltable.html) 创建一个表，并将基于资源的策略附加到该表及其流。

```
{
    "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": "*"
                                    }]
                                }
                            }
                        }
                    }
                ]
            }
        }
    }
}
```

------