Sreeragam
Sreeragam

Reputation: 11

how to get instance name, number of cpu, cores and operating system info of EC2 instance

I am writing Java code to retrieve below info of an EC2 instance? But not sure about the right AWS API to use to get these info.

  1. instance name
  2. number of cpu
  3. number of virtual processor cores
  4. operating system version, environment

Upvotes: 1

Views: 1661

Answers (1)

Julio Faerman
Julio Faerman

Reputation: 13501

You can get that from the instance metadata, as described at https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html

Note that this is an HTTP service hosted locally for each instance at 169.254.169.254, you can access with java http clients or directly, for example:

  1. instance name
$ curl http://169.254.169.254/latest/meta-data/instance-id
i-024a0de14f70ab64f
  1. number of cpu
  2. number of virtual processor cores

These are defined by the instance-type:

$ curl http://169.254.169.254/latest/meta-data/instance-type
t3.2xlarge
  1. operating system operating system version, environment?

This is defined by the image, and the details can be fetched from the describe-images api

$ aws ec2 describe-images \
--image-ids $(curl -s http://169.254.169.254/latest/meta-data/ami-id)
{
    "Images": [
        {
            "VirtualizationType": "hvm", 
            "Description": "Cloud9 Cloud9Default AMI", 
            "Hypervisor": "xen", 
            "EnaSupport": true, 
            "SriovNetSupport": "simple", 
            "ImageId": "ami-07606bae9eee7051c", 
            "State": "available", 
            "BlockDeviceMappings": [
                {
                    "DeviceName": "/dev/xvda", 
                    "Ebs": {
                        "SnapshotId": "snap-0ee3e3de47cfb2ce4", 
                        "DeleteOnTermination": true, 
                        "VolumeType": "gp2", 
                        "VolumeSize": 8, 
                        "Encrypted": false
                    }
                }
            ], 
            "Architecture": "x86_64", 
            "ImageLocation": "751997845865/Cloud9Default-2019-02-18T10-14", 
            "RootDeviceType": "ebs", 
            "OwnerId": "751997845865", 
            "RootDeviceName": "/dev/xvda", 
            "CreationDate": "2019-02-18T11:02:13.000Z", 
            "Public": true, 
            "ImageType": "machine", 
            "Name": "Cloud9Default-2019-02-18T10-14"
        }
    ]
}

Upvotes: 1

Related Questions