Last active
February 22, 2024 13:06
-
-
Save amalgjose/49c8a665d339bfef2b644e1a6c824bdf to your computer and use it in GitHub Desktop.
Python program to list all ec2 instances in a region using boto3
This file contains hidden or 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" | |
region = 'us-east-1' | |
client = boto3.client('ec2', aws_access_key_id=access_key, aws_secret_access_key=secret_key, | |
region_name=region) | |
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, instance.tags) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment