Single-valued context key policy examples - Amazon Identity and Access Management
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Single-valued context key policy examples

The following set of policy examples demonstrate how to create policy conditions with single-valued context keys.

Example: Multiple condition blocks with single-valued context keys

When a condition block has multiple conditions, each with a single context key, all context keys must resolve to true for the desired Allow or Deny effect to be invoked. When you use negated matching condition operators, the evaluation logic of the condition value is reversed.

The following example lets users create EC2 volumes and apply tags to the volumes during volume creation. The request context must include a value for context key aws:RequestTag/project, and the value for context key aws:ResourceTag/environment can be anything except production.

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "ec2:CreateVolume", "Resource": "*" }, { "Effect": "Allow", "Action": "ec2:CreateTags", "Resource": "arn:aws:ec2:::volume/*", "Condition": { "StringLike": { "aws:RequestTag/project": "*" } } }, { "Effect": "Allow", "Action": "ec2:CreateTags", "Resource": "arn:aws:ec2:region:account:*/*", "Condition": { "StringNotEquals": { "aws:ResourceTag/environment": "production" } } } ] }

The request context must include a project tag-value and cannot be created for a production resource to invoke the Allow effect. The following EC2 volume is successfully created because the project name is Feature3 with a QA resource tag.

aws ec2 create-volume \ --availability-zone us-east-1a \ --volume-type gp2 \ --size 80 \ --tag-specifications 'ResourceType=volume,Tags=[{Key=project,Value=Feature3},{Key=environment,Value=QA}]'

Example: One condition block with multiple single-valued context keys and values

When a condition block contains multiple context keys and each context key has multiple values, each context key must resolve to true for at least one key value for the desired Allow or Deny effect to be invoked. When you use negated matching condition operators, the evaluation logic of the context key value is reversed.

The following example allows users to start and run tasks on Amazon Elastic Container Service clusters.

  • The request context must include production OR pre-prod for the aws:RequestTag/environment context key AND.

  • The ecs:cluster context key makes sure that tasks are run on either the default1 OR default2 ARN ECS clusters.

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "ecs:RunTask", "ecs:StartTask" ], "Resource": [ "*" ], "Condition": { "StringEquals": { "aws:RequestTag/environment": [ "production", "prod-backup" ] }, "ArnEquals": { "ecs:cluster": [ "arn:aws-cn:ecs:us-east-1:111122223333:cluster/default1", "arn:aws-cn:ecs:us-east-1:111122223333:cluster/default2" ] } } } ] }