Quick way to get AWS Account number from the AWS CLI tools?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
The AWS Account Number is a 12-digit identifier that uniquely identifies your AWS account. It's essential in a variety of scenarios, such as when you want to set up a reliable cross-account AWS Identity and Access Management (IAM) access, create API calls, or when configuring certain AWS services. Retrieving this account number programmatically can save time and reduce the possibility of manual errors. In this article, we'll outline a quick and efficient way to obtain your AWS Account Number using AWS CLI tools.
Prerequisites
Before proceeding, ensure that you have:
- AWS CLI Installed: Follow the official guide to install AWS CLI if you haven't already.
- AWS CLI Configured: You should have your AWS CLI configured with the necessary credentials and default region. Use the command
aws configureto set up your CLI.
Steps to Retrieve AWS Account Number
The simplest method to retrieve your AWS Account Number is by using the AWS CLI to call the sts get-caller-identity command. This command returns details about the IAM user or role whose credentials are used to call the operation. Among the details returned is the AWS Account Number.
Command and Explanation
To get the AWS account number, execute the following command in your terminal:
Here's a breakdown of the command:
aws: The base command for the AWS CLI tool.sts get-caller-identity: Calls the Security Token Service (STS) API to return details about the IAM user or role, including the account number.--query "Account": This option allows you to filter and display only the "Account" field from the response.--output text: Specifies the output format. Thetextformat provides the account number directly without additional JSON formatting.
Example
Suppose you execute the command, the output would look something like this:
This is your AWS Account Number.
Key Points Summary
The following table summarizes the critical details and options used in the command:
| Key Component | Description | Example Usage |
sts | Service that returns details about the requester | aws sts get-caller-identity |
--query "Account" | Query option to extract specific information from the response | --query "Account" |
--output | Format of the output. Common options include json, text, and table | --output text |
| Output | The Account ID is displayed in the simplest form without any JSON structure | 123456789012 |
Additional Details and Subtopics
Security Implications
When using the AWS CLI to fetch information like the AWS Account Number, ensure that your credentials and configurations are securely stored. Use the AWS access key and secret key responsibly, and never share them publicly or through unsecured platforms.
Automating With Scripts
If you frequently need your AWS Account Number in shell scripts or automation tasks, you can store the command in a function or script. For example, creating a bash script named get-aws-account-number.sh may look like:
Don't forget to give the script execute permissions using the command:
Then you can easily retrieve the account number by running:
Conclusion
Retrieving your AWS Account Number using the AWS CLI is a straightforward process that can save time and minimize manual errors, especially when managing AWS environments programmatically. This method, while simple, is efficient for both individual use and within automated scripts and pipelines. Being able to quickly access this fundamental piece of information empowers you to better manage your AWS infrastructure and services.
Related reading
- R and data.table on AWS
- RabbitMQ on EC2 Consuming Tons of CPU
- Rails based EC2 AMI
- Rancher with cattle vs Rancher with Kubernetes vs Standalone Kubernetes
- Random status code 502 errors on AWS api gateway connected to lambda
- RDS endpoint name format
- RDS with Cloud Formation and AZ issues
- react router doesn't work in aws s3 bucket

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.