Fork me on GitHub
Teleport

Database Access with PostgreSQL on AWS

Improve
Setting up Database Access with Amazon Aurora PostgreSQL

Setting up Database Access with Amazon Aurora PostgreSQL

Length: 17:15

AWS RDS/Aurora PostgreSQL

Enable IAM authentication

Teleport Database Access for AWS RDS and Aurora uses IAM authentication which can be enabled with the following steps.

Open Amazon RDS console and create a new database instance with IAM authentication enabled, or modify an existing one to turn it on. Make sure to use PostgreSQL database type.

Aurora Serverless

Aurora Serverless does not support IAM authentication at the time of this writing so it can't be used with Database Access.

See Enabling and disabling IAM database authentication for more information.

Create IAM policy

To allow Teleport database service to log into the database instance using auth token, create an IAM policy and attach it to the user whose credentials the database service will be using. For example:

{
   "Version": "2012-10-17",
   "Statement": [
      {
         "Effect": "Allow",
         "Action": [
             "rds-db:connect"
         ],
         "Resource": [
             "arn:aws:rds-db:us-east-2:1234567890:dbuser:cluster-ABCDEFGHIJKL01234/*"
         ]
      }
   ]
}

The resource ARN in the policy has the following format:

arn:aws:rds-db:<region>:<account-id>:dbuser:<db-cluster-resource-id>/<db-user-name>

Parameters:

  • region: AWS region where the database cluster is deployed.
  • account-id: AWS account ID the database cluster is deployed under.
  • db-cluster-resource-id: identifier for the database cluster, can be found under Configuration section in the RDS control panel.
  • db-user-name: name of the database account to associate with IAM authentication. Can be a wildcard.

See Creating and using an IAM policy for IAM database access for more information.

Create database user

Database users must have a rds_iam role in order to be allowed access. For PostgreSQL:

CREATE USER alice;
GRANT rds_iam TO alice;

See Creating a database account using IAM authentication for more information.

Setup Teleport Auth and Proxy services

Teleport Database Access for PostgreSQL on AWS is available starting from 6.0 release.

Download the latest version of Teleport for your platform from our downloads page and follow the installation instructions.

Teleport requires a valid TLS certificate to operate and can fetch one automatically using Let's Encrypt ACME protocol. We will assume that you have configured DNS records for teleport.example.com and *.teleport.example.com to point to the Teleport node.

Generate Teleport config with ACME enabled:

$ teleport configure --cluster-name=teleport.example.com --acme [email protected] -o file
Web Proxy Port

Teleport uses TLS-ALPN-01 ACME challenge to validate certificate requests which only works on port 443. As such, in order to use ACME for certificate management, web proxy needs to be accessible on port 443.

Start Teleport Auth and Proxy services:

$ sudo teleport start

Database service requires a valid auth token to connect to the cluster. Generate one and save it in /tmp/token:

$ tctl tokens add --type=db

Create role and user

Create the role that will allow a user to connect to any database using any database account:

$ tctl --config=/path/to/teleport.yaml create <<EOF
kind: role
version: v4
metadata:
  name: db
spec:
  allow:
    db_labels:
      '*': '*'
    db_names:
    - '*'
    db_users:
    - '*'
EOF

Create the user assigned the db role we've just created:

tctl --config=/path/to/teleport.yaml users add --roles=admin,db testuser

Start Database service with CLI flags

For a quick try-out, Teleport database service doesn't require a configuration file and can be launched using a single CLI command:

teleport db start \ --token=/tmp/token \ --auth-server=teleport.example.com:3080 \ --name=aurora \ --protocol=postgres \ --uri=postgres-aurora-instance-1.xxx.us-east-1.rds.amazonaws.com:5432 \ --aws-region=us-east-1 \ --labels=env=dev

Note that the --auth-server flag must point to the Teleport cluster's proxy endpoint because database service always connects back to the cluster over a reverse tunnel.

Start Database service with config file

Below is an example of a database service configuration file that proxies a single AWS Aurora PostgreSQL database:

teleport:
  data_dir: /var/lib/teleport-db
  nodename: test
  # Proxy address to connect to. Note that it has to be the proxy address
  # because database service always connects to the cluster over reverse
  # tunnel.
  auth_servers:
  - teleport.example.com:3080
db_service:
  enabled: "yes"
  # This section contains definitions of all databases proxied by this
  # service, can contain multiple items.
  databases:
    # Name of the database proxy instance, used to reference in CLI.
  - name: "aurora"
    # Free-form description of the database proxy instance.
    description: "AWS Aurora PostgreSQL"
    # Database protocol.
    protocol: "postgres"
    # Database address, example of a AWS Aurora endpoint in this case.
    uri: "postgres-aurora-instance-1.xxx.us-east-1.rds.amazonaws.com:5432"
    # AWS specific configuration, only required for RDS and Aurora.
    aws:
      # Region the database is deployed in.
      region: us-east-1
    # Labels to assign to the database, used in RBAC.
    static_labels:
      env: dev
auth_service:
  enabled: "no"
ssh_service:
  enabled: "no"
proxy_service:
  enabled: "no"
Tip

A single Teleport process can run multiple different services, for example multiple database access proxies as well as running other services such an SSH service or an application access proxy.

Start the database service:

teleport start --config=/path/to/teleport-db.yaml --token=/tmp/token

AWS credentials

When setting up Teleport database service with AWS RDS or Aurora, it must have an IAM role allowing it to connect to that particular database instance. An example of such a policy is shown in the Create IAM Policy section above. See Creating and using an IAM policy for IAM database access in AWS documentation.

Teleport database service uses the default credential provider chain to find AWS credentials. See Specifying Credentials for more information.

Connect

Once the database service has joined the cluster, login to see the available databases:

tsh login --proxy=teleport.example.com --user=testuser
tsh db ls

Name Description Labels

------ --------------------- --------

aurora AWS Aurora PostgreSQL env=dev

Note that you will only be able to see databases your role has access to. See RBAC section for more details.

To connect to a particular database server, first retrieve credentials from Teleport using tsh db login command:

tsh db login aurora
Tip

You can be logged into multiple databases simultaneously.

You can optionally specify the database name and the user to use by default when connecting to the database instance:

tsh db login --db-user=postgres --db-name=postgres aurora

Once logged in, connect to the database:

tsh db connect aurora
Note

The psql command-line client should be available in PATH in order to be able to connect.

If you would like to see the native psql shell connect command, run:

tsh db config --format=cmd aurora

To log out of the database and remove credentials:

Remove credentials for a particular database instance.

tsh db logout aurora

Remove credentials for all database instances.

tsh db logout
Have a suggestion or can’t find something?
IMPROVE THE DOCS