Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the instance Name from the instance in AWS?

I'm trying to set up a means to register an instance in route53 automatically when the instance is created, using salt and this article.

The article uses ec2-metadata to get the instance-id and and the hostname. I'm wondering if there is a way, using bash within the instance, to get the instance Name instead. ec2-metadata only seems to show the instance-id. Thanks in advance.

like image 249
numb3rs1x Avatar asked Aug 20 '13 15:08

numb3rs1x


People also ask

How do I find out my instance?

In WindowsHold the Windows key and press R. At the run dialog, type in cmd and press enter. After a command prompt appears, type in nslookup example.my.salesforce.com (replace example with your My Domain). A few lines down you will see a line that starts with "Name:" The naXX afterwards will be your instance name.

How do you give an instance name?

In 2021 this can be done from the AWS console, by going to EC2 > Instances, clicking the instance ID, then in the bottom panel clicking on the Tags tab, and clicking Manage tags. From there, you can simply change the value of the Name tag then click Save to apply.

How do I find my AWS server name?

Choose Settings > Connections, and then choose the Hosts tab. Choose the button next to the host you want to view, and then choose View details. The following information appears for your host: The host name.

How to describe a specific instance by Instance ID using aws cli?

Example5: Describe a Specific instance by Instance ID using AWS CLI EC2 Sometimes all you want to know is a configuration of a Single instance and if you have the instance-id with you. you can do it easily with aws ec2 describe-instances command line. Here is the command, The --instance-ids parameter can accept one or more instance ids.

How to get the Instance ID of an EC2 instance?

aws ec2 describe-instances --filters 'Name=tag:Name,Values=dev-server-*' If you just wanted the instance ids of those instances, you could use: aws ec2 describe-instances --filters 'Name=tag:Name,Values=dev-server-*' --output text --query 'Reservations 1 ].Instances 1.1 ].InstanceId'

Where do I find the instance name on Amazon Connect?

On the instances page, the instance name appears in the Instance Alias column. This instance name appears in the URL you use to access Amazon Connect. Did this page help you?

What is the default user name for an Amazon Linux instance?

The default user name is determined by the AMI that was specified when you launched the instance. For Amazon Linux 2 or the Amazon Linux AMI, the user name is ec2-user.


2 Answers

First, you need to get the instance-id.

AWS_INSTANCE_ID=`curl -s http://169.254.169.254/latest/meta-data/instance-id` 

Than you can get the ec2 instance name using below command.

EC2_NAME=$(aws ec2 describe-tags --region $REGION --filters "Name=resource-id,Values=$AWS_INSTANCE_ID" "Name=key,Values=Name" --output text | cut -f5) 

Please ensure that you have AWS Cli Installed.

I hope this helps. Thanks!

like image 156
Jayesh Dhandha Avatar answered Sep 21 '22 10:09

Jayesh Dhandha


First and foremost, the Amazon EC2 Instance Metadata Service also provides quite some other Names besides the instance-id, if these might be what you are looking for - see Instance Metadata Categories:

  • hostname - The private hostname of the instance. In cases where multiple network interfaces are present, this refers to the eth0 device (the device for which the device number is 0).
  • local-hostname - The private DNS hostname of the instance. In cases where multiple network interfaces are present, this refers to the eth0 device (the device for which the device number is 0).
  • public-hostname - The instance's public DNS. If the instance is in a VPC, this category is only returned if the enableDnsHostnames attribute is set to true.

If you are looking for the Name as exposed in the AWS Management Console though, you would indeed need to resort to using one of the Tools for Amazon Web Services to retrieve it - that Name is in fact just a regular tag with the key Name (see Tagging Your Amazon EC2 Resources), which happens to be used across most AWS services for the obvious purpose.

Here's how to get it with the AWS Command Line Interface for example (skipping region and credentials):

aws ec2 describe-tags \ --filters Name=resource-id,Values=i-abcd1234 Name=key,Values=Name \ --query Tags[].Value --output text 
  • For more advanced CLI JSON output processing than what's possible with the built in --query option, you could resort to jq (a lightweight and flexible command-line JSON processor).

  • Overthink's answer provides an example based on the now legacy Amazon EC2 API Tools (please note the comments, which correctly point out that you'd nowadays deal with credentials differently, see Tell the CLI Tools Who You Are and IAM Roles for EC2 instances for details).

like image 35
Steffen Opel Avatar answered Sep 22 '22 10:09

Steffen Opel