將政策連接至 DynamoDB 現有資料表 - Amazon DynamoDB

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

將政策連接至 DynamoDB 現有資料表

您可以使用 DynamoDB PutResourcePolicy 主控台、、 API或 AWS CLI AWS SDK AWS CloudFormation 範本 ,將資源型政策連接至現有資料表或修改現有政策。

下列IAM政策範例使用 put-resource-policy AWS CLI 命令,將資源型政策連接至現有資料表。此範例允許使用者 John 在名為 的現有資料表上執行 GetItemUpdateItemPutItemUpdateTableAPI動作 MusicCollection.

請記得取代 italicized 包含資源特定資訊的文字。

aws dynamodb put-resource-policy \ --resource-arn arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection \ --policy \ "{ \"Version\": \"2012-10-17\", \"Statement\": [ { \"Effect\": \"Allow\", \"Principal\": { \"AWS\": \"arn:aws:iam::111122223333:user/John\" }, \"Action\": [ \"dynamodb:GetItem\", \"dynamodb:PutItem\", \"dynamodb:UpdateItem\", \"dynamodb:UpdateTable\" ], \"Resource\": \"arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection\" } ] }"

若要有條件更新資料表的現有資源型政策,您可以使用選用expected-revision-id參數。下列範例只有在 DynamoDB 中存在且其目前的修訂版 ID 符合提供的expected-revision-id參數時,才會更新政策。

aws dynamodb put-resource-policy \ --resource-arn arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection \ --expected-revision-id 1709841168699 \ --policy \ "{ \"Version\": \"2012-10-17\", \"Statement\": [ { \"Effect\": \"Allow\", \"Principal\": { \"AWS\": \"arn:aws:iam::111122223333:user/John\" }, \"Action\": [ \"dynamodb:GetItem\", \"dynamodb:UpdateItem\", \"dynamodb:UpdateTable\" ], \"Resource\": \"arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection\" } ] }"
  1. 登入 AWS Management Console 並在 開啟 DynamoDB 主控台https://console.aws.amazon.com/dynamodb/

  2. 從儀表板中,選擇現有的資料表。

  3. 導覽至許可索引標籤,然後選擇建立資料表政策

  4. 在資源型政策編輯器中,新增您要連接的政策,然後選擇建立政策

    下列IAM政策範例允許使用者 John 在名為 的現有資料表上執行 GetItemUpdateItemPutItemUpdateTableAPI動作 MusicCollection.

    請記得取代 italicized 包含資源特定資訊的文字。

    { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::111122223333:user/John" }, "Action": [ "dynamodb:GetItem", "dynamodb:PutItem", "dynamodb:UpdateItem", "dynamodb:UpdateTable" ], "Resource": "arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection" } ] }

下列IAM政策範例使用 putResourcePolicy方法將資源型政策連接至現有資料表。此政策允許使用者對現有資料表執行GetItemAPI動作。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import software.amazon.awssdk.services.dynamodb.model.PutResourcePolicyRequest; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * Get started with the AWS SDK for Java 2.x */ public class PutResourcePolicy { public static void main(String[] args) { final String usage = """ Usage: <tableArn> <allowedAWSPrincipal> Where: tableArn - The Amazon DynamoDB table ARN to attach the policy to. For example, arn:aws:dynamodb:us-west-2:123456789012:table/MusicCollection. allowed AWS Principal - Allowed AWS principal ARN that the example policy will give access to. For example, arn:aws:iam::123456789012:user/John. """; if (args.length != 2) { System.out.println(usage); System.exit(1); } String tableArn = args[0]; String allowedAWSPrincipal = args[1]; System.out.println("Attaching a resource-based policy to the Amazon DynamoDB table with ARN " + tableArn); Region region = Region.US_WEST_2; DynamoDbClient ddb = DynamoDbClient.builder() .region(region) .build(); String result = putResourcePolicy(ddb, tableArn, allowedAWSPrincipal); System.out.println("Revision ID for the attached policy is " + result); ddb.close(); } public static String putResourcePolicy(DynamoDbClient ddb, String tableArn, String allowedAWSPrincipal) { String policy = generatePolicy(tableArn, allowedAWSPrincipal); PutResourcePolicyRequest request = PutResourcePolicyRequest.builder() .policy(policy) .resourceArn(tableArn) .build(); try { return ddb.putResourcePolicy(request).revisionId(); } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } return ""; } private static String generatePolicy(String tableArn, String allowedAWSPrincipal) { return "{\n" + " \"Version\": \"2012-10-17\",\n" + " \"Statement\": [\n" + " {\n" + " \"Effect\": \"Allow\",\n" + " \"Principal\": {\"AWS\":\"" + allowedAWSPrincipal + "\"},\n" + " \"Action\": [\n" + " \"dynamodb:GetItem\"\n" + " ],\n" + " \"Resource\": \"" + tableArn + "\"\n" + " }\n" + " ]\n" + "}"; } }