Julien bulot
Julien bulot

Reputation: 91

Aurora RDS Postgres with IAM Authentication error?

I try to connect on my Aurora Postgres database with IAM user form my local machine but i have the following error :psql: FATAL: PAM authentication failed for user "test-rds"

Command for create user on db :

CREATE USER test-rds WITH LOGIN;
GRANT rds_iam TO test-rds;

I have create this policy and attach it to my IAM user.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "rds-db:connect"
            ],
            "Resource": [
                "arn:aws:rds-db:eu-west-1:$account:dbuser:$db-id/test-rds",
            ]
        }
    ]
}

Then test the following commands :

export PGPASSWORD=$(aws rds generate-db-auth-token --hostname $db-host --port $db-port --username test-rds --region eu-west-1)

psql "host=$db-host port=$db-port sslmode=require sslrootcert=rds-combined-ca-bundle.pem dbname=postgres user=test-rds"

If i try with an postgresql user i can access to my db but not with iam users i don't understand why.

Please help me ^^ !

Upvotes: 4

Views: 5698

Answers (2)

Julien bulot
Julien bulot

Reputation: 91

The problem was solved when I adapted the following command :

export PGPASSWORD=$(aws rds generate-db-auth-token --hostname $db-host --port $db-port --username test-rds --region eu-west-1)

I have add AWS_PROFILE in my command.

export PGPASSWORD=$(AWS_PROFILE=test-rds aws rds generate-db-auth-token --hostname $db-host --port $db-port --username test-rds --region eu-west-1)

Upvotes: 5

Prabhakar Reddy
Prabhakar Reddy

Reputation: 5124

Everything seems to be perfect except the authentication part.Can you generate temporary credentials as shown below and try connecting to database.It should work.

$ creds=$(aws sts assume-role --role-arn arn:aws:iam::ACC-ID:role/testauth --role-session-name test) 

$ echo $creds
{ "AssumedRoleUser": { "AssumedRoleId": "XXXXXXXXXXXXXX:test", "Arn": "arn:aws:sts::XXXXXXXXXXXX:assumed-role/testauth/test" }, "Credentials": { "SecretAccessKey": "+xxxx/XXXXXXXXXXX+g0mdOo7vyfloot07", "SessionToken": "xxxxXIvYXdzEJP//////////XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX+AO0fnzvmfGkd1QWLWdeVSOi4DTHabXYJZeBN6gYltigFnd61koyAMVD4Yw/eCi/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX+XXXXXXXXXXXXXXX+wgs22LnfYwy4Kqhh7NCK7Gk6VXQ518MdlVmFBxyko5ffx3QU=", "Expiration": "2018-10-09T10:59:01Z", "AccessKeyId": "XXXXXXXXXXXXXXXXXXX" } }

Export dynamic variable for the session:
______________________________

$ export AWS_ACCESS_KEY_ID="XXXXXXXXXXXXXXXXXXX"
$ export AWS_SECRET_ACCESS_KEY="+xxxx/kHESRK32Fso+g0mdOo7vyfloot07"
$ export AWS_SESSION_TOKEN="xxxxIvYXdzEJP//////////wEaDO2IFbHyBBMLuswSXiLoARHDVo6yvQnxsKCis62f4nmmmGBMJYXCLWoKSuMcVgn5Aw+xxxxxxxxxxxxxxxxxxxxxxxxxxxxx/eCi/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX+i3OLpjWQw+wgs22LnfYwy4Kqhh7NCK7Gk6VXQ518MdlVmFBxyko5ffx3QU="

Generate Token:
_______________

$ export PGPASSWORD="$(aws rds generate-db-auth-token --hostname=testauth.xxxxxxxxxx.us-west-2.rds.amazonaws.com --port=5432 --username=testcustom --region us-west-2)"

Try Connections( should work):
_______________

$ psql -h testauth.xxxxxxxxxx.us-west-2.rds.amazonaws.com -p 5432 "dbname=testauth user=testcustom sslrootcert=/home/ec2-user/rds-combined-ca-bundle.pem sslmode=verify-ca"
psql (9.2.24, server 10.4)
WARNING: psql version 9.2, server version 10.0.
         Some psql features might not work.
SSL connection (cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256)
Type "help" for help.

Upvotes: 0

Related Questions