Sub topic 6
Connecting and managing credentials
This chapter assumes you have provisioned a private RDS instance in a VPC, as in the previous chapter. You should also be comfortable with security groups and IAM roles.
The endpoint
Each DB instance gets a DNS name called an endpoint, such as demo-pg.example.us-east-1.rds.amazonaws.com. Always connect through this name and never through a cached IP address. After a Multi-AZ failover, RDS repoints the name to the new primary, and the IP address changes.
Because the instance was created with --no-publicly-accessible, the endpoint resolves to a private IP inside the VPC. A client must therefore run inside the VPC or reach it through a VPN, Direct Connect, or a bastion host. The instance's security group must also allow inbound traffic on the database port from the client's security group.
Managed master credentials
The earlier create-db-instance call used --manage-master-user-password. RDS then generates the master password and stores it in AWS Secrets Manager. It also rotates the password automatically, every seven days by default. The password never appears in your shell history or your infrastructure code.
The following script waits for the instance and prints the endpoint and the secret's ARN:
import boto3
rds = boto3.client("rds")
rds.get_waiter("db_instance_available").wait(DBInstanceIdentifier="demo-pg")
instance = rds.describe_db_instances(DBInstanceIdentifier="demo-pg")["DBInstances"][0]
endpoint = instance["Endpoint"]
print(endpoint["Address"], endpoint["Port"])
print(instance["MasterUserSecret"]["SecretArn"])To read the secret, the caller needs secretsmanager:GetSecretValue on that ARN. The secret is JSON with username and password fields:
aws secretsmanager get-secret-value \
--secret-id "<secret-arn>" \
--query SecretString --output textFetch the secret at connection time instead of copying the password into configuration. That way your application keeps working after a rotation.
Connecting over TLS
Download the RDS CA bundle (global-bundle.pem) and use verify-full. This encrypts the session and also checks that the server certificate matches the hostname:
psql "host=demo-pg.example.us-east-1.rds.amazonaws.com port=5432 dbname=postgres user=appadmin sslmode=verify-full sslrootcert=global-bundle.pem"psql prompts for the password. Paste the value from the secret.
Other authentication options
The master user is meant for administration. For applications, create separate database users with limited privileges. RDS also supports IAM database authentication on several engines, including PostgreSQL and MySQL. The client calls aws rds generate-db-auth-token and uses the result as a password that expires after 15 minutes. This removes stored passwords, but it limits the rate of new connections, so it suits low-churn workloads better than bursty ones.
Common errors
| Symptom | Likely cause |
|---|---|
| Connection times out | The security group has no inbound rule for the client, or the client has no network route to the VPC |
| `password authentication failed` | The secret was rotated and the client cached the old value, or the username is wrong |
| `certificate verify failed` | The CA bundle is missing or outdated, or the hostname does not match the endpoint |
| `no pg_hba.conf entry` | The server requires SSL and the client connected without it |
Exercise
Your application connects successfully for a week, then starts failing with "password authentication failed" for the master user. Nothing in the security group changed. What is the most likely cause and the fix?
Show answer
The managed master password was rotated, and the application cached the old one. Read the secret from Secrets Manager each time you open a connection, or refresh it when authentication fails, instead of storing the password at deploy time.