- Newest
- Most votes
- Most comments
hi,
in account A python service (your compute R1 role ) is running need to access account B using IAM auth by assuming role R2 (RDS MySQL)
role R1 needs assume role R2 with temp credential to generate DB auth token.
so, in account A role R1 needs permission to assume Role R2 in account B.
{
"Effect": "Allow",
"Action": "sts:AssumeRole", [Not DB connect it needs assume role in account B role R2]
"Resource": "arn:aws:iam::<Account-A2-ID>:role/R2"
}
rest code designed looks good to me.
for cross-account authentication. https://docs.aws.amazon.com/IAM/latest/UserGuide/tutorial_cross-account-with-roles.html https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.html
Hi @Malini, Permission is already there, But we dont have iam user, we have a service account that has access to role, which is further trying to assume a role. The problem, I thinks is somehow boto is unable to take the assumed role session for connecting with db. But, I am not sure how to fix it.
hi,
so, back to your original question "how to correctly connect to the Amazon RDS MySQL database in Account A2 from a service in Account A1 using IAM role-based authentication with Boto3 in Python"
Your service in Account A1 needs to assume the IAM role R2 in Account A2 to gain temporary credentials with the necessary permissions to access the RDS database.
so, in you code
"credentials = self.assume_role(aws_arn)"
you assume_role method is correctly calling AssumeRole API
and in account A1 has permission to assume role in account A2?
the trust policy of role in account A2 allows the entity in A1 to assume role?
you code is using classes, you need to know A1 service role has permission to assume role in account A2.
best, Malini
hi, as you mentioned plugin i looked in github bellow link:->
https://github.com/aws/aws-advanced-python-wrapper/blob/main/docs/examples/PGIamAuthentication.py
YOUR CODE:
#Using above token to connect to db in below code
AwsWrapperConnection.connect( connect, [ this variable?? if u r using mysql.connector mysql.connector.connect (github uses PostgreSQL) ] host=host, port=port, database=db_name, user=username, password=token, [ [don't pass password/token explicitly, the wrapper should handle] ssl_disabled=False, client_flags=[ClientFlag.SSL], plugins='iam', wrapper_dialect='aurora-mysql' )
Best, hope this will help.
hi again,
"assumed role cred in AwsWrapperConnection.connect method" print out what AWS SDK /Wrapper is using as credentials:->
[ import boto3 session = boto3.Session() creds = session.get_credentials().get_frozen_credentials()
print("Wrapper is using these creds:") print("Access Key:", creds.access_key) print("Secret Key:", creds.secret_key[:5] + "***")
]
if internal wrapper:
look inside the source ---boto3.session() and print it.
if awswrapperconnection is using the default session, instead of assumed role credentials. than modifying your wrapper to accept a session. hope this help a bit. best