Boto3 problem getting EC2 CPUOptions

0

I am having problems get certain values for EC2 instances. For example to get CPU Options.

import json import boto3

def lambda_handler(event, context): s_region = "us-east-1" instance_ids = [] ec2 = boto3.client('ec2', region_name=s_region) print(boto3.version) print(" ") response = ec2.describe_instances() instances_full_details = response['Reservations'] for instance_detail in instances_full_details: group_instances = instance_detail['Instances'] for instance in group_instances: instance_id = instance['InstanceId'] print("ID: " + instance_id) s_temp = instance.get("InstanceType") print('InstanceType=' + s_temp) temp = instance.get("cpu_options") s_temp = str(temp.keys())

this gives me an error. "'NoneType' object has no attribute 'keys'"

Basically I want to get all EC2 Instance properties. Some work find and others are a problem. Here is another trying to get the tag values.

    s_temp = instance.get("tags")
    if s_temp == "NoneType":
            s_print = "N/A"
        elif s_temp == "None":
            s_print = "N/A"
        else:
            s_print = str(s_temp)
        print("Tags: " + s_print)

This always is "None".

Very confusing to me how Boto3 returns the data. Thanks...

asked a year ago255 views
1 Answer
1
Accepted Answer

You can check the "InstanceType" and "cpu_options" with the following code.
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2/client/describe_instances.html

Also, the function "instance.get()" does not exist, resulting in an error.

If the instance tag is not set, nothing will be output.

import json
import boto3

def lambda_handler(event, context): 
    s_region = "ap-northeast-1" 
    instance_ids = [] 
    ec2 = boto3.client('ec2', region_name=s_region) 
    print(boto3.__version__) 
    print(" ") 
    response = ec2.describe_instances(InstanceIds=instance_ids) 
    instances_full_details = response['Reservations'] 
    for instance_detail in instances_full_details: 
        group_instances = instance_detail['Instances'] 
        for instance in group_instances: 
            instance_id = instance['InstanceId'] 
            print("ID: " + instance_id) 
            print(instance['InstanceType'])
            print(instance['CpuOptions'])
#            s_temp = instance.get("InstanceType") 
#            print('InstanceType=' + s_temp) 
#            temp = instance.get("cpu_options") 
#            s_temp = str(temp.keys())
profile picture
EXPERT
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions