RDS IAM authentication fails: `FATAL: PAM authentication failed for user "app_iam"`.
AdvancedTroubleshootingRDS, Aurora and DynamoDB
Answer
Likely causes → The token was generated for a different host, port, user or Region, or it is older than 15 minutes. The DB user is missing the rds_iam role (PostgreSQL) or AWSAuthenticationPlugin (MySQL). The IAM policy has no rds-db:connect on the right dbuser ARN (it uses the DbiResourceId, not the instance name). Or TLS is not used.
Diagnose →
aws rds describe-db-instances --db-instance-identifier mydb --query 'DBInstances[].[DbiResourceId,IAMDatabaseAuthenticationEnabled]'
TOKEN=$(aws rds generate-db-auth-token --hostname mydb.abc.us-east-1.rds.amazonaws.com --port 5432 --username app_iam --region us-east-1)
psql "host=mydb.abc.us-east-1.rds.amazonaws.com user=app_iam dbname=orders sslmode=require password=$TOKEN"
Fix →
- Run
GRANT rds_iam TO app_iam; - Policy resource:
arn:aws:rds-db:us-east-1:111111111111:dbuser:db-ABCDEFGH123/app_iam - Generate the token right before you connect, and always use TLS
Prevent → Wrap token generation in the connection factory, and keep the IAM auth rate limits in mind (use RDS Proxy at high connection rates).
More on RDS, Aurora and DynamoDB
The app cannot connect to RDS: `psql: error: connection to server at "mydb.abc.us-east-1.rds.amazonaws.com" (10.0.3.12), port 5432 failed: Connection timed out`.Beginner`FATAL: password authentication failed for user "postgres"` and nobody knows the master password.Beginner`FATAL: no pg_hba.conf entry for host "10.0.1.25", user "app", database "orders", no encryption`.IntermediateApp logs show `FATAL: remaining connection slots are reserved for non-replication superuser connections` (or MySQL `ERROR 1040 (HY000): Too many connections`).IntermediateRDS status shows `storage-full` and writes fail.IntermediateA read replica's `ReplicaLag` keeps growing and reports show stale data.Intermediate