This code snippet will help you to get the list of all running EC2 instances across all regions in an AWS account. I have used python boto3 package for developing the code. This code will dynamically pick up all the aws ec2 regions. So the code will work perfectly without any modification even if a new region gets added to the AWS.
Note: Only the basic api calls just to list the instance details are mentioned in this program . Proper coding conventions are not followed . 🙂
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
access_key = "XXXXXXXXXXXXXXXXXX" | |
secret_key = "XXXXXXXXXXXXXXXXXX" | |
client = boto3.client('ec2', aws_access_key_id=access_key, aws_secret_access_key=secret_key, | |
region_name='us-east-1') | |
ec2_regions = [region['RegionName'] for region in client.describe_regions()['Regions']] | |
for region in ec2_regions: | |
conn = boto3.resource('ec2', aws_access_key_id=access_key, aws_secret_access_key=secret_key, | |
region_name=region) | |
instances = conn.instances.filter() | |
for instance in instances: | |
if instance.state["Name"] == "running": | |
print (instance.id, instance.instance_type, region) |
Generate the AWS Access Key and Secret Access Key from the AWS console (IAM section). This code will be helpful to quickly find the summary of all the EC2 instances across regions.
great
This is great.
Could you please advice how to add also Tag name in this script so it returns the Name of EC2 instance?
The API has changed. The one I used in this code is with respect to boto2. Now it is boto3. Please refer to the boto3 docs for more details. Let me know if you face any issues.
Hi Amal,
I was beginner and in AWS and I am able to connect. This works perfectly after trying many ways to connect.
Regards
Arun
where is the code ???
The code is available in the blog post.
Thanks for this Amal. Got me the information that I was looking for.
is there any chance of getting the count from all regions in integers i.e count: 10
Yes, it is easy. Instance count is the size of the instance list.
I tried for instance.size but getting an error , can you please tell the correct way.
How to get the total count of Instances and volumes and loadbalancers present in the aws using boto3.?
Hi Amal ,
Lambda function how to list down instance with region if they don’t have below tag with proper values?
Hi Nandhu,
It is easy. We just need to define the entire logic in a function with the name lambda_handler. Something like the one below.
def lambda_handler(event,context):
“”” code here”””
Hello Programmers! I am new on Programming world can you please share some ideas with me how to modify the code to get all instances in a specific region let’s say in (us-east-1) and their tags?
Thank you in advance!
Hello Programmers! I am new on Programming world can you please share some ideas with me how to modify the code to get all instances in a specific region let’s say in (us-east-1) and their tags?
Thank you in advance!
Use the below program, This will list all the EC2 instances in us-east-1 region.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
list_ec2_instances.py
hosted with ❤ by GitHub
Please let me know if you are facing any issue
Thank you Amal!
Hi
Can this be achieved using the above code,
( Create an inventory script which can iterate through each of our different AWS accounts, grab information about all of the ec2 instances in all of the regions there and then export it to an csv or an xlsx file )
Make sure to include the following information from each ec2 instance:
Account ID
AWS Account Name
Instance ID
Instance Name
Private IP Address
Public IP Address
Instance Type
Region
Yes, with some minor modifications, this can be achieved
if possible can you share the script for this please