使用以資源為基礎的策略建立資料表 - Amazon DynamoDB

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

使用以資源為基礎的策略建立資料表

您可以在使用 DynamoDB 主控台、CreateTableAPI、AWS SDK 或範本建立表格時新增以資源為基礎的政策。 AWS CLI AWS CloudFormation

下列範例會建立MusicCollection使用create-table AWS CLI 命令名稱的資料表。此命令也包含將以資源為基礎的原則新增至資料表的resource-policy參數。此原則可讓使用者 John 在資料表上執行RestoreTableToPointInTimeGetItem、和 PutItemAPI 動作。

請記住將斜體文本替換為特定於資源的信息。

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. 登入 AWS Management Console 並開啟 DynamoDB 支援主控台,網址為 https://console.aws.amazon.com/dynamodb/。

  2. 在儀表板上,選擇 [建立資料表]。

  3. 表格詳細資訊中,輸入表格名稱、分割區索引鍵和排序索引鍵詳細資訊。

  4. [表格設定] 中選擇 [自訂設定]

  5. (選擇性) 指定表格類別容量計算器讀取/寫入容量設定、次要索引靜態加密刪除保護的選項。

  6. 以資源為基礎的策略中,新增政策以定義表格及其索引的存取權限。在此原則中,您可以指定有權存取這些資源的人員,以及允許這些資源對每個資源執行的動作。若要新增原則,請執行下列其中一個動作:

    • 輸入或貼上 JSON 政策文件。如需 IAM 政策語言的詳細資訊,請參閱 IAM 使用者指南中的使用 JSON 編輯器建立政策。

      提示

      若要在 Amazon DynamoDB 開發人員指南中查看以資源為基礎的政策範例,請選擇政策範例。

    • 選擇「新增陳述式」以新增陳述式,並在提供的欄位中輸入資訊。針對您想要新增的任意數量陳述式重複此步驟。

    重要

    儲存原則之前,請務必先解決任何安全性警告、錯誤或建議。

    下列 IAM 政策範例可讓使用者 John 在資料表上執行RestoreTableToPointInTimeGetItem、和 PutItemAPI 動作MusicCollection

    請記住將斜體文本替換為特定於資源的信息。

    { "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. (選用) 選擇右下角的 Preview external access (預覽外部存取),以預覽新政策會如何影響資源的公開和跨帳戶存取權。在儲存政策之前,您可以檢查它是否引入新的 IAM Access Analyzer 問題清單,或是解決現有的問題清單。如果您沒有看到作用中的分析器,請選擇 Go to Access Analyzer (移至 Access Analyzer),以在 IAM Access Analyzer 中建立帳戶分析器。如需詳細資訊,請參閱預覽存取權

  8. 選擇 建立資料表

Using the AWS::DynamoDB::Table resource

下列 CloudFormation 範本會使用:: DynamoDB:AWS: 資料表資源建立包含串流的資料表。此範本還包括附加至資料表和串流的資源型政策。

{ "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 範本會建立包含:: DynamoDB:AWS: 資源的資料表,並將以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": "*" }] } } } } ] } } } }