I am trying to connect to Amazon Aurora using SQLAlchemy using an SSL connection, specifying the IAM role as the database user account and the authentication token as the password, as described here in [AWS Documents] ( http://docs.aws.amazon) ..com / AmazonRDS / latest / UserGuide / UsingWithRDS.IAMDBAuth.html # UsingWithRDS.IAMDBAuth.Connecting )
Here are the steps I followed.
wget https:
export LIBMYSQL_ENABLE_CLEARTEXT_PLUGIN=1
aws rds generate-db-auth-token --hostname 'datadbcluster-1.cluster-xxxxxxxxxxxx.us-west-2.rds.amazonaws.com' --port 3306 --username dt_analyst --region us-west-2 > /home/ubuntu/dt_analyst.pem
mysql -h datadbinstance2nd. xxxxxxxxxxxx.us-west-2.rds.amazonaws.com--ssl-ca /home/ubuntu/rds-combined-ca-bundle.pem -u dt_analyst --ssl-verify-server-cert --enable-cleartext-plugin -p'<token>'
I have confirmed that I can connect via SSL using the mysql client.
But I want to connect using sqlalchemy and not mysql client. The following code has been compiled from a dozen tips found on the Internet, but only gives the following error.
'sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (1045, "Access denied to user ...")
My code is as follows.
import boto3
client = boto3.client('rds', region_name='us-west-2')
dialect='mysql'
user = ‘dt_analyst
host = 'datadbcluster-1.cluster-xxxxxxxxxxxx.us-west-2.rds.amazonaws.com'
port = 3306
data = ‘datadb
region='us-west-2'
token = client.generate_db_auth_token(host,port,user,region)
host1 = 'datadbinstance2nd. xxxxxxxxxxxx.us-west-2.rds.amazonaws.com'
conn_str = '%s://%s:%s@%s:%d/%s'%(dialect,user,token,host1,port,data)
conn_str += '?ssl_key=%s'%token
conn_str += '&ssl_cert=/home/ubuntu/rds-combined-ca-bundle.pem'
ssl_args = {
'ssl': {
'ca_cert': '/home/ubuntu/rds-combined-ca-bundle.pem',
'sslmode': 'require',
'verify_ssl_cert': True
}
}
engine = create_engine(conn_str,connect_args=ssl_args, echo=True)
source
share