

# Control access with tags

You can use tags in the `Condition` element of an AWS Identity and Access Management (IAM) policy to control access to AWS Context resources. Use the following condition keys.


| Condition key | Use it to | 
| --- | --- | 
|  `aws:ResourceTag/${TagKey}`  | Control access based on the tags on a resource. | 
|  `aws:RequestTag/${TagKey}`  | Control which tags a request can add. | 
|  `aws:TagKeys`  | Control which tag keys a request can include. | 

Changes to tags can take several minutes to affect authorization. This delay applies to every AWS Context resource, and to tasks and evaluations through their Agent Context.

For more information about using tags to control access, and for patterns such as requiring tags, see [Controlling access to AWS resources using tags](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_tags.html) in the *IAM User Guide* and [Implementing and enforcing tagging](https://docs.aws.amazon.com/whitepapers/latest/tagging-best-practices/implementing-and-enforcing-tagging.html) in the *Best Practices for Tagging AWS Resources* whitepaper.

## Permissions to create tagged resources
<a name="tagging-access-on-create"></a>

To add tags when you create a resource, you need permission for the create action and for `context:TagResource` on the new resource.

 [`StartOntologyInduction`](https://docs.aws.amazon.com/context/latest/APIReference/API_StartOntologyInduction.html) also creates an ontology, so it always requires permission for `context:CreateOntology`. To control the tags in `ontologyTags`, put your conditions on `context:CreateOntology`.

## Access to tasks, evaluations, and Agent Context data
<a name="tagging-access-tasks-evaluations"></a>

When a policy uses the `aws:ResourceTag` condition key on a task or an evaluation, AWS Context uses the tags on its Agent Context. AWS Context also authorizes actions on an Agent Context’s changesets and curation proposals, and retrieval of its data, against the Agent Context’s current tags. To change who can access them, change the Agent Context’s tags.

For the resource and condition keys of each action, see [Actions, resources, and condition keys for AWS services](https://docs.aws.amazon.com/service-authorization/latest/reference/reference_policies_actions-resources-contextkeys.html) in the *Service Authorization Reference*.

## Example policies
<a name="tagging-access-control-examples"></a>

The following examples show only the tag-related permissions. A working request also needs any other permissions that its action requires. In these examples, replace `us-east-1` and `111122223333` with your Region and account ID.

**Example: Allow a team to work with its Agent Contexts**  
The following policy applies to Agent Contexts that have the tag `team` with the value `analytics`. It allows a principal to view, update, and delete those Agent Contexts.

```json
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "context:GetAgentContext",
                "context:UpdateAgentContext",
                "context:DeleteAgentContext"
            ],
            "Resource": "arn:aws:context:us-east-1:111122223333:agent-context/*",
            "Condition": {
                "StringEquals": {
                    "aws:ResourceTag/team": "analytics"
                }
            }
        }
    ]
}
```

**Example: Allow a team to create Agent Contexts tagged for the team**  
The following policy allows a principal to create Agent Contexts only when the request adds the tag `team` with the value `analytics`. It also allows the principal to add tags to Agent Contexts that already have that tag, when the request includes that tag.

```json
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "context:CreateAgentContext",
                "context:TagResource"
            ],
            "Resource": "arn:aws:context:us-east-1:111122223333:agent-context/*",
            "Condition": {
                "StringEquals": {
                    "aws:RequestTag/team": "analytics",
                    "aws:ResourceTag/team": "analytics"
                }
            }
        }
    ]
}
```