

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

# 將資源型政策附加至 DynamoDB 串流
<a name="rbac-attach-resource-policy-streams"></a>

您可以使用 DynamoDB 主控台、[PutResourcePolicy](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_PutResourcePolicy.html) API、 AWS CLI、 AWS SDK 或 [CloudFormation 範本](rbac-create-table.md#rbac-create-table-cfn)，將資源型政策連接至現有資料表的串流或修改現有政策。

**注意**  
使用 [CreateTable](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_CreateTable.html) 或 [UpdateTable](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateTable.html) API 建立時，無法將政策附加至串流。不過，資料表刪除後仍可修改或刪除政策。您也可以修改或刪除停用串流的政策。



## AWS CLI
<a name="rbac-attach-policy-stream-CLI"></a>

下列 IAM 政策範例使用 `put-resource-policy` AWS CLI 命令，將資源型政策連接至名為 *MusicCollection* 的資料表串流。此範例允許使用者 *John* 對該串流執行 [GetRecords](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_streams_GetRecords.html)、[GetShardIterator](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_streams_GetShardIterator.html) 與 [DescribeStream](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_streams_DescribeStream.html) API 操作。

請以資源特定資訊取代*斜體*文字。

```
aws dynamodb put-resource-policy \
    --resource-arn arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/stream/2024-02-12T18:57:26.492 \
    --policy \
        "{
            \"Version\": \"2012-10-17\",		 	 	 
            \"Statement\": [
              {
                    \"Effect\": \"Allow\",
                    \"Principal\": {
                        \"AWS\": \"arn:aws:iam::111122223333:user/John\"
                    },
                    \"Action\": [
                        \"dynamodb:GetRecords\",
                        \"dynamodb:GetShardIterator\",
                        \"dynamodb:DescribeStream\"
                    ],
                    \"Resource\": \"arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection/stream/2024-02-12T18:57:26.492\"
                }
            ]
        }"
```

## AWS 管理主控台
<a name="rbac-attach-policy-stream-console"></a>

1. 登入 AWS 管理主控台 ，並在 https：//[https://console.aws.amazon.com/dynamodb/](https://console.aws.amazon.com/dynamodb/) 開啟 DynamoDB 主控台。

1. 在 DynamoDB 主控台的儀表板中，選取**資料表**，然後選取現有資料表。

   請確認所選資料表已啟用串流功能。如需啟用資料表串流的詳細資訊，請參閱 [啟用串流](Streams.md#Streams.Enabling)。

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 開發人員指南中的資源型政策範例，請選取**政策範例**。
   + 選擇**新增陳述式**，以在提供的欄位中輸入資訊並新增新的陳述式。針對您想要新增的任意數量陳述式重複此步驟。
**重要**  
儲存政策前，請務必先解決所有安全性警告、錯誤或建議。

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. 選擇**建立政策**。

下列 IAM 政策範例會將資源型政策附加至名為 *MusicCollection* 的資料表串流。此範例允許使用者 *John* 對該串流執行 [GetRecords](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_streams_GetRecords.html)、[GetShardIterator](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_streams_GetShardIterator.html) 與 [DescribeStream](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_streams_DescribeStream.html) API 操作。

請以資源特定資訊取代*斜體*文字。

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::111122223333:user/username"
      },
      "Action": [
        "dynamodb:GetRecords",
        "dynamodb:GetShardIterator",
        "dynamodb:DescribeStream"
      ],
      "Resource": [
        "arn:aws:dynamodb:us-east-1:123456789012:table/MusicCollection/stream/2024-02-12T18:57:26.492"
      ]
    }
  ]
}
```

------