DynamoDB コンソール、PutResourcePolicy API、AWS CLI、AWS SDK、または AWS CloudFormation テンプレートを使用して、リソースベースのポリシーを既存のテーブルにアタッチしたり、既存のポリシーを変更したりできます。
次の IAM ポリシーの例では、put-resource-policy
AWS CLI コマンドを使用して、リソースベースのポリシーを既存のテーブルにアタッチします。この例では、ユーザー John
に対して、MusicCollection
という名前の既存のテーブルで GetItem、PutItem、UpdateItem、UpdateTable の API アクションを実行することを許可しています。
イタリック体
のテキストを、リソース固有の情報に必ず置き換えてください。
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
\" } ] }"
AWS Management Console にサインインして DynamoDB コンソール (https://console.aws.amazon.com/dynamodb/
) を開きます。 -
ダッシュボードから、既存のテーブルを選択します。
-
[アクセス許可] タブに移動し、[テーブルポリシーを作成] を選択します。
-
[リソースベースのポリシー] エディターで、アタッチするポリシーを追加し、[ポリシーを作成] を選択します。
次の IAM ポリシーの例では、ユーザー
John
に対して、MusicCollection
という名前の既存のテーブルで GetItem、PutItem、UpdateItem、UpdateTable の API アクションを実行することを許可しています。イタリック体
のテキストを、リソース固有の情報に必ず置き換えてください。{ "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
メソッドを使用して、リソースベースのポリシーを既存のテーブルにアタッチしています。このポリシーは、既存のテーブルで GetItem API アクションを実行することをユーザーに許可します。
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
.
allowedAWSPrincipal - 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" +
"}";
}
}