> ## Documentation Index
> Fetch the complete documentation index at: https://docs.formal.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Give AgentCore Agents Database Access

> Let an Amazon Bedrock AgentCore Harness query a Connector-protected database using its AWS identity and a skill

## Overview

An [Amazon Bedrock AgentCore Harness](https://github.com/aws/agentcore-cli)
runs each agent session in its own Firecracker microVM, where you usually
can't install the Formal Endpoint. Putting a database password in the
environment instead would give every session standing access.

This guide has the agent exchange its Harness execution role for a
short-lived Formal token and log in to the [Connector](https://docs.formal.ai/docs/glossary/index.md#connector)
with it. The Connector swaps in the real
[native user](https://docs.formal.ai/docs/glossary/index.md#native-user) credentials and applies your
[policies](https://docs.formal.ai/docs/glossary/index.md#policy), so the agent never sees a database password.
A Harness skill walks the agent through the exchange, and the only setup in
the VM is a Python script that comes with the skill.

```mermaid theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
sequenceDiagram
  participant Agent as AgentCore Harness
  participant STS as AWS STS
  participant Formal as Formal Control Plane
  participant Connector as Formal Connector
  participant DB as Database

  Agent->>STS: GetWebIdentityToken with the execution role
  STS-->>Agent: AWS-signed JWT for Formal's audience
  Agent->>Formal: CreateFederationToken with the JWT
  Formal-->>Agent: Short-lived username and token
  Agent->>Connector: Postgres login with the federation token
  Connector->>DB: Query with native user credentials
  DB-->>Connector: Results
  Connector-->>Agent: Results with your policies applied
```

## Prerequisites

* A Postgres [resource](https://docs.formal.ai/docs/glossary/index.md#resource) behind a Formal Connector
  listener that the Harness can reach
* A native user on the resource with the database credentials, set as the
  default for the resource
* A working [AgentCore Harness](https://github.com/aws/agentcore-cli) and
  permission to edit its execution role

## Turn on outbound identity federation

The agent proves its identity to Formal with a JWT that AWS signs for the
Harness execution role. Turn this on once per AWS account:

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
aws iam enable-outbound-web-identity-federation
aws iam get-outbound-web-identity-federation-info
```

The second command prints your account's issuer URL, for example
`https://<ID>.tokens.sts.global.api.aws`. Keep it for the next step.

## Connect the execution role to Formal

Create a Formal machine user and an OIDC integration that maps the execution
role to it, then allow the role to request tokens for that integration. The role doesn't need any
database permissions.

> [tab] Control Plane
>
> **1. Create a machine user**
>
> Go to [Users](https://app.formal.ai/users) and create a machine user,
> for example `agentcore-harness`. The agent's sessions are logged and
> checked against policies as this user.
>
> **2. Create the OIDC integration**
>
> Go to [OIDC Integrations](https://app.formal.ai/oidc-integrations) and
> click **Create Integration**. Set **Issuer** to your account's issuer
> URL. Select the machine user, and restrict the claim condition to the
> execution role:
>
> ```cel theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
> claims.sub == "arn:aws:iam::<ACCOUNT_ID>:role/<EXECUTION_ROLE>"
> ```
>
> Set the integration to **Active**. Copy the audience,
> `oidc.formal.ai/integrationoidc_...`.
>
> **3. Allow the role to request tokens**
>
> Let the role request tokens for the integration's audience only, so
> it can't mint AWS-signed tokens for other services that trust your
> account:
>
> ```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
> aws iam put-role-policy --role-name <EXECUTION_ROLE> \
>   --policy-name formal --policy-document '{
>     "Version": "2012-10-17",
>     "Statement": [{
>       "Effect": "Allow",
>       "Action": "sts:GetWebIdentityToken",
>       "Resource": "*",
>       "Condition": {
>         "ForAllValues:StringEquals": {"sts:IdentityTokenAudience": ["oidc.formal.ai/<INTEGRATION_ID>"]},
>         "Null": {"sts:IdentityTokenAudience": "false"}
>       }
>     }]
>   }'
> ```
>
> **4. Restrict the machine user to federation tokens**
>
> By default, the agent's OIDC JWT works with any Control Plane API, but
> the agent only needs `CreateFederationToken`. Go to
> [Permissions](https://app.formal.ai/permissions), click
> **Create Permission**, name it `agentcore-harness`, and paste:
>
> ```rego theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
> package formal.app
>
> import future.keywords.if
>
> default allow = true
>
> allow = false if {
>   input.user.id == "<MACHINE_USER_ID>"
>   input.app.command.name != "CreateFederationToken"
> }
> ```
>
> Set the Permission to **Active**.

> [tab] Terraform
>
> Add the
> [example module](https://github.com/formalco/example-use-cases/tree/main/agentcore-harness-database-access/terraform)
> to a root module with the
> [AWS](https://registry.terraform.io/providers/hashicorp/aws) and
> [Formal](https://registry.terraform.io/providers/formalco/formal)
> providers configured:
>
> ```hcl theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
> module "agentcore_harness" {
>   source = "git::https://github.com/formalco/example-use-cases.git//agentcore-harness-database-access/terraform?ref=<COMMIT_SHA>"
>
>   execution_role_name = "<EXECUTION_ROLE>"
>   oidc_issuer         = "https://<ID>.tokens.sts.global.api.aws"
> }
>
> output "formal_oidc_audience" {
>   value = module.agentcore_harness.formal_oidc_audience
> }
> ```
>
> The `formal_oidc_audience` output is the audience for the next step.

## Add the skill to your Harness

The `formal-connector` skill is in
[formalco/example-use-cases](https://github.com/formalco/example-use-cases/tree/main/agentcore-harness-database-access/skill).
Add it to your Harness's `harness.yaml` as a Git source, and point it at your
Connector:

```yaml theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
skills:
  - git:
      url: https://github.com/formalco/example-use-cases.git
      path: agentcore-harness-database-access/skill
environmentVariables:
  FORMAL_CONNECTOR_HOST: <CONNECTOR_HOST>
  FORMAL_CONNECTOR_PORT: "<CONNECTOR_PORT>"
  FORMAL_OIDC_AUDIENCE: oidc.formal.ai/<INTEGRATION_ID>
```

You can also set:

* `FORMAL_DATABASE`: the database to connect to. Defaults to `postgres`.
* `FORMAL_NATIVE_USER`: the label of a native user to request instead of the
  resource's default.
* `FORMAL_SSLMODE`: `verify-full` (default), `require`, or `disable`.

In the system prompt, tell the agent to use the skill for database access:

```text theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
You have no database credentials of your own. All database access must go
through the Formal Connector using the formal-connector skill.
```

Deploy:

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
agentcore deploy
```

> [!NOTE]
>
> Git skill sources always load the default branch. To pin the skill to a
> version you've reviewed, fork the repository and point `url` at your fork.

### How the skill works

The skill runs
[`scripts/formal_pg.py`](https://github.com/formalco/example-use-cases/blob/main/agentcore-harness-database-access/skill/scripts/formal_pg.py).
On the first run in a session, the script installs its hash-pinned
dependencies into a virtualenv under `/tmp`, which takes about ten seconds.
It then calls STS `GetWebIdentityToken` with the execution role's credentials
and your integration's audience, and exchanges the resulting JWT for a
federation token at `CreateFederationToken`. Finally, it logs in to the
Connector with the federation `username` and `token` and runs the query.

## Verify the setup

Invoke the Harness:

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
agentcore invoke --harness <HARNESS_NAME> \
  --prompt "Use the formal-connector skill to run SELECT current_user."
```

The script prints the identity it used to stderr:

```text theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
# aws sub=arn:aws:iam::<ACCOUNT_ID>:role/<EXECUTION_ROLE> -> formal user=idp:formal:machine:agentcore-harness expires_at=...
```

The query should return the native user's database role. In
[Logs](https://app.formal.ai/logs), the session shows up under the
`agentcore-harness` machine user, with the AWS claims under
`user.oidc.claims`.

To check that the agent can't go around Formal, connect to the listener as
the native database user with a wrong password. The database should reject
the login.

## Restrict network access

The execution role lets the agent mint tokens for Formal, and the
skill isn't a security boundary. To keep a prompt-injected agent from sending
its tokens elsewhere, run the Harness in VPC mode and configure an egress policy.

## Troubleshooting

> [accordion] STS GetWebIdentityToken returns AccessDenied
>
> The execution role lacks `sts:GetWebIdentityToken`, the audience in
> `FORMAL_OIDC_AUDIENCE` doesn't match the one in the role's policy, or
> outbound identity federation is off. Run `aws iam get-outbound-web-identity-federation-info`
> and check that `JwtVendingEnabled` is `true`.

> [accordion] TLS errors connecting to the Connector
>
> By default, the script encrypts the connection and checks that the
> Connector's certificate is valid for `FORMAL_CONNECTOR_HOST`.
>
> * **`CERTIFICATE_VERIFY_FAILED`:** the listener's certificate isn't valid
>   for that hostname, for example because it's self-signed or issued for a
>   different name. Give the listener a certificate for the hostname the
>   Harness connects to.
> * **`Server refuses SSL`:** the listener doesn't have TLS configured.
>   Configure TLS on the listener.
>
> You can set `FORMAL_SSLMODE` to `require` to encrypt without checking
> the certificate, or to `disable` to turn off TLS entirely.

> [accordion] pip install fails on the first run
>
> The Harness cannot reach the package index. In VPC mode, allow egress to
> `pypi.org` and `files.pythonhosted.org`, or set `PIP_INDEX_URL` to your
> mirror in `environmentVariables`.

> [accordion] CreateFederationToken returns 401
>
> Confirm the integration is **Active** and its issuer is your account's
> issuer URL. Confirm the claim condition matches the execution role ARN,
> not the assumed-role session ARN.

> [accordion] The Connector returns Native user incorrect
>
> `FORMAL_NATIVE_USER` is set to a native user label that doesn't exist on
> the resource. Fix the label, or unset it to use the resource's default
> native user.

## Next steps

> [card] [OIDC Integrations](https://docs.formal.ai/docs/guides/integrations/oidc.md)
>
> Configure claims and federated access

> [card] [Native Users](https://docs.formal.ai/docs/guides/core-concepts/resources/native-users.md)
>
> Store the database credentials in Formal

> [card] [Devin Database Access](https://docs.formal.ai/docs/guides/example-use-cases/devin-database-access-oidc.md)
>
> Add Slack approval before each session

> [card] [ECS Egress](https://docs.formal.ai/docs/guides/client-apps/ecs-egress.md)
>
> Run the Endpoint where you can install it


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.