Skip to main content

Database Access with AWS RDS Proxy for PostgreSQL

Teleport can provide secure access to AWS RDS proxy for PostgreSQL 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 RDS proxy for PostgreSQL with IAM authentication.
  2. Join the AWS RDS proxy for PostgreSQL database to your Teleport cluster.
  3. Connect to the AWS RDS proxy for PostgreSQL database via the Teleport Database Service.

Teleport Database Access RDS Proxy Self-Hosted

Supported Engine Family

Teleport currently supports RDS Proxy instances with engine family PostgreSQL, MariaDB/MySQL or Microsoft SQL Server.

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 RDS Proxy instances and permissions to create and attach IAM policies.
  • Any RDS Proxy instances intended for connection through Teleport must have TLS enabled.
  • A host, e.g., an EC2 instance, where you will run the Teleport Database Service.
  • 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/5. Create a Database Service configuration

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 RDS Proxy in Discovery Service?

In your Teleport Discovery Service's configuration, use AWS matcher type rdsproxy, and update region and tags that match your RDS Proxy databases:

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

Restart the Discovery Service.

Step 2/5. Create an IAM policy for Teleport

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 rdsproxy
  • teleport db configure aws print-iam --types rdsproxy

Step 3/5. 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/5. Configure database user credentials

The Database Service connects to an RDS Proxy instance using IAM authentication. In addition, the RDS Proxy instance must also be able to connect to the RDS DB instance or Aurora DB cluster using pre-configured database user credentials.

First, use Secrets Manager to store sets of user name and password credentials. You create a separate Secrets Manager secret for each database user account that RDS Proxy connects to on the RDS DB instance or Aurora DB cluster. The password associated with the secret must match the database password for that user in the target database.

For example, the following AWS CLI command creates a Secrets Manager secret for database user alice:

$ aws secretsmanager create-secret \
--name rdsproxy_alice --description "database user alice" \
--secret-string '{"username":"alice","password":"password_for_alice"}'

Next, create an IAM role with a policy that can access these secrets, for example:

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "secretsmanager:GetSecretValue",
"Resource": [
"arn:aws:secretsmanager:us-west-1:account_id:secret:rdsproxy_alice",
"arn:aws:secretsmanager:us-west-1:account_id:secret:rdsproxy_anotheruser"
]
},
{
"Effect": "Allow",
"Action": "kms:Decrypt",
"Resource": "arn:aws:kms:us-west-1:account_id:key/key_id",
"Condition": {
"StringEquals": {
"kms:ViaService": "secretsmanager.us-east-2.amazonaws.com"
}
}
}
]
}

The IAM role should have the following trust policy:

{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "rds.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}

Lastly, modify the RDS Proxy authentication settings to use the created IAM role and Secrets Manager secrets. Make sure IAM authentication is set to Required:

RDS Proxy Authentication

IAM authentication per database user

If you need to disable IAM authentication for some database users that are not intended for Teleport access, use the following AWS CLI command to set IAMAuth per secret:

$ aws rds modify-db-proxy --db-proxy-name my-rds-proxy --auth AuthScheme=SECRETS,SecretArn=arn-of-teleport-access-user,\
IAMAuth=REQUIRED AuthScheme=SECRETS,SecretArn=arn-of-non-teleport-user,IAMAuth=DISABLED ...

See aws rds modify-db-proxy help for more information.

Step 5/5. Connect

Once the Database Service has started and joined the cluster, log in to see the registered databases:

$ tsh login --proxy=teleport.example.com --user=alice
$ tsh db ls
Name Description Labels
---------------------------- ------------------------------- -------
rds-proxy RDS Proxy in us-west-1 ...
rds-proxy-my-reader-endpoint RDS Proxy endpoint in us-west-1 ...

To retrieve credentials for a database and connect to it:

$ tsh db connect --db-user=alice --db-name=dev rds-proxy

To log out of the database and remove credentials:

$ tsh db logout rds-proxy

Troubleshooting

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.

Maximum policy size exceeded errors

Due to IAM and STS character limits, you may encounter one of the following errors in the Database Service logs when large numbers of databases are registered:

  • LimitExceeded: Maximum policy size of 2048 bytes exceeded for user <iam-user>
  • LimitExceeded: Maximum policy size of 10240 bytes exceeded for role <iam-role>

For reference, a user policy can maintain permissions for approximately 6 Redshift databases, or 20 RDS databases due to the IAM policy character limits. A role policy can maintain permissions for approximately 30 Redshift databases, or 100 RDS databases.

To get around this limit, try using one or a combination of the following methods:

Method 1: Organize IAM roles with "assume_role_arn"

You can reduce the policy size by separating them into multiple IAM roles. Use assume_role_arn to specify different IAM roles for accessing the databases:

You can specify assume_role_arn in the AWS matchers of Discovery Service's configuration:

discovery_service:
enabled: "yes"
aws:
- types: ["rds"]
regions: ["us-west-1", "us-west-2"]
assume_role_arn: "arn:aws:iam::123456789012:role/example-role-rds-env-prod-discovery"
tags:
"env": "prod"

- types: ["redshift", "redshift-serverless"]
regions: ["us-west-2"]
assume_role_arn: "arn:aws:iam::123456789012:role/example-role-redshift-env-dev"
tags:
"env": "dev"

The Discovery Service will use the IAM roles specified in assume_role_arn for discovery, and by default the Database Service will use the same IAM roles for authentication.

However, you can also overwrite the IAM roles for authentication by Database Service if you wish to use different roles:

db_service:
enabled: "yes"
resources:
# Matches us-west-1 env=prod RDS databases from Discovery Service, and
# overwrites assume_role_arn.
- labels:
"env": "prod"
"region": "us-west-1"
aws:
assume_role_arn: "arn:aws:iam::123456789012:role/example-role-rds-env-prod-us-west-1-access"

# Matches us-west-2 env=prod RDS databases from Discovery Service, and
# overwrites assume_role_arn.
- labels:
"env": "prod"
"region": "us-west-2"
aws:
assume_role_arn: "arn:aws:iam::123456789012:role/example-role-rds-env-prod-us-west-2-access"

# Matches env=dev Redshift databases from Discovery Service and inherits
# "arn:aws:iam::123456789012:role/example-role-redshift-env-dev"
- labels:
"env": "dev"
Auto-discovery labels

Teleport generates certain labels derived from the cloud resource attributes during discovery. See Auto-Discovery labels /labels/#auto-discovery) for more details.

Create or print the required IAM policies with the following commands and attach them to the respective IAM roles:

$ teleport db configure aws create-iam --types redshift,redshift-serverless --name teleport-redshift-access
$ teleport db configure aws print-iam --types redshift,redshift-serverless

Refer to the command usage for a complete list of database types supported by the --types option.

The IAM roles specified in assume_role_arn must trust the IAM identity of the host running the Database Service.

The assume_role_arn is not limited to the same AWS account so you can also use this feature for AWS Cross-Account Access.

Method 2: Manually manage your IAM policies

You can manually manage IAM policies for database connections instead of relying on the Database Service to update them.

For example, you can limit the character size by attaching a policy with a wildcard "*" for "Resource":

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "rds-db:connect",
"Resource": "*"
}
]
}

You can safely remove the inline policy created by the Database Service and the IAM permissions for the Database Service to Get/Put/Delete the user or role policy.

Method 3: Separate Database Services

You can deploy the Database Service in a highly available (HA) configuration where databases can be sharded to separate Database Services with different IAM roles.

Method 4: Use IAM roles instead of IAM users

IAM users have a lower character limit compared to IAM roles. If the limit is exceeded for a user policy, it is recommended to use IAM roles for the Database Service instead.

Next steps

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