AWS IAM Authentication
SQL Exporter can connect to Amazon RDS and Aurora (PostgreSQL, MySQL) with IAM database authentication instead of a static password. It's supported for PostgreSQL and MySQL.
Query parameters for IAM authentication:
aws-iam-auth=trueto authenticate with an IAM auth token (required);aws-iam-region=<AWS_REGION>if SQL Exporter runs in a different region than the database, or environment has no region configured (optional).aws-iam-role-arn=<ROLE_ARN>to assume an IAM role and sign the token with its credentials, e.g. for a database in another account (optional).
The connection string would look like this:
# Without TLS verification
postgres://user@mydb.abcdefgh.eu-west-1.rds.amazonaws.com:5432/dbname?sslmode=require&aws-iam-auth=true
mysql://user@mydb.abcdefgh.eu-west-1.rds.amazonaws.com:3306/dbname?tls=skip-verify&aws-iam-auth=true
# With TLS verification
postgres://user@mydb.abcdefgh.eu-west-1.rds.amazonaws.com:5432/dbname?sslmode=verify-full&sslrootcert=/path/to/global-bundle.pem&aws-iam-auth=true
mysql://user@mydb.abcdefgh.eu-west-1.rds.amazonaws.com:3306/dbname?tls=custom&tls-ca=/path/to/global-bundle.pem&aws-iam-auth=truesslmode=require and tls=skip-verify encrypt the connection, but don't verify the server certificate. sslmode=verify-full and tls=custom (see MySQL Custom Certificates) also verify it with the RDS certificate bundle.
AWS credentials are taken from the default credential chain. The identity needs the rds-db:connect permission for the database user. With aws-iam-role-arn, the identity needs sts:AssumeRole on the role instead, and the role needs rds-db:connect. A token is valid for 15 minutes, so SQL Exporter generates a new one for every new database connection.
A few things to keep in mind:
- Only
aws-iam-auth=trueenables IAM authentication. To disable it, remove the parameter instead of setting it tofalse. - For PostgreSQL, the connection is always opened with the
pgxdriver, for every scheme (postgres://,postgresql://,pg://,pgsql://,pgx://).lib/pqis not used for IAM authentication. - A password in the connection string is rejected, as the token is used as the password.
- For PostgreSQL, multiple hosts in the connection string (
host1,host2) are rejected, as the token is signed for a single host. - AWS IAM Authentication has some restrictions:
- Host can't be an IP address or a custom DNS record.
- TLS is required: see
sslmodefor PostgreSQL,tlsfor MySQL. SQL Exporter rejects a connection string that can connect without TLS:sslmodehas to berequire,verify-caorverify-full(not the defaultprefer), andtlshas to betrue,skip-verifyorcustom(notpreferred). - For MySQL, the token is sent as a cleartext password, so
allowCleartextPasswordsis enabled automatically. - For MySQL,
tls=truechecks the server certificate against the system CA certificates, and the Amazon RDS CA is usually not one of them (x509: certificate signed by unknown authority). Usetls=skip-verify, ortls=customwithtls-ca(see MySQL Custom Certificates) instead. - The database user has to be set up for IAM authentication:
- Postgres:
rds_iam - MySQL:
AWSAuthenticationPlugin
- Postgres:
Example setup
PostgreSQL
Enable IAM database authentication on the DB instance (RDS) or on the DB cluster (Aurora).
Create the database user and
GRANTit therds_iamrole:sqlCREATE USER monitoring; GRANT rds_iam TO monitoring;WARNING
A user with the
rds_iamrole can no longer log in with a password, as IAM authentication takes precedence. Don't grant it to the master user, or to a user that something else connects as with a password.Give the necessary IAM policy to the role SQL Exporter uses (ECS task role, EC2 instance profile, etc.):
json{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "rds-db:connect", "Resource": "arn:aws:rds-db:eu-west-1:123456789012:dbuser:cluster-ABCDEFGHIJKL01234/monitoring" } ] }The resource ID in the ARN is the cluster resource ID (
cluster-...) for Aurora, and the instance resource ID (db-...) for an RDS instance.Use the connection string without a password:
yamltarget: data_source_name: 'postgres://monitoring@mydb.cluster-abcdefgh.eu-west-1.rds.amazonaws.com:5432/postgres?sslmode=require&aws-iam-auth=true'To also verify the server certificate, download the RDS certificate bundle and set it in the connection string:
shellcurl -o /path/to/global-bundle.pem https://truststore.pki.rds.amazonaws.com/global/global-bundle.pemyamltarget: data_source_name: 'postgres://monitoring@mydb.cluster-abcdefgh.eu-west-1.rds.amazonaws.com:5432/postgres?sslmode=verify-full&sslrootcert=/path/to/global-bundle.pem&aws-iam-auth=true'
MySQL
Enable IAM database authentication on the DB instance (RDS) or on the DB cluster (Aurora).
Create the database user with the
AWSAuthenticationPlugin:sqlCREATE USER 'monitoring'@'%' IDENTIFIED WITH AWSAuthenticationPlugin AS 'RDS';WARNING
A user created with the
AWSAuthenticationPlugincan only log in with an IAM auth token, not with a password. Don't change the master user, or a user that something else connects as with a password, to this plugin.Give the necessary IAM policy to the role SQL Exporter uses (ECS task role, EC2 instance profile, etc.):
json{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "rds-db:connect", "Resource": "arn:aws:rds-db:eu-west-1:123456789012:dbuser:db-ABCDEFGHIJKL01234/monitoring" } ] }The resource ID in the ARN is the instance resource ID (
db-...) for an RDS instance, and the cluster resource ID (cluster-...) for Aurora.Use the connection string without a password:
yamltarget: data_source_name: 'mysql://monitoring@mydb.abcdefgh.eu-west-1.rds.amazonaws.com:3306/dbname?tls=skip-verify&aws-iam-auth=true'To also verify the server certificate, download the RDS certificate bundle and set it in the connection string:
shellcurl -o /path/to/global-bundle.pem https://truststore.pki.rds.amazonaws.com/global/global-bundle.pemyamltarget: data_source_name: 'mysql://monitoring@mydb.abcdefgh.eu-west-1.rds.amazonaws.com:3306/dbname?tls=custom&tls-ca=/path/to/global-bundle.pem&aws-iam-auth=true'tls=customandtls-caare the parameters of MySQL Custom Certificates.
