Interview Prep/AWS/RDS, Aurora and DynamoDB

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 →

  1. Run GRANT rds_iam TO app_iam;
  2. Policy resource: arn:aws:rds-db:us-east-1:111111111111:dbuser:db-ABCDEFGH123/app_iam
  3. 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).

aws All AWS questions Roadmap: AWS core services

More on RDS, Aurora and DynamoDB