

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用資源型政策建立資料表
<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 管理主控台 ，並在 https：//[https://console.aws.amazon.com/dynamodb/](https://console.aws.amazon.com/dynamodb/) 開啟 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. (選用) 選擇右下角的 **Preview external access** (預覽外部存取)，以預覽新政策會如何影響資源的公開和跨帳戶存取權。在儲存政策之前，您可以檢查它是否引入新的 IAM Access Analyzer 問題清單，或是解決現有的問題清單。如果您沒有看到作用中的分析器，請選擇 **Go to Access Analyzer** (移至 Access Analyzer)，以在 IAM 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. 選擇 **Create Table** (建立資料表)。

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

------