AWS
CLI
Account Number
Cloud Computing
AWS Tools

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.

Practice system design

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:

  1. AWS CLI Installed: Follow the official guide to install AWS CLI if you haven't already.
  2. AWS CLI Configured: You should have your AWS CLI configured with the necessary credentials and default region. Use the command aws configure to 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:

bash
aws sts get-caller-identity --query "Account" --output text

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. The text format provides the account number directly without additional JSON formatting.

Example

Suppose you execute the command, the output would look something like this:

 
123456789012

This is your AWS Account Number.

Key Points Summary

The following table summarizes the critical details and options used in the command:

Key ComponentDescriptionExample Usage
stsService that returns details about the requesteraws sts get-caller-identity
--query "Account"Query option to extract specific information from the response--query "Account"
--outputFormat of the output. Common options include json, text, and table--output text
OutputThe Account ID is displayed in the simplest form without any JSON structure123456789012

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:

bash
#!/bin/bash
aws sts get-caller-identity --query "Account" --output text

Don't forget to give the script execute permissions using the command:

bash
chmod +x get-aws-account-number.sh

Then you can easily retrieve the account number by running:

bash
./get-aws-account-number.sh

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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.