Database Access with Amazon ElastiCache Serverless for Redis and Valkey
Teleport can provide secure access to Amazon ElastiCache Serverless via the Teleport Database Service. This allows for fine-grained access control through Teleport's RBAC.
In this guide, you will:
- Configure your Amazon ElastiCache Serverless database with IAM authentication.
- Add the database to your Teleport cluster.
- Connect to the database via Teleport.
How it works
The Teleport Database Service connects on user behalf using IAM authentication and proxies traffic from users to Amazon ElastiCache Serverless.
- Self-Hosted
- Teleport Cloud
This guide shows how to register a single Amazon Elasticache Serverless cache with your Teleport cluster. For a more scalable approach, learn how to set up Database Auto-Discovery to automatically enroll all AWS databases in your infrastructure.
Prerequisites
-
A running Teleport cluster. If you do not have one, read Getting Started.
-
The
tctl
andtsh
clients.Installing
tctl
andtsh
clients-
Determine the version of your Teleport cluster. The
tctl
andtsh
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:TELEPORT_DOMAIN=teleport.example.com:443TELEPORT_VERSION="$(curl -s https://$TELEPORT_DOMAIN/v1/webapi/find | jq -r '.server_version')" -
Follow the instructions for your platform to install
tctl
andtsh
clients:- Mac
- Windows - Powershell
- Linux
Download the signed macOS .pkg installer for Teleport, which includes the
tctl
andtsh
clients:curl -O https://cdn.teleport.dev/teleport-${TELEPORT_VERSION?}.pkgIn Finder double-click the
pkg
file to begin installation.dangerUsing 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.
curl.exe -O https://cdn.teleport.dev/teleport-v${TELEPORT_VERSION?}-windows-amd64-bin.zipUnzip 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.
All of the Teleport binaries in Linux installations include the
tctl
andtsh
clients. For more options (including RPM/DEB packages and downloads for i386/ARM/ARM64) see our installation page.curl -O https://cdn.teleport.dev/teleport-v${TELEPORT_VERSION?}-linux-amd64-bin.tar.gztar -xzf teleport-v${TELEPORT_VERSION?}-linux-amd64-bin.tar.gzcd teleportsudo ./installTeleport binaries have been copied to /usr/local/bin
-
- AWS account with an ElastiCache Serverless for Redis or Valkey cache.
- Permissions to create and attach IAM policies.
redis-cli
version6.2
or newer installed and added to your system'sPATH
environment variable.- 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 runtctl
commands using your current credentials. For example, run the following command, assigning teleport.example.com to the domain name of the Teleport Proxy Service in your cluster and [email protected] to your Teleport username:If you can connect to the cluster and run thetsh login --proxy=teleport.example.com --user=[email protected]tctl statusCluster teleport.example.com
Version 19.0.0-dev
CA pin sha256:abdc1245efgh5678abdc1245efgh5678abdc1245efgh5678abdc1245efgh5678
tctl status
command, you can use your current credentials to run subsequenttctl
commands from your workstation. If you host your own Teleport cluster, you can also runtctl
commands on the computer that hosts the Teleport Auth Service for full permissions.
Step 1/6. Create a Teleport user
To modify an existing user to provide access to the Database Service, see Database Access Controls
- Teleport Community Edition
- Teleport Enterprise/Enterprise Cloud
Create a local Teleport user with the built-in access
role:
tctl users add \ --roles=access \ --db-users="*" \ --db-names="*" \ alice
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. |
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 documentation.
Step 2/6. 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=textabcd123-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:
To install a Teleport Agent on your Linux server:
The easiest installation method, for Teleport versions 17.3 and above, is the cluster install script. It will use the best version, edition, and installation mode for your cluster.
-
Assign teleport.example.com:443 to your Teleport cluster hostname and port, but not the scheme (https://).
-
Run your cluster's install script:
curl "https://teleport.example.com:443/scripts/install.sh" | sudo bash
On older Teleport versions:
-
Assign edition to one of the following, depending on your Teleport edition:
Edition Value Teleport Enterprise Cloud cloud
Teleport Enterprise (Self-Hosted) enterprise
Teleport Community Edition oss
-
Get the version of Teleport to install. If you have automatic agent updates enabled in your cluster, query the latest Teleport version that is compatible with the updater:
TELEPORT_DOMAIN=teleport.example.com:443TELEPORT_VERSION="$(curl https://$TELEPORT_DOMAIN/v1/webapi/automaticupgrades/channel/default/version | sed 's/v//')"Otherwise, get the version of your Teleport cluster:
TELEPORT_DOMAIN=teleport.example.com:443TELEPORT_VERSION="$(curl https://$TELEPORT_DOMAIN/v1/webapi/ping | jq -r '.server_version')" -
Install Teleport on your Linux server:
curl https://cdn.teleport.dev/install.sh | bash -s ${TELEPORT_VERSION} editionThe installation script detects the package manager on your Linux server and uses it to install Teleport binaries. To customize your installation, learn about the Teleport package repositories in the installation guide.
Create the Database Service configuration:
ELASTICACHE_SERVERLESS_URI=""sudo teleport db configure create \ -o file \ --name="elasticache-serverless" \ --proxy=example.teleport.sh:443 \ --protocol="redis" \ --uri=${ELASTICACHE_SERVERLESS_URI?} \ --token=/tmp/token
Change example.teleport.sh:443
to the host and port of your Teleport Proxy
Service. Set ELASTICACHE_SERVERLESS_URI
to the domain name and port of your ElastiCache
database.
The command will generate a Database Service configuration and place it at the
/etc/teleport.yaml
location.
Step 3/6. Create an IAM role for Teleport
The Teleport Database Service needs AWS IAM permissions to provide access to ElastiCache Serverless caches.
Create an IAM role for Teleport
Grant the Database Service access to credentials that it can use to authenticate to AWS.
- If you are running the Database Service on an EC2 instance, you may use the EC2 Instance Metadata Service method
- If you are running the Database Service in Kubernetes, you can use IAM Roles for Service Accounts (IRSA)
- Otherwise, you must use environment variables
- Instance Metadata Service
- Kubernetes IRSA
- Environment Variables
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.
Refer to IAM Roles for Service Accounts (IRSA) 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.
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 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 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 for a detailed description of credential loading behavior.
Grant permissions
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ElastiCacheServerlessFetchMetadata",
"Effect": "Allow",
"Action": "elasticache:DescribeServerlessCaches",
"Resource": "*"
},
{
"Sid": "ElastiCacheServerlessDescribeUsers",
"Effect": "Allow",
"Action": "elasticache:DescribeUsers",
"Resource": "*"
},
{
"Sid": "ElastiCacheServerlessConnect",
"Effect": "Allow",
"Action": "elasticache:Connect",
"Resource": "*"
}
]
}
Statement | Purpose |
---|---|
ElastiCacheServerlessFetchMetadata | Automatically import AWS metadata about the database. |
ElastiCacheServerlessDescribeUsers | Determine whether a user is compatible with IAM authentication. |
ElastiCacheServerlessConnect | Connect using IAM authentication. |
Details
You can reduce the scope of the ElastiCacheServerlessConnect
statement by
updating it to only allow specific ElastiCache Serverless caches and IAM users.
The resource ARN you can specify has the following formats:
arn:aws:elasticache:{Region}:{AccountID}:serverlesscache:{CacheName}arn:aws:elasticache:{Region}:{AccountID}:user:{UserName}
See Authenticating with IAM for ElastiCache for more information.
Step 4/6. Start the Database Service
Configure the 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 Database Service.
- Package Manager
- TAR Archive
On the host where you will run the Database Service, enable and start Teleport:
sudo systemctl enable teleportsudo systemctl start teleport
On the host where you will run the 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.servicesudo systemctl enable teleportsudo systemctl start teleport
You can check the status of the Database Service with systemctl status teleport
and view its logs with journalctl -fu teleport
.
Step 5/6. Configure authentication for ElastiCache users
Configure authentication for your ElastiCache Serverless database.
To enable ACL, please see Authenticating users with Role-Based Access Control for ElastiCache.
ElastiCache Serverless Redis OSS will automatically create a default user with no password. We strongly recommend that you take steps to disable the default user. The automatically created default user cannot be modified or deleted and you cannot remove the default user name from an ElastiCache Redis OSS user group. To disable the default user:
- create a new user with the user name "default" and a unique (i.e., not "default") user ID.
- replace the default user in your ElastiCache user group with the new default user.
We recommend setting a strong password on your customized default user (IAM authentication is not possible for the default user) and using an access string that is not permissive.
For example, the access string off -@all
will disable logins and deny all Redis privileges to the default user.
See Applying RBAC for more information.
Some additional limitations apply when using IAM authentication - for more information, see: ElastiCache Auth IAM Limits.
There are a few requirements for configuring an ElastiCache IAM-enabled user:
- the user must have identical username and user id properties.
- the user must have authentication mode set to "IAM".
- the user must be attached to an ElastiCache user group.
Create an ElastiCache IAM-enabled user.
The following example creates an ElastiCache user with the access string
on ~* +@all
that represents an active user with access to all available keys
and commands:
aws elasticache create-user \ --user-name iam-user-01 \ --user-id iam-user-01 \ --authentication-mode Type=iam \ --engine redis \ --access-string "on ~* +@all"
You may prefer a less permissive access string for your ElastiCache users. For more information about ElastiCache access strings, please see: ElastiCache Cluster RBAC Access String.
Create an ElastiCache user group and attach it to your ElastiCache Serverless cache:
aws elasticache create-user-group \ --user-group-id iam-user-group-01 \ --engine redis \ --user-ids default iam-user-01aws elasticache modify-serverless-cache \ --serverless-cache-name serverless-cache-1 \ --user-group-id "iam-user-group-01"
Once the ElastiCache user has been created, verify that the user is configured to satisfy the requirements for IAM authentication:
If you choose not to use the above options, Teleport will not automatically authenticate with the database.
You can either set up a "no password" configuration for your ElastiCache Serverless user,
or manually enter an AUTH
command with the password you have configured after
a successful client connection. However, it is strongly advised to use one of
the first two options or a strong password for better security.
Step 6/6. Connect
Once the Database Service has started and joined the cluster, log in to see the registered databases:
- Self-Hosted
- Teleport Enterprise Cloud
tsh login --proxy=teleport.example.com --user=alicetsh db lsName Description Allowed Users Labels------------------- ----------- ------------- -----------------------my-serverless-cache [*] account_id=123456789012
tsh login --proxy=mytenant.teleport.sh --user=alicetsh db lsName Description Allowed Users Labels------------------- ----------- ------------- -----------------------my-serverless-cache [*] account_id=123456789012
To retrieve credentials for a database and connect to it:
tsh db connect --db-user=my-database-user my-serverless-cache
If flag --db-user
is not provided, Teleport logs in as the default
user.
Now, depending on the authentication configurations, you may need to send an
AUTH
command to authenticate with the Redis server:
- Redis with ACL
- Redis with AUTH token
- Redis without AUTH
The Database Service automatically authenticates Teleport-managed and
IAM-enabled users with the database. No AUTH
command is required after
successful connection.
If you are connecting as a user that is not managed by Teleport and is not
IAM-enabled, the connection normally starts as the default
user.
Now you can authenticate the database user with its password:
AUTH my-database-user <USER_PASSWORD>
Now you can authenticate with the shared AUTH token:
AUTH <SHARED_AUTH_TOKEN>
For Redis or Valkey deployments without the ACL system or legacy
requirepass
directive enabled, no AUTH
command is required.
To log out of the database and remove credentials:
Remove credentials for a particular database instance.
tsh db logout my-serverless-cacheRemove credentials for all database instances.
tsh db logout
Troubleshooting
Certificate error
If your tsh db connect
error includes the following text, you likely have an RDS or DocumentDB 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.
When running on EKS, this error may occur if the Teleport Database Service cannot access IMDSv2 when the PUT requests hop limit on the worker node instance is set to 1. You can use the following commands to check the hop limit:
aws ec2 describe-instances --instance-ids <node-instance-id> | grep HttpPutResponseHopLimit"HttpPutResponseHopLimit": 1,
See IMDSv2 support for EKS and EKS best practices for more details.
Timeout errors
The Teleport Database Service requires connectivity to your database endpoints.
Check that firewall rules (e.g., AWS security groups) allow connectivity between the Teleport Database Service and the database endpoint.
- Inbound firewall rules for the database must allow connections from the Teleport Database Service.
- Outbound firewall rules for the Teleport Database Service must allow connections to the database endpoint.
On the same host as the Teleport Database Service, try running nc
to check if it can reach the database port.
- Database host: postgres-instance-1.sadas.us-east-1.rds.amazonaws.com
- Database port: 5432
nc -zv database-host database-portConnection to postgres-instance-1.sadas.us-east-1.rds.amazonaws.com (172.31.24.172) 5432 port [tcp/postgresql] succeeded!
Debugging connection timeout errors in AWS
For deployments in AWS, it may be helpful to use AWS Reachability Analyzer to analyze the network path between the Teleport Database Service and the database.
- Identify the Elastic Network Interface (ENI) associated with the Teleport Database Service host. This can be found in the EC2 console.
- Identify the private IP address of the database.
- Create and analyze a network path:
- Set the path source to the ENI associated with the Teleport Database Service host.
- Set the path destination to the database IP.
- Check the analysis results to identify reachability issues.
Not authorized to perform sts:AssumeRole
The Database Service assumes an IAM role in one of following situations:
- A Teleport user specifies an IAM role as the database user they wish to use when accessing AWS services that require IAM roles as database users. Databases that support using an IAM role as a database user include: DynamoDB, Keyspaces, Opensearch, Redshift, and Redshift Serverless.
- The
assume_role_arn
field is specified for the database resources or dynamic resource matchers.
What if both situations apply? (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
in assume_role_arn
first, then using that IAM role to assume the IAM role for
the database 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/teleport-db-service-role/i-* is not authorized to perform: sts:AssumeRole on resource: arn:aws:iam::111111111111:role/db-user-role
how to properly configure the trust relationship?
To allow IAM Role teleport-db-service-role
to assume IAM Role db-user-role
, the following is
generally required:
1. Configure Trust Relationships on db-user-role
teleport-db-service-role
or its AWS account should be set as Principal
in db-user-role
's trust
policy.
- Role as principal
- Account as principal
- Cross-account with external-id
Assign aws-account-id to your AWS account ID:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::aws-account-id:role/teleport-db-service-role"
},
"Action": "sts:AssumeRole"
}
]
}
Assign aws-account-id to your AWS account ID:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::aws-account-id:root"
},
"Action": "sts:AssumeRole"
}
]
}
Assign external-aws-account-id to an external AWS account ID:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::external-aws-account-id:role/teleport-db-service-role"
},
"Action": "sts:AssumeRole",
"Condition": {
"StringEquals": {
"sts:ExternalId": "example-external-id"
}
}
}
]
}
2. Configure Permissions Policies on teleport-db-service-role
teleport-db-service-role
requires sts:AssumeRole
permissions, for example:
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Resource": "arn:aws:iam::aws-account-id:role/db-user-role"
}
]
}
Note that this policy can be omitted when teleport-db-service-role
and db-user-role
are in the same
AWS account and teleport-db-service-role
's full ARN is configured as Principal in db-user-role
's
trust policy.
3. Configure Permissions Boundary on teleport-db-service-role
If teleport-db-service-role
does not have an attached
Permissions boundary
then you can skip this step.
Otherwise, the boundary policy attached to teleport-db-service-role
must include
sts:AssumeRole
permissions, for example:
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Resource": "*"
}
]
}
You can test the trust relationship by running this AWS CLI command as teleport-db-service-role
:
aws sts assume-role --role-arn arn:aws:iam::111111111111:role/db-user-role --role-session-name test-trust-relationship
Learn more on how to use trust policies with IAM roles.
Next steps
- Learn how to restrict access to certain users and databases.
- View the High Availability (HA) guide.
- Take a look at the YAML configuration reference.
- See the full CLI reference.