MANISHDAN LANGA
MANISHDAN LANGA

Reputation: 2237

Find the OS name for EC2 AWS Using SDK

I am trying to fine OS name using AWS SDK ,I tried using describe instances:

ec2.describe_instances();

I able to collect the all instances but not able to get the OS name for Linux instances, any other way to get OS name for Linux instance.

Upvotes: 2

Views: 460

Answers (2)

Jedi
Jedi

Reputation: 3358

As @helloV says, the OS information is not provided by the describe instances command. You could, at a stretch, fetch it by querying the metadata from the instance's AMI.

Note: this script is hacky and could be easily improved (edits appreciated)

set -f; IFS=$'\n' # split array on newlines
for  x in $(IFS=$'\n' aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId,ImageId]' --output text); do instance=$(echo $x | cut -f1); ami=$(echo $x | cut -f2); echo $instance $ami $(aws ec2 describe-images --image-ids $ami --query Images[*].[Name] --output text); done;
set +f; unset IFS # reset separator

If you are using AMIs that specify the base image, you should see something like:

i-abd94ccc ami-a73264ce ubuntu/images/ebs/ubuntu-precise-12.04-amd64-server-20131003
i-ea827c52 ami-abcdefgh Custom Secret Image
i-71ccf280 ami-9eaa1cf6 ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-20140927
i-cb72a300 ami-08ab2f65 k8s-1.3-debian-jessie-amd64-hvm-ebs-2016-06-18
i-2aaed139 ami-e3c3b8f4 ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-20160922

Upvotes: 2

John Rotenstein
John Rotenstein

Reputation: 270294

You can display the platform of an instance:

$ aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId, Platform]' --output text
i-0c9c9494b3b83afdc None
i-0ef635853b32f705e windows
i-3da613a2  None
i-5d261c40  None
i-8daa32d2  windows
i-292b91c7  None

If the instance is Windows, then the value is windows, otherwise it is null.

There is no way to obtain the OS Name (eg Redhat, Suse) -- for that, you could examine the name of the AMI used to launch the instance.

Upvotes: 1

Related Questions