How to use Python Boto3 to list Instances in Amazon AWS

Continuing on with simple examples to help beginners learn the basics of Python and Boto3.

This is a very simple tutorial showing how to get a list of instances in your Amazon AWS environment.

import boto3    
ec2client = boto3.client('ec2')
response = ec2client.describe_instances()
for reservation in response["Reservations"]:
    for instance in reservation["Instances"]:
        # This sample print will output entire Dictionary object
        print(instance)
        # This will print will output the value of the Dictionary key 'InstanceId'
        print(instance["InstanceId"])

You can also create a resource object from the instance item as well.

.
.
    for instance in reservation["Instances"]
        ec2 = boto3.resource('ec2')
        specificinstance = ec2.Instance(instance["InstanceId"])
Tagged , , . Bookmark the permalink.

3 Responses to How to use Python Boto3 to list Instances in Amazon AWS

  1. Ike Kim says:

    Really like the examples on your site.

    I need to understand the difference and know how to work with ec2client = boto3.client(‘ec2’) versus ec2 = boto3.resource(‘ec2’), but my biggest difficulty is to referring and extracting the data from ec2client or ec2 objects.

    I find other examples online, but I haven’t found where they explain how to walk through and extract all the values you need. I usually see isolated examples.

    Do your Python Boto3 Guides Volume 1 & 2 show what I’m looking to do and is Kindle is the only option?

    I don’t own a Kindle.

    • mike says:

      The client is akin to the aws cli api calls. You are asking the AWS API for something or to do something. e.g. describe instances.
      The resource is more like a python object. You create a resource representing 1 specific instance and then can query or use methods on that object.

      My Volume 1 guide has the info on learning about the client vs. resource and shows how to pick apart the responses you get back from AWS. Good Luck

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve : *
13 − 5 =