Summer Sale 2026 – NPI EA (cat = Baeldung on Ops)
announcement - icon

Yes, we're now running our only Summer Sale. All Courses are 30% off until 20th July, 2026:

>> EXPLORE ACCESS NOW

Baeldung Pro – Ops – NPI EA (cat = Baeldung on Ops)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

1. Overview

Understanding the instance type of an AWS EC2 is essential for managing resources, optimizing costs, and troubleshooting performance issues. The instance type determines the computing power, memory, storage, and network capacity of the virtual machine. It’s important to know how to retrieve this information directly from the command line, especially when working with automation scripts or managing multiple EC2 instances.

In this tutorial, we’ll examine ways to fetch the EC2 instance type using the AWS Command Line Interface (CLI).

2. Why Knowing the Instance Type Matters

Instance types play a big role in both the performance and cost of running applications in the cloud. They’re optimized for different use cases, like compute-intensive, memory-optimized, or storage-focused operations. Knowing our instance type makes it easier to scale efficiently and decide if switching to a different type could help optimize resources or cut costs. This information is also key when troubleshooting issues or setting up monitoring systems.

Before diving into commands, we should have the AWS CLI installed and configured with the necessary permissions. The commands assume access to AWS resources through the command line.

We can verify the AWS CLI installation by running the following command:

$ aws --version
aws-cli/2.5.1 Python/3.8.8 Linux/4.15.0-136-generic exe/x86_64.ubuntu.20

This output shows that we have the CLI installed. However, if not installed, we can use the apt install command:

$ sudo apt install awscli

Once installed, let’s configure the CLI with this command:

$ aws configure

It prompts for AWS Access Key ID, Secret Access Key, region, and output format. Once we provide these requirements, our environment is ready.

3. Retrieving Instance Type Using the describe-instances Command

The AWS CLI provides us with a straightforward way to get the instance type for EC2 instances. The describe-instances command is particularly useful for retrieving detailed information about our instances.

To get the instance type for a specific instance, let’s use the following command:

$ aws ec2 describe-instances --instance-ids i-1234567890abcdef0 --query "Reservations[].Instances[].InstanceType" --output text
t2.small

We use the –query parameter to ensure that only the instance type is returned rather than the entire instance’s details, while the –output text flag formats the result in plain text. This command is useful for automation scripts where retrieving only specific fields like the instance type is necessary.

Assuming we have environments with multiple EC2 instances, it’s often necessary to retrieve the instance type for more than one machine at once. We can achieve this by passing multiple instance IDs to the describe-instances command. Let’s get the instance type for two of our instances:

$ aws ec2 describe-instances --instance-ids i-1234567890abcdef0 i-abcdef01234567890 --query "Reservations[].Instances[].InstanceType" --output text
t2.small
t3.large

This is especially useful when we are managing large fleets of EC2 instances and helps in quickly assessing the types used across different services.

4. Using Instance Metadata to Retrieve the Instance Type

When running the command directly from within an EC2 instance, another approach is to query the instance metadata. AWS provides instance metadata that includes useful information, such as instance IDs, public and private IP addresses, and, most importantly, the instance type.

Let’s run the following command directly from the EC2 instance to retrieve its instance type:

$ curl http://169.254.169.254/latest/meta-data/instance-type
t2.micro

This IP address 169.254.169.254 is reserved for accessing instance metadata within an AWS environment. This method is very easy to use and requires no authentication, it provides us immediate access to instance-specific information.

5. Automating Instance Type Retrieval

For environments with numerous EC2 instances, we may want to fetch our instance types. This process can be simplified through automation. By combining AWS CLI with scripting tools like Python or Bash, we can create custom scripts to handle the task.

Here’s a simple example in Bash to retrieve and print the instance type for all instances in a specific AWS region:

#!/bin/bash
region="us-west-1"
instance_ids=$(aws ec2 describe-instances --region $region --query "Reservations[].Instances[].InstanceId" --output text)

for instance_id in $instance_ids
do
  instance_type=$(aws ec2 describe-instances --instance-ids $instance_id --query "Reservations[].Instances[].InstanceType" --output text)
  echo "Instance ID: $instance_id, Instance Type: $instance_type"
done

This script first retrieves all instance IDs in the AWS region we specified and then loops through them, fetching the instance type for each one. It’s particularly useful in environments where we may be required to regularly audit or monitor instance types across various services.

6. Conclusion

In this article, we learned how to get the EC2 instance type through the AWS CLI. This information can help us in optimizing our workloads, controlling costs, and maintaining application performance. In automation scripts, knowing the instance type can help make informed decisions based on available resources, ensuring that applications run optimally in any environment.