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

# SSH tunnels

> Connect a private database through a bastion with verified host identity.

Struct can reach PostgreSQL and PlanetScale Postgres through an SSH bastion, including an EC2 Instance Connect Endpoint. PlanetScale Vitess does not support this tunnel.

## Connect from the CLI

With the [Struct CLI](https://docs.struct.ai/reference/cli.md), first read the bastion's host key fingerprint. Struct reads it without logging in:

```bash theme={null}
struct connections ssh-host-key --ssh-host bastion.example.com
```

Compare the fingerprint with the bastion's own key (see [Host key check](#host-key-check)), then connect with it in `--trust-host-key`, the SSH password in `SSH_PASSWORD` and the database password in `PGPASSWORD` (or `PLANETSCALE_PASSWORD`):

```bash theme={null}
struct connect postgres --host db.internal.example.com \
  --database app --user struct_reader \
  --ssh-host bastion.example.com --ssh-user jump --trust-host-key SHA256:...
```

| Setup | Flags |
| - | - |
| Public bastion | `--ssh-host`, `--ssh-port` (default 22), `--ssh-user` |
| Bastion behind an EC2 Instance Connect Endpoint | `--ssh-endpoint-id`, `--ssh-instance-id`, `--ssh-aws-connection <AWS connection id>`, `--ssh-region` (defaults to the AWS connection's region), `--ssh-user`. Pass these flags except `--ssh-user` to `struct connections ssh-host-key` instead of `--ssh-host`. |
| AWS EC2 Instance Connect login | `--ssh-auth ec2_instance_connect` and `--ssh-instance-id`, with no `SSH_PASSWORD` |

Without `--trust-host-key`, or if the bastion presents a different key, Struct stops before sending any credential to the bastion, saves nothing, and exits with code `3` (`ssh_host_key_needed` or `ssh_host_key_changed`). The message, and the `sshHostKeyFingerprint` field with `--json`, give the fingerprint Struct read: check it, then run the command again with it.

To move a saved connection to another bastion, use `struct connections rotate <id>` with the new SSH flags, the passwords again and `--trust-host-key`. `--ssh-via none` removes the tunnel.

## Host key check

Compare the returned fingerprint with the bastion's own key through a trusted administrative connection:

```bash theme={null}
ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub
```

Do not trust a fingerprint solely because the same network connection returned it. A changed bastion or key requires a new independent check.

## Network and permissions

The bastion must allow TCP forwarding and reach the database. For an EC2 Instance Connect Endpoint, allow SSH from the endpoint security group and keep Preserve Client IP off. Endpoint grants are scoped to the endpoint and port; use provider network policy to restrict which private targets are reachable. Use the connection's grant instructions for the intended instance and user.

Database TLS still uses the database hostname and configured SSL mode through the tunnel. Supply database and SSH secrets through their documented environment variables, never checked-in files or literal command arguments.

For EC2 Instance Connect, the AWS role Struct assumes needs ec2-instance-connect:SendSSHPublicKey on the bastion instance, plus ec2-instance-connect:OpenTunnel on the endpoint when it sits behind an EC2 Instance Connect Endpoint.

### EC2 Instance Connect

Push a one-time SSH key to the bastion and open a tunnel through an EC2 Instance Connect Endpoint, so no SSH key is stored.

| Permission | Used for |
| - | - |
| `ec2-instance-connect:SendSSHPublicKey` | Pushing a temporary SSH key to the bastion instance, limited to its OS user with the ec2:osuser condition |
| `ec2-instance-connect:OpenTunnel` | On the endpoint, limited to the SSH port and tunnels of at most one hour |
| `ec2:DescribeInstanceConnectEndpoints` | Finding the endpoint (on `*`) |
| `ec2:DescribeInstances` | Finding the bastion's private address (on `*`) |

* Struct shows a policy scoped to your instance, OS user, endpoint and port when you set up the tunnel.

Permission catalog version `2026-11-04.1`. See [permission changes](https://docs.struct.ai/reference/permission-changes.md).


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