1

I need to get this info ("Amazon Linux" line) via aws-cli or aws-sdk. enter image description here

I've tried:

aws ec2 describe-images --image-id ami-0b898040803850657 --region us-east-1 --output json

But it doesn't give this kind of info in a straightforward way:

{
    "Images": [
        {
            "Architecture": "x86_64",
            "CreationDate": "2019-06-19T21:59:15.000Z",
            "ImageId": "ami-0b898040803850657",
            "ImageLocation": "amazon/amzn2-ami-hvm-2.0.20190618-x86_64-gp2",
            "ImageType": "machine",
            "Public": true,
            "OwnerId": "137112412989",
            "State": "available",
            "BlockDeviceMappings": [
                {
                    "DeviceName": "/dev/xvda",
                    "Ebs": {
                        "Encrypted": false,
                        "DeleteOnTermination": true,
                        "SnapshotId": "snap-08091107f3acb12b2",
                        "VolumeSize": 8,
                        "VolumeType": "gp2"
                    }
                }
            ],
            "Description": "Amazon Linux 2 AMI 2.0.20190618 x86_64 HVM gp2",
            "EnaSupport": true,
            "Hypervisor": "xen",
            "ImageOwnerAlias": "amazon",
            "Name": "amzn2-ami-hvm-2.0.20190618-x86_64-gp2",
            "RootDeviceName": "/dev/xvda",
            "RootDeviceType": "ebs",
            "SriovNetSupport": "simple",
            "VirtualizationType": "hvm"
        }
    ]
}

Sure it is possible to parse "Description" field but there is no guarantee that the OS name is in the description.

So what is the correct way to get the OS of an AMI? Also note that I need to differentiate between RHEL, SUSE, Linux and Windows. I need the info about OS to lookup the pricing for my instances (programmatically). If someone knows the easier way then please feel free to share.

1
1

From just the image list, more robust to check both "OwnerId" and "Name" match patterns. Which is how AWS documents how to find popular AMIs.

Beyond doing your own scripting, any inventory system claiming AWS support can group by OS. For example:

  • Of course AWS has their own, Systems Manager Inventory, at extra cost.
  • Ansible will log into each instance and identify OS. (Also works for hosts not in any cloud, and on obscure OS distributions.)

Often it is easy to tell Linux from Windows because the clouds treat those platforms very differently. RHEL vs CentOS is a more subtle distinction, both Linux, both EL, one has usage costs.

1

Unfortunately I suspect this is impossible. This is because both in the AWS-CLI and in boto3 for python on which the AWS CLI is based the platform option in describe images is designed to return a blank if it is not in windows and "windows" if it is a windows machine.

Citations: For Boto3, for the CLI.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.