How to print all the instance-id and instance-state using Python boto3

+2 votes
I want to print all the instance-id and instance-state using Python boto3.

How can i do that?
Dec 3, 2018 in AWS by Nitesh
• 3,080 points
33,878 views

1 answer to this question.

0 votes

You can simply loop through using a for loop.

Using for loop you can traverse through all the instances.

Here is a simple program that you can use after configuring your IAM using using AWS CLI.

import boto3
ec2 = boto3.resource('ec2')
for instance in ec2.instances.all():
    print (instance.id , instance.state)

Hope this helps!

If you need to know more about Python, join Python online course certification today.

Thanks!

answered Dec 3, 2018 by Nabarupa
this giving for only one region

what if i want to get list of all instances in each zone

how do i do it ????

Hey! You can try something like this:

for region in `aws ec2 describe-regions --output text | cut -f3`
do
     echo -e "\nListing Instances in region:'$region'..."
     aws ec2 describe-instances --region $region
done

Here, it loops through all the regions, printing the ec2 instances that exist in them.

Hey thanks for the response

my senior if the person have only the instance ID or Name of the instance

he should know the status of that  instance irrespective of region it can be from any region.

The following will print the instance status in all the regions.

for region in `aws ec2 describe-regions --output text | cut -f3`
do
     echo -e "\nInstances status in region: '$region'"
     aws ec2 describe-instance-status --include-all-instances
done

Related Questions In AWS

0 votes
1 answer

How to Pass the VPC ID while creating the Ec2 instance in AWS using Python Boto3

import boto3 ec2 = boto3.resource('ec2') instance = ec2.create_instances( ...READ MORE

answered Jan 29, 2019 in AWS by Priyaj
• 58,090 points
2,961 views
0 votes
1 answer

How to delete all the subnets from a VPC using boto3?

You can refer to this question here: https://www.edureka.co/community/32182/unable-to-delete-subnets-in-a-vpc-using-boto3 You ...READ MORE

answered Dec 5, 2018 in AWS by Shuvodip Ghosh
1,540 views
0 votes
1 answer

AWS S3 uploading hidden files by default

versioning is enabled in your bucket. docs.aws.amazon.com/AmazonS3/latest/user-guide/….... the ...READ MORE

answered Oct 4, 2018 in AWS by Priyaj
• 58,090 points
5,447 views
–1 vote
1 answer

How to decrypt the encrypted S3 file using aws-encryption-cli --decrypt

Use command : aws s3 presign s3://mybucket/abc_count.png you get ...READ MORE

answered Oct 22, 2018 in AWS by Priyaj
• 58,090 points
4,866 views
0 votes
1 answer

Import my AWS credentials using python script

Using AWS Cli  Configure your IAM user then ...READ MORE

answered Nov 16, 2018 in AWS by Jino
• 5,810 points
2,591 views
0 votes
2 answers
+1 vote
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP