以下示例描述了一些使用标签来实现基于属性的条件的用例。
示例 1:使用 aws:ResourceTag 支持操作
使用 aws:ResourceTag/tag-key
条件键,可以将在 IAM 策略中指定的标签键值对与 DynamoDB 表中附加的键值对进行比较。例如,如果 IAM 策略和表中的标签条件匹配,则可以支持执行特定操作,例如 PutItem。为此,请执行以下步骤:
-
创建表。以下示例使用 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
-
向此表添加标签。以下 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 -
创建内联策略并将其添加到附加了 AmazonDynamoDBReadOnlyAccess AWS 托管式策略的角色,如以下示例所示。
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "dynamodb:PutItem", "Resource": "arn:aws:dynamodb:*:*:table/*", "Condition": { "StringEquals": { "aws:ResourceTag/Title": "ProductManager" } } } ] }
当附加到表的标签键和值与在策略中指定的标签匹配时,此策略支持对表执行
PutItem
操作。 -
使用在步骤 3 中描述的策略代入该角色。
-
使用 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"} }} }'
-
扫描该表,以验证该项目是否已添加到表。
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
。为此,请执行以下步骤:
-
创建内联策略并将其添加到附加了 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" } } } ] }
-
创建包含标签键值对
"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
。为此,请执行以下步骤:
-
将客户管理型策略添加到附加了 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" } } } ] }
-
代入策略所附加到的角色,然后使用标签键
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
运算符的原因,标签键 Title
与 Deny
策略中存在的标签键不匹配。因此,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.