Is it possible to get aws account id with only aws access key and secret key in command line (CLI)
This is the correct way:
~ $ aws sts get-caller-identity
{
"Account": "123456789012",
"UserId": "AIDABCDEFGHJKL...",
"Arn": "arn:aws:iam::123456789012:user/some.user"
}
It works for IAM Users, Cross-account IAM Roles, EC2 IAM Roles, etc.
Use together with jq
to obtain just the account id:
~ $ aws sts get-caller-identity | jq -r .Account
123456789012