Find AWS Account Id using CLI or Boto3
How to find the AWS Account ID using the AWS CLI or Boto3?
AWS CLI
Retrieve your full identity
1aws sts get-caller-identity
1{
2 "UserId": "AIDA...",
3 "Account": "123456789012",
4 "Arn": "arn:aws:iam::123456789012:user/user"
5}
Use native AWS CLI --query
option for querying the output.
1aws sts get-caller-identity --query "Account" --output text
1123456789012
Python Boto3
1import boto3
2
3sts = boto3.client("sts")
4account_id = sts.get_caller_identity()["Account"]
5print(account_id) # 0123456789012