选择您的 Cookie 首选项

我们使用必要 Cookie 和类似工具提供我们的网站和服务。我们使用性能 Cookie 收集匿名统计数据,以便我们可以了解客户如何使用我们的网站并进行改进。必要 Cookie 无法停用,但您可以单击“自定义”或“拒绝”来拒绝性能 Cookie。

如果您同意,AWS 和经批准的第三方还将使用 Cookie 提供有用的网站功能、记住您的首选项并显示相关内容,包括相关广告。要接受或拒绝所有非必要 Cookie,请单击“接受”或“拒绝”。要做出更详细的选择,请单击“自定义”。

将 ABAC 与 DynamoDB 表和索引结合使用的示例

聚焦模式
将 ABAC 与 DynamoDB 表和索引结合使用的示例 - Amazon DynamoDB

以下示例描述了一些使用标签来实现基于属性的条件的用例。

示例 1:使用 aws:ResourceTag 支持操作

使用 aws:ResourceTag/tag-key 条件键,可以将在 IAM 策略中指定的标签键值对与 DynamoDB 表中附加的键值对进行比较。例如,如果 IAM 策略和表中的标签条件匹配,则可以支持执行特定操作,例如 PutItem。为此,请执行以下步骤:

Using the AWS CLI
  1. 创建表。以下示例使用 create-table AWS CLI 命令来创建名为 myMusicTable 的表。

    aws dynamodb create-table \ --table-name myMusicTable \ --attribute-definitions AttributeName=id,AttributeType=S \ --key-schema AttributeName=id,KeyType=HASH \ --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \ --region us-east-1
  2. 向此表添加标签。以下 tag-resource AWS CLI 命令示例将标签键值对 Title: ProductManager 添加到 myMusicTable

    aws dynamodb tag-resource --region us-east-1 --resource-arn arn:aws:dynamodb:us-east-1:123456789012:table/myMusicTable --tags Key=Title,Value=ProductManager
  3. 创建内联策略并将其添加到附加了 AmazonDynamoDBReadOnlyAccess AWS 托管式策略的角色,如以下示例所示。

    { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "dynamodb:PutItem", "Resource": "arn:aws:dynamodb:*:*:table/*", "Condition": { "StringEquals": { "aws:ResourceTag/Title": "ProductManager" } } } ] }

    当附加到表的标签键和值与在策略中指定的标签匹配时,此策略支持对表执行 PutItem 操作。

  4. 使用在步骤 3 中描述的策略代入该角色。

  5. 使用 put-item AWS CLI 命令将项目放置到 myMusicTable

    aws dynamodb put-item \ --table-name myMusicTable --region us-east-1 \ --item '{ "id": {"S": "2023"}, "title": {"S": "Happy Day"}, "info": {"M": { "rating": {"N": "9"}, "Artists": {"L": [{"S": "Acme Band"}, {"S": "No One You Know"}]}, "release_date": {"S": "2023-07-21"} }} }'
  6. 扫描该表,以验证该项目是否已添加到表。

    aws dynamodb scan --table-name myMusicTable --region us-east-1
Using the AWS SDK for Java 2.x
  1. 创建表。以下示例使用 CreateTable API 来创建名为 myMusicTable 的表。

    DynamoDbClient dynamoDB = DynamoDbClient.builder().region(region).build(); CreateTableRequest createTableRequest = CreateTableRequest.builder() .attributeDefinitions( Arrays.asList( AttributeDefinition.builder() .attributeName("id") .attributeType(ScalarAttributeType.S) .build() ) ) .keySchema( Arrays.asList( KeySchemaElement.builder() .attributeName("id") .keyType(KeyType.HASH) .build() ) ) .provisionedThroughput(ProvisionedThroughput.builder() .readCapacityUnits(5L) .writeCapacityUnits(5L) .build() ) .tableName("myMusicTable") .build(); CreateTableResponse createTableResponse = dynamoDB.createTable(createTableRequest); String tableArn = createTableResponse.tableDescription().tableArn(); String tableName = createTableResponse.tableDescription().tableName();
  2. 向此表添加标签。以下示例中的 TagResource API 将标签键值对 Title: ProductManager 添加到 myMusicTable

    TagResourceRequest tagResourceRequest = TagResourceRequest.builder() .resourceArn(tableArn) .tags( Arrays.asList( Tag.builder() .key("Title") .value("ProductManager") .build() ) ) .build(); dynamoDB.tagResource(tagResourceRequest);
  3. 创建内联策略并将其添加到附加了 AmazonDynamoDBReadOnlyAccess AWS 托管式策略的角色,如以下示例所示。

    { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "dynamodb:PutItem", "Resource": "arn:aws:dynamodb:*:*:table/*", "Condition": { "StringEquals": { "aws:ResourceTag/Title": "ProductManager" } } } ] }

    当附加到表的标签键和值与在策略中指定的标签匹配时,此策略支持对表执行 PutItem 操作。

  4. 使用在步骤 3 中描述的策略代入该角色。

  5. 使用 PutItem API 将项目放置到 myMusicTable

    HashMap<String, AttributeValue> info = new HashMap<>(); info.put("rating", AttributeValue.builder().s("9").build()); info.put("artists", AttributeValue.builder().ss(List.of("Acme Band","No One You Know").build()); info.put("release_date", AttributeValue.builder().s("2023-07-21").build()); HashMap<String, AttributeValue> itemValues = new HashMap<>(); itemValues.put("id", AttributeValue.builder().s("2023").build()); itemValues.put("title", AttributeValue.builder().s("Happy Day").build()); itemValues.put("info", AttributeValue.builder().m(info).build()); PutItemRequest putItemRequest = PutItemRequest.builder() .tableName(tableName) .item(itemValues) .build(); dynamoDB.putItem(putItemRequest);
  6. 扫描该表,以验证该项目是否已添加到表。

    ScanRequest scanRequest = ScanRequest.builder() .tableName(tableName) .build(); ScanResponse scanResponse = dynamoDB.scan(scanRequest);
Using the AWS SDK for Python (Boto3)
  1. 创建表。以下示例使用 CreateTable API 来创建名为 myMusicTable 的表。

    create_table_response = ddb_client.create_table( AttributeDefinitions=[ { 'AttributeName': 'id', 'AttributeType': 'S' }, ], TableName='myMusicTable', KeySchema=[ { 'AttributeName': 'id', 'KeyType': 'HASH' }, ], ProvisionedThroughput={ 'ReadCapacityUnits': 5, 'WriteCapacityUnits': 5 }, ) table_arn = create_table_response['TableDescription']['TableArn']
  2. 向此表添加标签。以下示例中的 TagResource API 将标签键值对 Title: ProductManager 添加到 myMusicTable

    tag_resouce_response = ddb_client.tag_resource( ResourceArn=table_arn, Tags=[ { 'Key': 'Title', 'Value': 'ProductManager' }, ] )
  3. 创建内联策略并将其添加到附加了 AmazonDynamoDBReadOnlyAccess AWS 托管式策略的角色,如以下示例所示。

    { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "dynamodb:PutItem", "Resource": "arn:aws:dynamodb:*:*:table/*", "Condition": { "StringEquals": { "aws:ResourceTag/Title": "ProductManager" } } } ] }

    当附加到表的标签键和值与在策略中指定的标签匹配时,此策略支持对表执行 PutItem 操作。

  4. 使用在步骤 3 中描述的策略代入该角色。

  5. 使用 PutItem API 将项目放置到 myMusicTable

    put_item_response = client.put_item( TableName = 'myMusicTable' Item = { 'id': '2023', 'title': 'Happy Day', 'info': { 'rating': '9', 'artists': ['Acme Band','No One You Know'], 'release_date': '2023-07-21' } } )
  6. 扫描该表,以验证该项目是否已添加到表。

    scan_response = client.scan( TableName='myMusicTable' )
  1. 创建表。以下示例使用 create-table AWS CLI 命令来创建名为 myMusicTable 的表。

    aws dynamodb create-table \ --table-name myMusicTable \ --attribute-definitions AttributeName=id,AttributeType=S \ --key-schema AttributeName=id,KeyType=HASH \ --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \ --region us-east-1
  2. 向此表添加标签。以下 tag-resource AWS CLI 命令示例将标签键值对 Title: ProductManager 添加到 myMusicTable

    aws dynamodb tag-resource --region us-east-1 --resource-arn arn:aws:dynamodb:us-east-1:123456789012:table/myMusicTable --tags Key=Title,Value=ProductManager
  3. 创建内联策略并将其添加到附加了 AmazonDynamoDBReadOnlyAccess AWS 托管式策略的角色,如以下示例所示。

    { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "dynamodb:PutItem", "Resource": "arn:aws:dynamodb:*:*:table/*", "Condition": { "StringEquals": { "aws:ResourceTag/Title": "ProductManager" } } } ] }

    当附加到表的标签键和值与在策略中指定的标签匹配时,此策略支持对表执行 PutItem 操作。

  4. 使用在步骤 3 中描述的策略代入该角色。

  5. 使用 put-item AWS CLI 命令将项目放置到 myMusicTable

    aws dynamodb put-item \ --table-name myMusicTable --region us-east-1 \ --item '{ "id": {"S": "2023"}, "title": {"S": "Happy Day"}, "info": {"M": { "rating": {"N": "9"}, "Artists": {"L": [{"S": "Acme Band"}, {"S": "No One You Know"}]}, "release_date": {"S": "2023-07-21"} }} }'
  6. 扫描该表,以验证该项目是否已添加到表。

    aws dynamodb scan --table-name myMusicTable --region us-east-1
不带 ABAC

如果您未为 AWS 账户启用 ABAC,则 IAM 策略和 DynamoDB 表中的标签条件将不匹配。因此,由于 AmazonDynamoDBReadOnlyAccess 策略的效果,PutItem 操作将返回 AccessDeniedException

An error occurred (AccessDeniedException) when calling the PutItem operation: User: arn:aws:sts::123456789012:assumed-role/DynamoDBReadOnlyAccess/Alice is not authorized to perform: dynamodb:PutItem on resource: arn:aws:dynamodb:us-east-1:123456789012:table/myMusicTable because no identity-based policy allows the dynamodb:PutItem action.
带有 ABAC

如果您为 AWS 账户启用了 ABAC,则 put-item 操作将成功完成,并向表中添加一个新项目。这是因为,如果 IAM 策略和表中的标签条件匹配,则表中的内联策略支持 PutItem 操作。

示例 2:使用 aws:RequestTag 支持操作

使用 aws:RequestTag/tag-key 条件键,可以将在请求中传递的标签键值对与在 IAM 策略中指定的标签对进行比较。例如,您可以支持特定操作,例如 CreateTable,如果标签条件不匹配,则使用 aws:RequestTag。为此,请执行以下步骤:

Using the AWS CLI
  1. 创建内联策略并将其添加到附加了 AmazonDynamoDBReadOnlyAccess AWS 托管式策略的角色,如以下示例所示。

    { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "dynamodb:CreateTable", "dynamodb:TagResource" ], "Resource": "arn:aws:dynamodb:*:*:table/*", "Condition": { "StringEquals": { "aws:RequestTag/Owner": "John" } } } ] }
  2. 创建包含标签键值对 "Owner": "John" 的表。

    aws dynamodb create-table \ --attribute-definitions AttributeName=ID,AttributeType=S \ --key-schema AttributeName=ID,KeyType=HASH \ --provisioned-throughput ReadCapacityUnits=1000,WriteCapacityUnits=500 \ --region us-east-1 \ --tags Key=Owner,Value=John \ --table-name myMusicTable
Using the AWS SDK for Python (Boto3)
  1. 创建内联策略并将其添加到附加了 AmazonDynamoDBReadOnlyAccess AWS 托管式策略的角色,如以下示例所示。

    { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "dynamodb:CreateTable", "dynamodb:TagResource" ], "Resource": "arn:aws:dynamodb:*:*:table/*", "Condition": { "StringEquals": { "aws:RequestTag/Owner": "John" } } } ] }
  2. 创建包含标签键值对 "Owner": "John" 的表。

    ddb_client = boto3.client('dynamodb') create_table_response = ddb_client.create_table( AttributeDefinitions=[ { 'AttributeName': 'id', 'AttributeType': 'S' }, ], TableName='myMusicTable', KeySchema=[ { 'AttributeName': 'id', 'KeyType': 'HASH' }, ], ProvisionedThroughput={ 'ReadCapacityUnits': 1000, 'WriteCapacityUnits': 500 }, Tags=[ { 'Key': 'Owner', 'Value': 'John' }, ], )
  1. 创建内联策略并将其添加到附加了 AmazonDynamoDBReadOnlyAccess AWS 托管式策略的角色,如以下示例所示。

    { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "dynamodb:CreateTable", "dynamodb:TagResource" ], "Resource": "arn:aws:dynamodb:*:*:table/*", "Condition": { "StringEquals": { "aws:RequestTag/Owner": "John" } } } ] }
  2. 创建包含标签键值对 "Owner": "John" 的表。

    aws dynamodb create-table \ --attribute-definitions AttributeName=ID,AttributeType=S \ --key-schema AttributeName=ID,KeyType=HASH \ --provisioned-throughput ReadCapacityUnits=1000,WriteCapacityUnits=500 \ --region us-east-1 \ --tags Key=Owner,Value=John \ --table-name myMusicTable
不带 ABAC

如果您未为 AWS 账户启用 ABAC,则内联策略和 DynamoDB 表中的标签条件将不匹配。因此,CreateTable 请求将失败,而不会创建您的表。

An error occurred (AccessDeniedException) when calling the CreateTable operation: User: arn:aws:sts::123456789012:assumed-role/Admin/John is not authorized to perform: dynamodb:CreateTable on resource: arn:aws:dynamodb:us-east-1:123456789012:table/myMusicTable because no identity-based policy allows the dynamodb:CreateTable action.
带有 ABAC

如果您为 AWS 账户启用了 ABAC,则表创建请求将成功完成。由于 CreateTable 请求中存在标签键值对 "Owner": "John",因此内联策略支持用户 John 执行 CreateTable 操作。

示例 3:使用 aws:TagKeys 拒绝操作

使用 aws:TagKeys 条件键,可以将请求中的标签键与在 IAM 策略中指定的键进行比较。例如,您可以拒绝特定的操作,例如 CreateTable,如果请求中 存在特定的标签键,则使用 aws:TagKeys。为此,请执行以下步骤:

Using the AWS CLI
  1. 客户管理型策略添加到附加了 AmazonDynamoDBFullAccess AWS 托管式策略的角色中,如以下示例所示。

    { "Version": "2012-10-17", "Statement": [ { "Effect": "Deny", "Action": [ "dynamodb:CreateTable", "dynamodb:TagResource" ], "Resource": "arn:aws:dynamodb:*:*:table/*", "Condition": { "Null": { "aws:TagKeys": "false" }, "ForAllValues:StringNotEquals": { "aws:TagKeys": "CostCenter" } } } ] }
  2. 代入策略所附加到的角色,然后使用标签键 Title 创建表。

    aws dynamodb create-table \ --attribute-definitions AttributeName=ID,AttributeType=S \ --key-schema AttributeName=ID,KeyType=HASH \ --provisioned-throughput ReadCapacityUnits=1000,WriteCapacityUnits=500 \ --region us-east-1 \ --tags Key=Title,Value=ProductManager \ --table-name myMusicTable
Using the AWS SDK for Python (Boto3)
  1. 客户管理型策略添加到附加了 AmazonDynamoDBFullAccess AWS 托管式策略的角色中,如以下示例所示。

    { "Version": "2012-10-17", "Statement": [ { "Effect": "Deny", "Action": [ "dynamodb:CreateTable", "dynamodb:TagResource" ], "Resource": "arn:aws:dynamodb:*:*:table/*", "Condition": { "Null": { "aws:TagKeys": "false" }, "ForAllValues:StringNotEquals": { "aws:TagKeys": "CostCenter" } } } ] }
  2. 代入策略所附加到的角色,然后使用标签键 Title 创建表。

    ddb_client = boto3.client('dynamodb') create_table_response = ddb_client.create_table( AttributeDefinitions=[ { 'AttributeName': 'id', 'AttributeType': 'S' }, ], TableName='myMusicTable', KeySchema=[ { 'AttributeName': 'id', 'KeyType': 'HASH' }, ], ProvisionedThroughput={ 'ReadCapacityUnits': 1000, 'WriteCapacityUnits': 500 }, Tags=[ { 'Key': 'Title', 'Value': 'ProductManager' }, ], )
  1. 客户管理型策略添加到附加了 AmazonDynamoDBFullAccess AWS 托管式策略的角色中,如以下示例所示。

    { "Version": "2012-10-17", "Statement": [ { "Effect": "Deny", "Action": [ "dynamodb:CreateTable", "dynamodb:TagResource" ], "Resource": "arn:aws:dynamodb:*:*:table/*", "Condition": { "Null": { "aws:TagKeys": "false" }, "ForAllValues:StringNotEquals": { "aws:TagKeys": "CostCenter" } } } ] }
  2. 代入策略所附加到的角色,然后使用标签键 Title 创建表。

    aws dynamodb create-table \ --attribute-definitions AttributeName=ID,AttributeType=S \ --key-schema AttributeName=ID,KeyType=HASH \ --provisioned-throughput ReadCapacityUnits=1000,WriteCapacityUnits=500 \ --region us-east-1 \ --tags Key=Title,Value=ProductManager \ --table-name myMusicTable
不带 ABAC

如果您未为 AWS 账户启用 ABAC,则 DynamoDB 不会在 create-table 命令中将标签键发送到 IAM。Null 条件可确保在请求中没有标签键时,条件的计算结果为 false。由于 Deny 策略不匹配,因此 create-table 命令成功完成。

带有 ABAC

如果您为 AWS 账户启用了 ABAC,则在 create-table 命令中传递的标签键将传递给 IAM。标签键 Title 是根据 Deny 策略中存在的基于条件的标签键 CostCenter 进行评估的。由于 StringNotEquals 运算符的原因,标签键 TitleDeny 策略中存在的标签键不匹配。因此,CreateTable 操作将失败,而不会创建表。运行 create-table 命令会返回 AccessDeniedException

An error occurred (AccessDeniedException) when calling the CreateTable operation: User: arn:aws:sts::123456789012:assumed-role/DynamoFullAccessRole/ProductManager is not authorized to perform: dynamodb:CreateTable on resource: arn:aws:dynamodb:us-east-1:123456789012:table/myMusicTable with an explicit deny in an identity-based policy.

下一主题:

故障排除

上一主题:

使用 ABAC
隐私网站条款Cookie 首选项
© 2025, Amazon Web Services, Inc. 或其附属公司。保留所有权利。