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.

10 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

  2. ramu says:

    How can I get these details automatically ? Is there any Lambda function to get the below details ?

    Instance ID
    Private IP
    Tags information
    Instance Type
    Root Volume size
    Data Volume Encryption status
    OS
    EBS Optimized status

  3. Marc says:

    Thanks, this code was a big help.

  4. Daljit singh says:

    thanks worked for me with some new features

  5. goenergia says:

    Thanks for code, is perfect for me.

  6. laxman uppada says:

    Hi All,
    I am new to AWS and i am looking for python script with lamda function which need to pull all the ec2 instances with the security group with inbound and outbound (0.0.0.0.) for all the ec2 instances in the account

    Appreciate if any one suggest me on this .. Thanks in Advance
    Laxman

    • mike says:

      Sounds like you would need to pull all sec groups and identify the secgroup IDs that contain that rule. Then its just a matter of describing instances and filtering on those with that sec group id.

  7. VNCoupon says:

    Thanks for sharing!

Leave a Reply

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

Solve : *
28 − 21 =