I have been working on a Cloud data protection application which would be running on AWS Cloud EC2 instance, for some information processing I need to get the instance id of my Amazon AWS EC2 instance which my software is running.
Instance metadata is data about your instance that you can use to configure or manage the running instance. Instance metadata is divided into categories, there are multiple ways to get the instance id and other user data and meta data of the AWS EC2 instance within that EC2 instance.
In Linux (Ubuntu ) terminal you can use ec2metadata which gives the metadata of the instance, to get only the instance id you can use ec2metadata –instance-id
In case of Amazon Linux ec2-metadata -i
Alternatively, you can use HTTP call
Which will give you the instance id of your instance,
You can get more metadata and user data using the CLI or HTTP call, and you are not billed for HTTP requests used to retrieve instance metadata and user data.
EC2 Instance Metadata Query Tool,
Download the tool from http://s3.amazonaws.com/ec2metadata/ec2-metadata
$ wget http://s3.amazonaws.com/ec2metadata/ec2-metadata
Make the file executable and try ec2-metadata –help for help
–all Show all metadata information for this host (also default).
-a/–ami-id The AMI ID used to launch this instance
-l/–ami-launch-index The index of this instance in the reservation (per AMI).
-m/–ami-manifest-path The manifest path of the AMI with which the instance was launched.
-n/–ancestor-ami-ids The AMI IDs of any instances that were rebundled to create this AMI.
-b/–block-device-mapping Defines native device names to use when exposing virtual devices.
-i/–instance-id The ID of this instance
-t/–instance-type The type of instance to launch. For more information, see Instance Types.
-h/–local-hostname The local hostname of the instance.
-o/–local-ipv4 Public IP address if launched with direct addressing; private IP address if launched with public addressing.
-k/–kernel-id The ID of the kernel launched with this instance, if applicable.
-z/–availability-zone The availability zone in which the instance launched. Same as placement
-c/–product-codes Product codes associated with this instance.
-p/–public-hostname The public hostname of the instance.
-v/–public-ipv4 NATted public IP Address
-u/–public-keys Public keys. Only available if supplied at instance launch time
-r/–ramdisk-id The ID of the RAM disk launched with this instance, if applicable.
-e/–reservation-id ID of the reservation.
-s/–security-groups Names of the security groups the instance is launched in. Only available if supplied at instance launch time
-d/–user-data User-supplied data.Only available if supplied at instance launch time.
Note: Although you can only access instance metadata and user data from within the instance itself, the data is not protected by cryptographic methods. Anyone who can access the instance can view its metadata. Therefore, you should take suitable precautions to protect sensitive data (such as long-lived encryption keys). You should not store sensitive data, such as passwords, as user data.
** All the IP, Instance ID in my GIST view are changed to random value for security purpose