ivarrian
ivarrian

Reputation: 137

How to find OS of an EC2 instance using AWS CLI

How can you find out the OS running on an EC2 instance using AWS CLI.

The ec2 describe-instance command spits out a lot of information , but there is nothing indicating the OS .

I also tried ec2 describe-images on a specific image. Again, there doesn't seem to be any indication of OS.

Help..?

Upvotes: 8

Views: 36848

Answers (7)

Pratik Das
Pratik Das

Reputation: 1

Yes, there is a way to get the OS details using aws cli command "aws ec2 describe-instances". Of course, from SSM query you can get the same, but "PlatformDetails" parameter can give you the details

Type aws ec2 describe-instances --profile $PROFILE --region $REGION --instance-ids "$instance_id" --query 'Reservations[ ].Instances[ ].PlatformDetails' --output text

Replace the $PROFILE with your local aws profile, $ REGION with the Region name, and $instance_id with the id of the ec2 instance.

Upvotes: 0

lvthillo
lvthillo

Reputation: 30723

Based on @John Rotenstein answer:

$ aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId,PlatformDetails]' --output text

The difference here is PlatformDetails:

i-01520855c048a1111     Windows
i-03ea279dd83851111     Linux/UNIX
i-091f7c7a4164f1111     Linux/UNIX
...

Upvotes: 0

Manoj Jadhav
Manoj Jadhav

Reputation: 9

If you have a System manager agent installed on your instances then you can get OS details and platform version with

aws ssm describe-instance-information --query 'InstanceInformationList[*].[InstanceId,PlatformName,PlatformVersion]' --output text | sort

Upvotes: 0

Seyeong Jeong
Seyeong Jeong

Reputation: 11028

If you have a System Manager agent install on your instances you can use DescribeInstanceInformation API to find that information:

$ aws ssm describe-instance-information --query 'InstanceInformationList[*].[InstanceId,PlatformType,PlatformName]' --output text | sort

i-016073859e4b31111 Linux   Amazon Linux AMI
i-01fa3efe71e4b1111 Linux   Amazon Linux AMI
i-03d437d24f7341111 Windows Microsoft Windows Server 2012 R2 Standard
i-048fa3ba0aa151111 Windows Microsoft Windows Server 2012 R2 Standard
i-05e27c562eb881111 Linux   Amazon Linux AMI
i-09283c3c05d551111 Windows Microsoft Windows Server 2012 R2 Standard
i-0a51eb40351911111 Linux   Amazon Linux AMI
i-0a5aeab8f56ba1111 Linux   Amazon Linux AMI
i-0a61968dc51ba1111 Linux   Amazon Linux AMI
i-0a84d5b23e5251111 Linux   Amazon Linux AMI
i-0b057729594791111 Windows Microsoft Windows Server 2012 R2 Standard
i-0b1d0a7fb339b1111 Linux   Amazon Linux AMI
i-0da2fefde50351111 Linux   Amazon Linux AMI
i-0eafb22a9581a1111 Linux   Amazon Linux AMI

Upvotes: 5

riverfall
riverfall

Reputation: 840

Try this command:

aws ec2 describe-images --image-ids $(aws ec2 describe-instances --instance-ids i-xxxxxxxxxxxxx --query 'Reservations[0].Instances[0].ImageId' --output text) --query 'Images[0].Name'

$() part gets the ImageId using InstanceId.

Upvotes: 5

John Rotenstein
John Rotenstein

Reputation: 269282

Here's a quick way to list the Platform field, which at least distinguishes between Windows and Linux:

aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId,Platform]' --output text
i-78b4ef47  windows
i-b8ae3386  windows
i-9d3611a2  None
i-1c57c651  windows
i-a241ec91  None
i-7d26b630  None

Upvotes: 6

Rico
Rico

Reputation: 61551

You can't query the specific OS of the instance from the AWS cli but you can query the AMI that the instance is based off of. Also, you can't get an 'OS' attribute but you can get the Description or Name of the AMI, so if you create your AMIs with a meaningful description you can make it work.

$ aws ec2 describe-images --image-ids "ami-xxxxxxxx"
{
    "Images": [
        {
            "VirtualizationType": "paravirtual", 
            "Name": "amazon-linux-20130509", 
            "Tags": [
                {
                    "Value": "amazon-linux-20130509", 
                    "Key": "Name"
                }
            ], 
            "Hypervisor": "xen", 
            "ImageId": "ami-xxxxxxxx", 
            "RootDeviceType": "ebs", 
            "State": "available", 
            "BlockDeviceMappings": [
                {
                    "DeviceName": "/dev/sda1", 
                    "Ebs": {
                        "DeleteOnTermination": true, 
                        "SnapshotId": "snap-xxxxxxxx", 
                        "VolumeSize": 100, 
                        "VolumeType": "standard"
                    }
                }
            ], 
            "Architecture": "x86_64", 
            "ImageLocation": "123456789012/amazon-linux-20130509", 
            "KernelId": "aki-fc37bacc", 
            "OwnerId": "123456789012", 
            "RootDeviceName": "/dev/sda1", 
            "Public": false, 
            "ImageType": "machine", 
            "Description": "Amazon Linux"
        }
    ]
}

If you want to get more detailed you can always write your own script to ssh into the machines and run cat /etc/issue in each one of them.

Upvotes: 1

Related Questions