

# Grant cross-account access to an Agent Context

To grant a principal in another AWS account access to your Agent Context, attach a resource-based policy to the Agent Context. To source data from another account into an Agent Context, see [Cross-account access](cross-account-access.md).

Only the Agent Context owner can call `PutResourcePolicy`, `GetResourcePolicy`, and `DeleteResourcePolicy`. You can’t grant these three operations to another account through the resource-based policy.

With the following example policy, a role in account `444455556666` can query the Agent Context `ac-EXAMPLE11111` in account `111122223333`:

```json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "AllowCrossAccountRetrieval",
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::{{444455556666}}:role/{{ContextConsumerRole}}"
      },
      "Action": [
        "context:SearchAgentContext",
        "context:TraverseAgentContext",
        "context:GetAgentContextData",
        "context:GetAgentContextStatistics",
        "context:AgenticRetrieve",
        "context:AgenticRetrieveStream",
        "context:ExecuteSql"
      ],
      "Resource": "arn:aws:context:{{us-east-1}}:{{111122223333}}:agent-context/{{ac-EXAMPLE11111}}"
    }
  ]
}
```

Replace the following values with your own:
+  `444455556666` – The ID of the caller’s account.
+  `ContextConsumerRole` – The name of the caller’s role.
+  `111122223333` – The ID of the account that owns the Agent Context.
+  `us-east-1` – The Region of the Agent Context.
+  `ac-EXAMPLE11111` – The ID of the Agent Context.

Follow these rules when you write the policy:
+ Grant only the actions the caller needs. For example, omit `context:ExecuteSql` if the caller doesn’t run SQL queries.
+  `ExecuteSql` works only on Agent Contexts whose tables are in the Agent Context account (see [When to use cross-account access](cross-account-when.md)).
+ Set the `Resource` of every statement to the ARN of the Agent Context that you attach the policy to.
+ You can’t use `NotAction` or `NotResource`.

Apply the policy with `PutResourcePolicy`, which replaces the entire policy. To change a policy, get it with `GetResourcePolicy`, merge your changes, and pass its `revisionId` as `expectedRevisionId`. If someone changes the policy after you read it, the call fails with a `ConflictException`. In that case, repeat these steps. The following commands get and update the policy:

```bash
aws context get-resource-policy \
    --resource-arn arn:aws:context:{{us-east-1}}:{{111122223333}}:agent-context/{{ac-EXAMPLE11111}}

aws context put-resource-policy \
    --resource-arn arn:aws:context:{{us-east-1}}:{{111122223333}}:agent-context/{{ac-EXAMPLE11111}} \
    --policy file://{{agent-context-policy.json}} \
    --expected-revision-id {{revision-id}}
```

The caller’s role also needs an identity-based policy in its own account that allows the same actions on the Agent Context:

```json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "QueryAgentContextInAnotherAccount",
      "Effect": "Allow",
      "Action": [
        "context:SearchAgentContext",
        "context:TraverseAgentContext",
        "context:GetAgentContextData",
        "context:GetAgentContextStatistics",
        "context:AgenticRetrieve",
        "context:AgenticRetrieveStream",
        "context:ExecuteSql"
      ],
      "Resource": "arn:aws:context:{{us-east-1}}:{{111122223333}}:agent-context/{{ac-EXAMPLE11111}}"
    }
  ]
}
```

To revoke access, remove the statement from the policy, or delete the policy with `DeleteResourcePolicy`. Changes to the policy, including revocations, take effect shortly after you make them.