{"token_count": 5454}

# Database Access with Amazon Keyspaces (Apache Cassandra)

Teleport can provide secure access to Amazon Keyspaces (Apache Cassandra) via the [Teleport Database Service](https://goteleport.com/docs/enroll-resources/database-access.md). This allows for fine-grained access control through [Teleport's RBAC](https://goteleport.com/docs/enroll-resources/database-access/rbac.md).

In this guide, you will:

1. Configure your Amazon Keyspaces (Apache Cassandra) database with IAM authentication.
2. Add the database to your Teleport cluster.
3. Connect to the database via Teleport.

## How it works

The Teleport Database Service uses IAM authentication to communicate with Amazon Keyspaces. When a user connects to the database via Teleport, the Teleport Database Service obtains AWS credentials and authenticates to AWS as an IAM principal with permissions to access the database.

**Self-Hosted**

![Enroll Amazon Keyspaces with a Self-Hosted Teleport Cluster](/docs/assets/images/cassandra_keyspaces_selfhosted-027a61468dd9b048bc1c84e3d536feb8.png)

**Teleport Enterprise Cloud**

![Enroll Amazon Keyspaces with a Cloud-Hosted Teleport Cluster](/docs/assets/images/cassandra_keyspaces_cloud-4800da9e351aad28c3cfa3087ee1ee6c.png)

## Prerequisites

- A running Teleport cluster accessible at a hostname with a valid TLS certificate. If you want to get started with Teleport, [sign up](https://goteleport.com/signup) for a free trial or [set up a demo environment](https://goteleport.com/docs/get-started/deploy-community.md).

- The `tctl` and `tsh` clients.

  Installing `tctl` and `tsh` clients

  1. Determine the version of your Teleport cluster. The `tctl` and `tsh` clients must be at most one major version behind your Teleport cluster version. Send a GET request to the Proxy Service at `/v1/webapi/find` and use a JSON query tool to obtain your cluster version. Replace teleport.example.com:443 with the web address of your Teleport Proxy Service:

     **Mac/Linux**

     ```
     $ TELEPORT_DOMAIN=teleport.example.com:443
     $ TELEPORT_VERSION="$(curl -s https://$TELEPORT_DOMAIN/v1/webapi/find | jq -r '.server_version')"
     ```

     **Windows - Powershell**

     ```
     $ $TELEPORT_DOMAIN = "teleport.example.com:443"
     $ $TELEPORT_VERSION = (Invoke-RestMethod -Uri "https://${TELEPORT_DOMAIN}/v1/webapi/find").server_version
     ```

  2. Follow the instructions for your platform to install `tctl` and `tsh` clients:

     **Mac**

     Download the signed macOS .pkg installer for Teleport, which includes the `tctl` and `tsh` clients:

     ```
     $ curl -O https://cdn.teleport.dev/teleport-${TELEPORT_VERSION?}.pkg
     ```

     In Finder double-click the `pkg` file to begin installation.

     ---

     DANGER

     Using Homebrew to install Teleport is not supported. The Teleport package in Homebrew is not maintained by Teleport and we can't guarantee its reliability or security.

     ---

     **Windows - Powershell**

     ```
     $ curl.exe -O https://cdn.teleport.dev/teleport-v$TELEPORT_VERSION-windows-amd64-bin.zip
     Unzip the archive and move the `tctl` and `tsh` clients to your %PATH%
     NOTE: Do not place the `tctl` and `tsh` clients in the System32 directory, as this can cause issues when using WinSCP.
     Use %SystemRoot% (C:\Windows) or %USERPROFILE% (C:\Users\<username>) instead.
     ```

     **Linux**

     All of the Teleport binaries in Linux installations include the `tctl` and `tsh` clients. For more options (including RPM/DEB packages and downloads for i386/ARM/ARM64) see our [installation page](https://goteleport.com/docs/installation/single-machine.md).

     ```
     $ curl -O https://cdn.teleport.dev/teleport-v${TELEPORT_VERSION?}-linux-amd64-bin.tar.gz
     $ tar -xzf teleport-v${TELEPORT_VERSION?}-linux-amd64-bin.tar.gz
     $ cd teleport
     $ sudo ./install
     Teleport binaries have been copied to /usr/local/bin
     ```

  Connecting with TLS routing disabled

  This guide's commands assume your Teleport cluster uses TLS routing (`proxy_listener_mode: multiplex`), where the `tctl` and `tsh` clients reach every Teleport service through the Proxy Service's web address on port `443`. If you're not sure whether this applies to your cluster, check with whoever manages it.

  If your cluster uses separate listener ports instead, adjust ports as follows:

  - **`tsh` commands** (e.g., `tsh login --proxy=...`): continue using the Proxy Service web address on port `3080` (or `443` if behind a load balancer). Do not change these to port `3025`.

  - **Direct `tctl` or Auth Service API commands**: use port `3025` for the Auth Service gRPC listener:

    ```
    $ tctl status --auth-server=teleport.example.com:3025
    ```

* AWS Account with Amazon Keyspaces database and permissions to create and attach IAM policies

* The `cqlsh` Cassandra client installed and added to your system's `PATH` environment variable.

* A host, e.g., an Amazon EC2 instance, where you will run the Teleport Database Service.

* An IAM principal for the Teleport Database Service. We assume that this is either an IAM role called `TeleportDatabaseServiceRole` or an IAM user called `TeleportDatabaseServiceUser`.

* Check that you can connect to your Teleport cluster and verify that you can run `tctl` and `tsh` commands using your current credentials.

  1. Assign teleport.example.com to the domain name of the Teleport Proxy Service in your cluster and email\@example.com to your Teleport username.

  2. Authenticate to your Teleport cluster. This depends on whether your shell is interactive or not.

     **In an interactive shell:** Run the following command. By default, this triggers a multi-factor authentication prompt:

     ```
     $ tsh login --proxy=teleport.example.com --user=email@example.com
     $ tctl status
     Cluster  teleport.example.com
     Version  18.10.0
     CA pin   sha256:abdc1245efgh5678abdc1245efgh5678abdc1245efgh5678abdc1245efgh5678
     ```

     **On non-interactive environments:** If you are running `tsh` and `tctl` as an AI agent, in a CI/CD environment, or similar, make sure the `TELEPORT_IDENTITY_FILE` environment variable is assigned to a valid file path with credentials for your cluster. `tsh` and `tctl` read the file path from the environment variable and do not require a separate authentication step. If there is no identity file available, we recommend that you [set up Machine ID](https://goteleport.com/docs/machine-workload-identity/getting-started.md) to provision one automatically.

     When executing `tctl` commands with an identity file, you must pass the `--auth-server` flag to provide the Teleport Auth Service address, which is not included in the identity file. If you provide the Proxy Service address, `tctl` connects to the Proxy Service, which forwards traffic to and from the Teleport Auth Service. Update 443 to `3025` if you are contacting the Auth Service directly with `tctl`:

     ```
     $ tctl status --auth-server=teleport.example.com:443
     ```

     For `tsh` commands that read an identity file, you must pass the `--proxy` flag, which points `tsh` to the address of the Teleport Proxy Service:

     ```
     $ tsh status --proxy=teleport.example.com
     ```

     Ensure client commands can access your identity file. Replace path/to/identity/file with the path to your identity file:

     ```
     $ export TELEPORT_IDENTITY_FILE="${TELEPORT_IDENTITY_FILE:-path/to/identity/file}"
     ```

     Add the `--auth-server` or `--proxy` flags to all subsequent `tctl` and `tsh` commands.

  If you can connect to the cluster and run the `tctl status` command, you can use your current credentials to run subsequent `tctl` commands from your workstation. If you host your own Teleport cluster, you can also run `tctl` commands on the computer that hosts the Teleport Auth Service for full permissions.

## Step 1/5. Set up the Teleport Database Service

The Database Service requires a valid join token to join your Teleport cluster. Run the following `tctl` command and save the token output in `/tmp/token` on the server that will run the Database Service:

```
$ tctl tokens add --type=db --format=text
abcd123-insecure-do-not-use-this
```

Alternative methods

For users with a lot of infrastructure in AWS, or who might create or recreate many instances, consider alternative methods for joining new EC2 instances running Teleport:

- [Configure Teleport to Automatically Enroll EC2 instances](https://goteleport.com/docs/enroll-resources/auto-discovery/servers/ec2-discovery.md)
- [Joining Teleport Services via AWS IAM Role](https://goteleport.com/docs/installation/agents/aws-iam.md)
- [Joining Teleport Services via AWS EC2 Identity Document](https://goteleport.com/docs/installation/agents/aws-ec2.md)

To install Teleport binaries on your Linux server, the recommended installation method is the cluster install script. It will select the correct version, edition, and installation mode for your cluster.

1. Remove any existing Teleport binaries on your system:

   ```
   $ sudo rm -f /usr/local/bin/{tsh,teleport,tctl,tbot,fdpass-teleport,teleport-update}
   ```

2. Assign teleport.example.com:443 to your Teleport cluster hostname and port, but not the scheme (https\://).

3. Run your cluster's install script:

   ```
   $ curl "https://teleport.example.com:443/scripts/install.sh" | sudo bash
   ```

**Self-Hosted**

Create a configuration for the Teleport Database Service, pointing the `--proxy` flag to the address of your Teleport Proxy Service:

```
$ sudo teleport db configure create \
   -o file \
  --token=/tmp/token \
  --proxy=teleport.example.com:443 \
  --name=keyspaces \
  --protocol=cassandra \
  --aws-account-id=12345678912 \
  --aws-region=us-east-1 \
  --labels=env=dev
```

**Teleport Enterprise Cloud**

Create a configuration for the Teleport Database Service, pointing the `--proxy` flag to the address of your Teleport Proxy Service:

```
$ sudo teleport db configure create \
   -o file \
  --token=/tmp/token \
  --proxy=mytenant.teleport.sh:443 \
  --name=keyspaces \
  --protocol=cassandra \
  --aws-account-id=12345678912 \
  --aws-region=us-east-1 \
  --labels=env=dev
```

Grant the Teleport Database Service access to credentials that it can use to authenticate to AWS.

- If you are running the Teleport Database Service on an EC2 instance, you may use the EC2 Instance Metadata Service method
- If you are running the Teleport Database Service in Kubernetes, you can use IAM Roles for Service Accounts (IRSA)
- Otherwise, you must use environment variables

**Instance Metadata Service**

Teleport will detect when it is running on an EC2 instance and use the Instance Metadata Service to fetch credentials.

The EC2 instance should be configured to use an EC2 instance profile. For more information, see: [Using Instance Profiles](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-ec2_instance-profiles.html).

**Kubernetes IRSA**

Refer to [IAM Roles for Service Accounts (IRSA)](https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html) to set up an OIDC provider in AWS and configure an AWS IAM role that allows the pod's service account to assume the role.

**Environment Variables**

Teleport's built-in AWS client reads credentials from the following environment variables:

- `AWS_ACCESS_KEY_ID`
- `AWS_SECRET_ACCESS_KEY`
- `AWS_DEFAULT_REGION`

When you start the Teleport Database Service, the service reads environment variables from a file at the path `/etc/default/teleport`. Obtain these credentials from your organization. Ensure that `/etc/default/teleport` has the following content, replacing the values of each variable:

```
AWS_ACCESS_KEY_ID=00000000000000000000
AWS_SECRET_ACCESS_KEY=0000000000000000000000000000000000000000
AWS_DEFAULT_REGION=<YOUR_REGION>

```

Have multiple sources of AWS credentials?

Teleport's AWS client loads credentials from different sources in the following order:

- Environment Variables
- Shared credentials file
- Shared configuration file (Teleport always enables shared configuration)
- EC2 Instance Metadata (credentials only)

While you can provide AWS credentials via a shared credentials file or shared configuration file, you will need to run the Teleport Database Service with the `AWS_PROFILE` environment variable assigned to the name of your profile of choice.

If you have a specific use case that the instructions above do not account for, consult the documentation for the [AWS SDK for Go](https://docs.aws.amazon.com/sdk-for-go/api/aws/session/) for a detailed description of credential loading behavior.

Start the Teleport Database Service. The instructions depend on how you installed the Teleport Database Service and whether your system supports systemd:

**Package Manager**

Configure the Teleport Database Service to start automatically when the host boots up by creating a systemd service for it. On the host where you will run the Teleport Database Service, enable and start Teleport:

```
$ sudo systemctl enable teleport
$ sudo systemctl start teleport
```

You can check the status of the Teleport Database Service with `systemctl status teleport` and view its logs with `journalctl -fu teleport`.

**TAR Archive**

Configure the Teleport Database Service to start automatically when the host boots up by creating a systemd service for it. On the host where you will run the Teleport Database Service, create a systemd service configuration for Teleport, enable the Teleport service, and start Teleport:

```
$ sudo teleport install systemd -o /etc/systemd/system/teleport.service
$ sudo systemctl enable teleport
$ sudo systemctl start teleport
```

You can check the status of the Teleport Database Service with `systemctl status teleport` and view its logs with `journalctl -fu teleport`.

**No systemd**

On the host where you will run the Teleport Database Service, start Teleport:

```
$ sudo teleport start --config=/etc/teleport.yaml
```

Teleport runs in the foreground and outputs logs for the services it is running.

## Step 2/5. Create a Teleport user

---

TIP

To modify an existing user to provide access to the Database Service, see [Database Access Controls](https://goteleport.com/docs/enroll-resources/database-access/rbac.md)

---

**Teleport Community Edition**

Create a local Teleport user with the built-in `access` role:

```
$ tctl users add \
  --roles=access \
  --db-users="*" \
  --db-names="*" \
  alice
```

**Teleport Enterprise/Enterprise Cloud**

Create a local Teleport user with the built-in `access` and `requester` roles:

```
$ tctl users add \
  --roles=access,requester \
  --db-users="*" \
  --db-names="*" \
  alice
```

| Flag         | Description                                                                                                                              |
| ------------ | ---------------------------------------------------------------------------------------------------------------------------------------- |
| `--roles`    | List of roles to assign to the user. The builtin `access` role allows them to connect to any database server registered with Teleport.   |
| `--db-users` | List of database usernames the user will be allowed to use when connecting to the databases. A wildcard allows any user.                 |
| `--db-names` | List of logical databases (aka schemas) the user will be allowed to connect to within a database server. A wildcard allows any database. |

---

WARNING

Database names are only enforced for PostgreSQL, MongoDB, and Cloud Spanner databases.

---

For more detailed information about database access controls and how to restrict access see [RBAC](https://goteleport.com/docs/enroll-resources/database-access/rbac.md) documentation.

## Step 3/5. Create an Amazon Keyspaces role

Create an AWS IAM Role that will be used as your Keyspaces user.

1. Define a trust policy for the role. The following command retrieves your AWS account ID and grants access to the account to the principal you selected when starting this guide. Select the tab for your principal:

   **Role**

   ```
   $ ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
   $ jq -n --arg acct "$ACCOUNT_ID" \
     '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"AWS":("arn:aws:iam::"+$acct+":role/TeleportDatabaseServiceRole")},"Action":"sts:AssumeRole"}]}' > keyspaces-trust-policy.json
   ```

   **User**

   ```
   $ ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
   $ jq -n --arg acct "$ACCOUNT_ID" \
     '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"AWS":("arn:aws:iam::"+$acct+":user/TeleportDatabaseServiceUser")},"Action":"sts:AssumeRole"}]}' > keyspaces-trust-policy.json
   ```

   This command writes a trust policy document to `keyspaces-trust-policy.json`.

2. Create an IAM role called `KeyspacesReader` that uses this trust policy. We will use this role to connect to the database later in this guide:

   ```
   $ aws iam create-role \
     --role-name KeyspacesReader \
     --assume-role-policy-document file://keyspaces-trust-policy.json
   ```

3. Assign permissions to the `KeyspacesReader` role.

   AWS provides the `AmazonKeyspacesReadOnlyAccess` and `AmazonKeyspacesFullAccess` IAM policies that you can incorporate into your Keyspaces user's role. You can choose `AmazonKeyspacesReadOnlyAccess` for read-only access to Amazon Keyspaces or `AmazonKeyspacesFullAccess` for full access.

   Assign one of these policies to the role you created:

   **Read-only**

   ```
   $ aws iam attach-role-policy \
     --role-name KeyspacesReader \
     --policy-arn arn:aws:iam::aws:policy/AmazonKeyspacesReadOnlyAccess
   ```

   **Full access**

   ```
   $ aws iam attach-role-policy \
     --role-name KeyspacesReader \
     --policy-arn arn:aws:iam::aws:policy/AmazonKeyspacesFullAccess
   ```

---

TIP

The `AmazonKeyspacesReadOnlyAccess` and `AmazonKeyspacesFullAccess` policies may provide too much or not enough access for your intentions. Validate that these meet your expectations if you plan on using them. You can also create your own custom Amazon Keyspaces Permissions Policies: [Amazon Keyspaces identity-based policy examples](https://docs.aws.amazon.com/keyspaces/latest/devguide/security_iam_id-based-policy-examples.html).

---

## Step 4/5. Give Teleport permissions to assume roles

Next, attach the following policy to the IAM role or IAM user the Teleport Database Service instance is using, which allows the Database Service to assume the IAM roles.

1. Save the following JSON document as `assume-role.json`:

   ```
   {
     "Version": "2012-10-17",
     "Statement": [
       {
         "Effect": "Allow",
         "Action": "sts:AssumeRole",
         "Resource": "*"
       }
     ]
   }

   ```

2. Create the policy and attach it to the Teleport Database Service. This depends on whether the Database Service authenticates as an IAM role or IAM user:

   **Role**

   ```
   $ aws iam put-role-policy \
     --role-name TeleportDatabaseServiceRole \
     --policy-name TeleportAssumeRole \
     --policy-document file://assume-role.json
   ```

   **User**

   ```
   $ aws iam put-user-policy \
     --user-name TeleportDatabaseServiceUser \
     --policy-name TeleportAssumeRole \
     --policy-document file://assume-role.json
   ```

---

TIP

You can make the policy more strict by providing specific IAM role resource ARNs in the "Resource" field instead of using a wildcard.

---

## Step 5/5. Connect

Once the Database Service has joined the cluster, log in to see the available databases:

**Self-Hosted**

```
$ tsh login --proxy=teleport.example.com --user=alice
$ tsh db ls
Name      Description Allowed Users Labels  Connect
--------- ----------- ------------- ------- -------
keyspaces             [*]           env=dev
```

**Teleport Enterprise Cloud**

```
$ tsh login --proxy=mytenant.teleport.sh --user=alice
$ tsh db ls
Name      Description Allowed Users Labels  Connect
--------- ----------- ------------- ------- -------
keyspaces             [*]           env=dev
```

To connect to a particular database instance using the `KeyspacesReader` AWS IAM Keyspaces role as a database user:

```
$ tsh db connect --db-user=KeyspacesReader keyspaces
Connected to Amazon Keyspaces at localhost:55084
[cqlsh 6.0.0 | Cassandra 3.11.2 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
KeyspacesReader@cqlsh>
```

---

TIP

To connect to the database using a tool of your choice (for example, a graphical database client), create a local tunnel and configure your client to use it.

```
$ tsh proxy db keyspaces --tunnel --db-user=KeyspacesReader
```

See [Database GUI Clients](https://goteleport.com/docs/connect-your-client/third-party/gui-clients.md) guide for more information on specific tools.

---

To log out of the database and remove credentials:

```
Remove credentials for a particular database instance.
$ tsh db logout keyspaces
Remove credentials for all database instances.
$ tsh db logout
```

## Further reading

- [How Amazon Keyspaces works with IAM](https://docs.aws.amazon.com/keyspaces/latest/devguide/security_iam_service-with-iam.html)
- [What is Amazon Keyspaces (for Apache Cassandra)?](https://docs.aws.amazon.com/keyspaces/latest/devguide/what-is-keyspaces.html)

## Next steps

- Learn how to [restrict access](https://goteleport.com/docs/enroll-resources/database-access/rbac.md) to certain users and databases.

* View the [High Availability (HA)](https://goteleport.com/docs/installation/agents/high-availability.md) guide.

- Take a look at the YAML configuration [reference](https://goteleport.com/docs/enroll-resources/database-access/reference/configuration.md).

* See the full CLI [reference](https://goteleport.com/docs/enroll-resources/database-access/reference/cli.md).
