Search code examples
amazon-web-servicesamazon-ec2

How to get the list of Nitro system based EC2 instance type by CLI?


I know this page lists up the instance types which based on Nitro system but I would like to know the list in a dynamic way with CLI. (for example, using aws ec2 describe-instances). Is it possible to get Nitro based instance type other than parsing the static page? If so, could you tell me the how?


Solution

  • You'd have to write a bit of additional code to get that information. aws ec2 describe-instances will give you InstanceType property. You should use a programming language to parse the JSON, extract InstanceType and then call describe-instances like so: https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instance-types.html?highlight=nitro

    From the JSON you get back, extract hypervisor. That'll give you Nitro if the instance is Nitro.

    Here's a Python code that might work. I have not tested it fully but you can tweak this to get the results you want.

    """List all EC2 instances"""
    import boto3
    
    def ec2_connection():
        """Connect to AWS using API"""
    
        region = 'us-east-2'
    
        aws_key = 'xxx'
        aws_secret = 'xxx'
    
        session = boto3.Session(
            aws_access_key_id = aws_key,
            aws_secret_access_key = aws_secret
        )
    
        ec2 = session.client('ec2', region_name = region)
    
        return ec2
    
    
    def get_reservations(ec2):
        """Get a list of instances as a dictionary"""
    
        response = ec2.describe_instances()
    
        return response['Reservations']
    
    
    def process_instances(reservations, ec2):
        """Print a colorful list of IPs and instances"""
    
        if len(reservations) == 0:
            print('No instance found. Quitting')
            return
    
        for reservation in reservations:
            for instance in reservation['Instances']:
    
                # get friendly name of the server
                # only try this for mysql1.local server
                friendly_name = get_friendly_name(instance)
                if friendly_name.lower() != 'mysql1.local':
                    continue
    
                # get the hypervisor based on the instance type
                instance_type = get_instance_info(instance['InstanceType'], ec2)
    
                # print findings
                print(f'{friendly_name} // {instance["InstanceType"]} is {instance_type}')
                break
    
    
    def get_instance_info(instance_type, ec2):
        """Get hypervisor from the instance type"""
    
        response = ec2.describe_instance_types(
            InstanceTypes=[instance_type]
        )
    
        return response['InstanceTypes'][0]['Hypervisor']
    
    
    def get_friendly_name(instance):
        """Get friendly name of the instance"""
    
        tags = instance['Tags']
        for tag in tags:
            if tag['Key'] == 'Name':
                return tag['Value']
    
        return 'Unknown'
    
    
    def run():
        """Main method to call"""
    
        ec2 = ec2_connection()
        reservations = get_reservations(ec2)
        process_instances(reservations, ec2)
    
    
    if __name__ == '__main__':
    
        run()
        print('Done')