Python Forum
Get EC2 Regions and Zones to correspond
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get EC2 Regions and Zones to correspond
#1
Hello,

I'm trying to pull a list of AWS EC2 instances. I need to pull a list of instances from each AWS zone and region.

Problems I'm having:
  1. Need to pull instances only from corresponding zone and region each loop.
  2. Ec2 Instance, zone and region has to correspond and be accurate.

Currently my script pulls a list of instances, but the correlation is nonsense. For example:
------------------------------------- Instance ID: i-6143add1 Type: m4.2xlarge State: stopped Private IP: 10.1.232.175 Public IP: None Region: eu-north-1 Availability Zone: us-east-1a Launch Time: February 08 2016 -----------------------------------
Is output by the script. But instance ID: i-6143add1 is really in region us-east-1a, and obviously availability zone us-east-1a is not in region eu-north-1.

How can I get the data to line up? Here is my code:

#!/usr/bin/env python import boto3 import collections from collections import defaultdict import time from datetime import datetime from colorama import init, deinit, Fore, Back, Style import csv init() print(Fore.CYAN) print('******************************************************') print(' List AWS Instances') print('******************************************************\n') print(Fore.YELLOW) aws_account = input("Enter the name of the AWS account you'll be working in: ") session = boto3.Session(profile_name=aws_account) ec2 = session.client('ec2') aws_regions = ec2.describe_regions() aws_azs = ec2.describe_availability_zones() ec2info = defaultdict() for region in aws_regions['Regions']: region_name = region['RegionName'] for az in aws_azs['AvailabilityZones']: zone = az['ZoneName'] instance_list = ec2.describe_instances() for reservation in instance_list["Reservations"]: for instance in reservation.get("Instances", []): private_ip_address = instance.get("PrivateIpAddress" , None) public_ip_address = instance.get("PublicIpAddress" , None) instance_state = instance['State']['Name'] if private_ip_address and public_ip_address: launch_time = instance['LaunchTime'] launch_time_friendly = launch_time.strftime("%B %d %Y") ec2info[instance['InstanceId']] = { 'Instance ID': instance['InstanceId'], 'Type': instance['InstanceType'], 'State': instance_state, 'Private IP': instance['PrivateIpAddress'], 'Public IP': instance['PublicIpAddress'], 'Region': region_name, 'Availability Zone': zone, 'Launch Time' : launch_time_friendly } attributes = ['Instance ID', 'Type', 'State', 'Private IP', 'Public IP', 'Region', 'Availability Zone', 'Launch Time' ] for instance_id, instance in ec2info.items(): print(Fore.RESET + "-------------------------------------") for key in attributes: print(Fore.CYAN + "{0}: {1}".format(key, instance[key])) writer.writerow({'Instance ID': key, 'Type': key, 'Launch Time': key}) print(Fore.RESET + "-------------------------------------") elif private_ip_address: launch_time = instance['LaunchTime'] launch_time_friendly = launch_time.strftime("%B %d %Y") ec2info[instance['InstanceId']] = { 'Instance ID': instance['InstanceId'], 'Type': instance['InstanceType'], 'State': instance_state, 'Private IP': instance['PrivateIpAddress'], 'Public IP': None, 'Region': region_name, 'Availability Zone': zone, 'Launch Time' : launch_time_friendly } attributes = ['Instance ID', 'Type', 'State', 'Private IP', 'Public IP', 'Region', 'Availability Zone', 'Launch Time' ] for instance_id, instance in ec2info.items(): print(Fore.RESET + "-------------------------------------") for key in attributes: print(Fore.CYAN + "{0}: {1}".format(key, instance[key])) print(Fore.RESET + "-------------------------------------")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Numpy, array(2d,bool), flipping regions. MvGulik 2 1,583 Oct-27-2024, 11:06 AM
Last Post: MvGulik

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.