Skip to main content

Database Access with Redshift Serverless on AWS

Teleport can provide secure access to AWS Redshift Serverless via the Teleport Database Service. This allows for fine-grained access control through Teleport's RBAC.

In this guide, you will:

  1. Configure an AWS Redshift Serverless database with IAM authentication.
  2. Join the AWS Redshift Serverless database to your Teleport cluster.
  3. Connect to the AWS Redshift Serverless database via the Teleport Database Service.

This guide will help you to:

  • Set up Teleport to access your AWS Redshift Serverless workgroups.
  • Connect to your databases through Teleport.

Teleport Database Access Redshift Self-Hosted

Prerequisites

  • A running Teleport cluster version 14.3.33 or above. If you want to get started with Teleport, sign up for a free trial or set up a demo environment.

  • The tctl admin tool and tsh client tool.

    Visit Installation for instructions on downloading tctl and tsh.

  • AWS account with a Redshift Serverless configuration and permissions to create and attach IAM policies.
  • Command-line client psql installed and added to your system's PATH environment variable.
  • A host where you will run the Teleport Database Service. This guide assumes an EC2 instance, and provides a corresponding example of access control.
  • A running Teleport Discovery Service if you plan to use Database Auto-Discovery.
  • To check that you can connect to your Teleport cluster, sign in with tsh login, then verify that you can run tctl commands using your current credentials. tctl is supported on macOS and Linux machines. For example:
    $ tsh login --proxy=teleport.example.com [email protected]
    $ tctl status
    # Cluster teleport.example.com
    # Version 14.3.33
    # CA pin sha256:abdc1245efgh5678abdc1245efgh5678abdc1245efgh5678abdc1245efgh5678
    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/4. Create an IAM Role for user access

Create an AWS IAM role to provide user access to Redshift Serverless. This role will be granted to Teleport users via a corresponding Teleport role. In this guide we will use the example name teleport-redshift-serverless-access.

Under Trusted entity type choose "Custom trust policy". Edit the trust policy to allow the IAM role generated in the previous step to assume this role, so that the Teleport node can use the permissions granted by this role to access databases:

{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Statement1",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::abcd1234-this-is-an-example:role/teleport-redshift-serverless-node",
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}

Remember to replace the example AWS account ID.

Create and apply a permission policy to allow access to Redshift Serverless. This policy can allow access to all instances:

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "redshift-serverless:GetCredentials",
"Resource": "*"
}
]
}

Or you can restrict the Resource line to a specific Redshift Serverless workgroup:

{
...
"Resource": "arn:aws:redshift-serverless:us-west-2:1234567890:workgroup/some-workgroup-id"
...
}

Step 2/4. Create a Teleport role for Redshift Serverless access

On your workstation logged in to your Teleport cluster with tsh, define a new role to provide access to Redshift Serverless. Our example file is redshift-role.yaml:

version: v5
kind: role
metadata:
name: redshift-serverless-access
spec:
allow:
db_labels:
'*': '*'
db_names:
- dev
db_users:
- 'teleport-redshift-serverless-access'
  • The value of db_users corresponds to the IAM role created in the previous step. You can provide either the role name or the full AWS ARN of the IAM role.
  • The value(s) for db_names will depend on your Redshift Serverless configuration, but dev is the default name applied by AWS. You can also provide * to grant access to all instances.

Save this file and apply it to your Teleport cluster:

$ tctl create -f redshift-role.yaml
role 'redshift-serverless-access' has been created

Assign the redshift-serverless-access role to your Teleport user by running the appropriate commands for your authentication provider:

  1. Retrieve your local user's configuration resource:

    $ tctl get users/$(tsh status -f json | jq -r '.active.username') > out.yaml
  2. Edit out.yaml, adding redshift-serverless-access to the list of existing roles:

      roles:
    - access
    - auditor
    - editor
    + - redshift-serverless-access
  3. Apply your changes:

    $ tctl create -f out.yaml
  4. Sign out of the Teleport cluster and sign in again to assume the new role.

Step 3/4. Install and start 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:

Install Teleport on the host where you will run the Teleport Database Service:

Select an edition, then follow the instructions for that edition to install Teleport.

The following command updates the repository for the package manager on the local operating system and installs the provided Teleport version:

$ curl https://cdn.teleport.dev/install-v14.3.33.sh | bash -s 14.3.33

Databases can be registered dynamically by Discovery Service, tctl, etc.

Generate a Database Service configuration that monitors the dynamic database resources:

$ sudo teleport db configure create \
-o file \
--proxy=mytenant.teleport.sh:443 \
--token=/tmp/token \
--dynamic-resources-labels env=prod

This command will place the Database Service configuration at the /etc/teleport.yaml location.

Enable auto-discovery for AWS Redshift Serverless in Discovery Service?

In your Teleport Discovery Service's configuration, use AWS matcher type redshift-serverless, and update region and tags that match your Redshift Serverless databases:

discovery_service:
enabled: "yes"
aws:
- types: ["redshift-serverless"]
regions: ["us-west-1"]
tags:
"env": "prod" # Match database resource tags where tag:env=prod

Restart the Discovery Service.

Bootstrap access to Redshift Serverless

Teleport can bootstrap IAM permissions for the Database Service based on its configuration:

Teleport can bootstrap IAM permissions for the Database Service based on Discovery Service's configuration while bootstrapping the Discovery Service using the teleport discovery bootstrap command. You can use this command in automatic or manual mode:

  • In automatic mode, Teleport will attempt to create appropriate IAM policies and attach them to the specified IAM role. This requires IAM permissions to create and attach IAM policies.
  • In manual mode, Teleport will print required IAM policies. You can then create and attach them manually using the AWS management console. Add --manual flag to the command to enable manual mode.
$ teleport discovery bootstrap \
--attach-to-role arn:aws:iam::aws-account-id:role/discovery-iam-role-name \
--policy-name TeleportDatabaseDiscovery \
--database-service-role arn:aws:iam::aws-account-id:role/database-iam-role-name \
--database-service-policy-name TeleportDatabaseAccess \
-c path to Discovery's teleport.yaml

Alternatively, you can create or print the required IAM policies with the following commands and manually attach them to the IAM role:

  • teleport db configure aws create-iam --types redshift-serverless
  • teleport db configure aws print-iam --types redshift-serverless

Start the Database service

Configure the Teleport Database Service to start automatically when the host boots up by creating a systemd service for it. The instructions depend on how you installed the Teleport Database Service.

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.

Step 4/4. Connect

Once the Database Service has started and joined the cluster, log in to see the registered databases. Replace --proxy with the address of your Teleport Proxy Service or cloud tenant:

$ tsh login --proxy=mytenant.teleport.sh --user=alice
$ tsh db ls
Name Description Labels
----------- ------------------------------ --------
my-redshift Redshift cluster in us-east-1 ...

To connect to the Redshift Serverless instance:

$ tsh db connect my-redshift --db-user=teleport-redshift-serverless-access --db-name=dev
psql (15.1, server 8.0.2)
WARNING: psql major version 15, server major version 8.0.
Some psql features might not work.
SSL connection (protocol: TLSv1.3, cipher: TLS_CHACHA20_POLY1305_SHA256, compression: off)
Type "help" for help.

dev=>

To log out of the database and remove credentials:

$ tsh db logout my-redshift

Troubleshooting

User permission errors

The IAM role teleport-redshift-serverless-access will be automatically mapped as IAMR:teleport-redshift-serverless-access inside the Redshift Serverless database.

Users (database admins) can optionally set up this database user's permissions prior to logging in as this new IAM role to avoid or resolve user permission issues:

  1. Connect to the Redshift Serverless workgroup as the admin user, and execute:

    CREATE USER "IAMR:teleport-redshift-serverless-access" WITH PASSWORD DISABLE;
  2. Grant this user appropriate in-database permissions. For example:

    GRANT SELECT ON TABLE users  TO "IAMR:teleport-redshift-serverless-access";

Certificate error

If your tsh db connect error includes the following text, you likely have an RDS database created before July 28, 2020, which presents an X.509 certificate that is incompatible with Teleport:

x509: certificate relies on legacy Common Name field, use SANs instead

AWS provides instructions to rotate your SSL/TLS certificate.

No credential providers error

If you see the error NoCredentialProviders: no valid providers in chain in Database Service logs then Teleport is not detecting the required credentials to connect via AWS IAM permissions. Check whether the credentials or security role has been applied in the machine running the Teleport Database Service.

Timeout errors

The Teleport Database Service needs connectivity to your database endpoints. That may require enabling inbound traffic on the database from the Database Service on the same VPC or routing rules from another VPC. Using the nc program you can verify connections to databases:

$ nc -zv postgres-instance-1.sadas.us-east-1.rds.amazonaws.com 5432
# Connection to postgres-instance-1.sadas.us-east-1.rds.amazonaws.com (172.31.24.172) 5432 port [tcp/postgresql] succeeded!

Not authorized to perform sts:AssumeRole

The Database Service assumes an IAM role in one of following situations:

  • An IAM role is used as db_user when accessing AWS services that require IAM roles as database users, such as DynamoDB, Keyspaces, Opensearch, and Redshift Serverless.
  • The assume_role_arn field is specified for the database resources or dynamic resource matchers.
Role chaining

When both of the above conditions are true for a database connection, the Database Service performs a role chaining by assuming the IAM role specified assume_role_arn first then using that IAM role to assume the IAM role for db_user.

You may encounter the following error if the trust relationship is not configured properly between the IAM roles:

AccessDenied: User: arn:aws:sts::111111111111:assumed-role/database-service-role/i-* is not authorized to perform: sts:AssumeRole on resource: arn:aws:iam::111111111111:role/database-user-role

To allow IAM Role role1 to assume IAM Role role2, the following is generally required:

1. Configure Trust Relationships on role2

role1 or its AWS account should be set as Principal in role2's trust policy.

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::111111111111:role/role1"
},
"Action": "sts:AssumeRole"
}
]
}
2. Configure Permissions Policies on role1

role1 requires sts:AssumeRole permissions, for example:

{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Resource": "arn:aws:iam::111111111111:role/role2"
}
]
}

Note that this policy can be omitted when role1 and role2 are in the same AWS account and role1's full ARN is configured as Principal in role2's trust policy.

3. Configure Permissions Boundary on role1

role1 also requires sts:AssumeRole permissions in its boundary policy, for example:

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

Note that this is only required when a boundary policy is attached to role1.

You can test the trust relationship by running this AWS CLI command as role1:

aws sts assume-role --role-arn arn:aws:iam::111111111111:role/role2 --role-session-name test-trust-relationship

Learn more on how to use trust policies with IAM roles.

Unable to cancel a query

If you use a PostgreSQL cli client like psql, and you try to cancel a query with ctrl+c, but it doesn't cancel the query, then you need to connect using a tsh local proxy instead. When psql cancels a query, it establishes a new connection without TLS certificates, however Teleport requires TLS certificates not only for authentication, but also to route database connections.

If you enable TLS Routing in Teleport then tsh db connect will automatically start a local proxy for every connection. Alternatively, you can connect via Teleport Connect which also uses a local proxy. Otherwise, you need to start a tsh local proxy manually using tsh proxy db and connect via the local proxy.

If you have already started a long-running query in a psql session that you cannot cancel with ctrl+c, you can start a new client session to cancel that query manually:

First, find the query's process identifier (PID):

SELECT session_id AS pid, database_name,start_time,trim(query_text) AS query FROM SYS_QUERY_HISTORY WHERE status = 'running';

Next, gracefully cancel the query using its PID. This will send a SIGINT signal to the postgres backend process for that query:

SELECT pg_cancel_backend(<PID>);

You should always try to gracefully terminate a query first, but if graceful cancellation is taking too long, then you can forcefully terminate the query instead. This will send a SIGTERM signal to the postgres backend process for that query:

SELECT pg_terminate_backend(<PID>);

See the PostgreSQL documentation on admin functions for more information about the pg_cancel_backend and pg_terminate_backend functions.

Next steps

  • See the YAML configuration reference for updating dynamic resource matchers or static database definitions.