Reputation: 10943
I am trying get some information from my AWS EC2 instances. I will like to know if there is a way to pull information like:
| Platform | Version |
|-----------|---------------:|
| CentOS | 6.0 or 7.0 |
| Ubuntu | 10.04 or 12.04 |
| Windows | |
I will like to know if this is possible using the SDK. I tried with Python SDK Boto3 but no results.
Upvotes: 3
Views: 6253
Reputation: 1267
If you are using boto3 sdk, the docs state that Reservations[].Instances[].Platform
will not be present unless Platform='Windows'
. This function will build you a dict with InstanceId
for keys and Platform
as values:
from boto3 import client
from botocore.exceptions import ClientError
def get_instance_platform(instance_ids:list) -> dict:
ec2 = client('ec2')
paginator = ec2.get_paginator('describe_instances')
try:
# paginator in case you have a lot of instances in account
pages = paginator.paginate(
InstanceIds=instance_ids
)
instances_with_platform = {}
for page in pages:
for reservation in page['Reservations']:
for instance in reservation['Instances']:
instances_with_platform[instance['InstanceId'] = instance.get('Platform','Linux/UNIX')
except ClientError as err:
print('BIG OOF')
# handle this in your own way, I just raise original exception
raise err
return instances_with_platform
I decided to use the built in paginator, which means this will work even if there are a lot of instances (>50) in the account.
Upvotes: 1
Reputation: 146
We can obtain the platform as already answered by @helloV, but there is no means to get the OS directly. But we can use the image id to determine the OS name using some string operations. Using the below command we can obtain the image names, in which we have the operating system names to some extent.
aws ec2 describe-images --image-ids $(aws ec2 describe-instances --query 'Reservations[*].Instances[*].ImageId' --output text) --query 'Images[*].Name' The output of the command is as follows: [ "RHEL-7.6_HVM_GA-20190128-x86_64-0-Hourly2-GP2", "CentOS_7.5_Private", "amzn2-ami-hvm-2.0.20191024.3-x86_64-gp2", "amzn-ami-hvm-2018.03.0.20190826-x86_64-gp2", "Windows_Server-2016-English-Full-Base-2019.11.13", "Windows_Server-2019-English-Full-Base-2019.10.09" ]
Further, if you have an SSM agent installed on you instance,you can run the below command to get the exact Operating System Name.
aws ssm describe-instance-information --query 'InstanceInformationList[*].[InstanceId,PlatformType,PlatformName]' --output text | sort The output of this command is as follows: i-xxxxxxxxxxxx Linux Amazon Linux AMI i-xxxxxxxxxxxx Linux CentOS Linux i-xxxxxxxxxxxx Linux Amazon Linux i-xxxxxxxxxxxx Windows Microsoft Windows Server 2016 Datacenter
Hope this helps!
Upvotes: 2
Reputation: 52383
It is not possible with SDK or CLI unless you have stored that information as tags
. AWS SDK and CLI can help you get information that are available at the hypervisor level. But what you are asking is available inside the VM not at hypervisor.
While the following CLI command can help you a bit, but there is no guarantee you will get the platform information for all instances.
aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId,Platform]' --output text
i-07843115f653771c8 windows
i-e34364c87d4cebd12 None
i-0493b6a67b31df018 None
Upvotes: 3