I have an AWS_ACCESS_KEY_ID and an AWS_SECRET_KEY. These are active credentials, so they belong to an active user, who belongs to an AWS Account. How, using Boto3, do I fi
Something like this will work:
import boto3
ACCESS_KEY = 'FOO'
SECRET_KEY = 'BAR'
iam = boto3.resource('iam',
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY,
)
account_id = iam.CurrentUser().arn.split(':')[4]
print account_id
If you use EC2 IAM roles, you can omit all of the access/secret key stuff and the code becomes simply:
iam = boto3.resource('iam')
account_id = iam.CurrentUser().arn.split(':')[4]