44

Do you guys know if we can display the EC2 instance type using command line?

Currently, I only have access to the command line of an EC2 instance. Is there a command line that I can type to display the type of instance. eg, p2.8xLarge or g.16x etc.

4 Answers 4

77

Yes - you can use the meta-data endpoint to retrieve information about your EC2 instance type via the command line. This command will work if you are using IMDS v1:

> curl http://169.254.169.254/latest/meta-data/instance-type
t1.micro

But note that IMDS v2 is now the default! Running the above command on an instance that only allows v2 will result in "401 - Unauthorized".

See this page for more information about the available commands.

If interacting with IMDS v2, you would issue these commands instead:

TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"`
curl -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/instance-type
Sign up to request clarification or add additional context in comments.

1 Comment

curl: (7) Failed to connect to <> port 80: Connection refused. Despite querying it form within the instance.
25

To get the EC2 instance type, run one of these commands depending on your Linux distribution:

Amazon Linux:

ec2-metadata --instance-type

Ubuntu:

ec2metadata --instance-type

If neither ec2-metadata nor ec2metadata is available, fall back to this:

wget -q -O - http://169.254.169.254/latest/meta-data/instance-type

All commands will output just the instance type value, like t2.micro.

6 Comments

This works for IMDS v2. It should now be the accepted answer.
does not work for me, I get Command 'ec2-metadata' not found
For me the command was ec2metadata
I get a ec2metadata: command not found on a t3a.2xlarge instance, so I'm not sure this is a good answer :(
This worked for me: ec2-metadata | grep "instance-type"
|
2

Once you get access to ec2. Type:

ec2-metadata | grep "instance-type"

Comments

1
ec2-metadata -t | cut -d ' ' -f 2

1 Comment

While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.