Display amazon servers

Assignment

These are the AMIs that should display:

US East – N. Virginia = ami-cd0f5cb6
US East – Ohio = ami-10547475
US West – N. California = ami-09d2fb69
US West – Oregon = ami-6e1a0117 California ca-central1 = ami-b3d965d7 ***
EU – Ireland = ami-785db401
EU – Frankfurt = ami-1e339e71
EU – London = ami-996372fd
Asia Pacific – Singapore = ami-6f198a0c
Asia Pacific – Sydney = ami-e2021d81
Asia Pacific – Seoul = ami-d28a53bc
Asia Pacific – Tokyo = ami-ea4eae8c A
sia Pacific – Mumbai = ami-099fe766
South America – Sao Paulo = ami-10186f7c

Python code:

import boto3 from pprint

importpprint ec2 = boto3.client(‘ec2’)

resp = ec2.describe_regions()

region_names = sorted([region[‘RegionName’] for region in resp[‘Regions’]])

filters = [{‘Name’: ‘architecture’, ‘Values’: [‘x86_64’]}, {‘Name’: ‘virtualization-type’, ‘Values’: [‘hvm’]}, {‘Name’: ‘owner-id’, ‘Values’: [‘099720109477’]}, {‘Name’: ‘name’, ‘Values’: [‘ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-20170721’]}] for region in region_names: ec2 = boto3.client(‘ec2’, region)

resp = ec2.describe_images(Filters=filters)

#print(resp) print(‘ ‘ + region + ‘:’) print(‘ AMI: ‘ + resp[‘Images’][0][‘ImageId’]) #pprint(resp) #pprint(ec2.describe_images(Filters=[{‘Name’: ‘image-id’, ‘Values’: [‘ami-8c1be5f6’]}])) 

Correct:

‘CreationDate’: ‘2017-04-14T16:48:59.000Z’,

‘Description’: ‘Canonical, Ubuntu, 16.04 LTS, amd64 xenialimage ‘

‘build on 2017-04-14’,

‘EnaSupport’: True,

‘Hypervisor’: ‘xen’,

‘ImageId’: ‘ami-b3d965d7’,

‘ImageLocation’: ‘099720109477/ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-20170414’,

‘ImageType’: ‘machine’,

‘Name’: ‘ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-20170414’,

‘OwnerId’: ‘099720109477’,

‘Public’: True,

‘RootDeviceName’: ‘/dev/sda1’,

‘RootDeviceType’: ‘ebs’,

‘SriovNetSupport’: ‘simple’,

‘State’: ‘available’,

‘VirtualizationType’: ‘hvm’},

{‘Architecture’: ‘x86_64’,

‘BlockDeviceMappings’: [{‘DeviceName’: ‘/dev/sda1’,

‘Ebs’: {‘DeleteOnTermination’: True,

‘Encrypted’: False,

‘SnapshotId’: ‘snap-0e41c8ba6b1c86240’,

‘VolumeSize’: 8,

‘VolumeType’: ‘gp2’}},

{‘DeviceName’: ‘/dev/sdb’,

‘VirtualName’: ‘ephemeral0’},

{‘DeviceName’: ‘/dev/sdc’,

‘VirtualName’: ‘ephemeral1’}],

Incorrect:

‘CreationDate’: ‘2017-07-24T20:27:27.000Z’,

‘Description’: ‘Canonical, Ubuntu, 16.04 LTS, amd64 xenialimage ‘

‘build on 2017-07-21’,

‘EnaSupport’: True,

‘Hypervisor’: ‘xen’,

‘ImageId’: ‘ami-9818a7fc’,

‘ImageLocation’: ‘099720109477/ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-20170721’,

‘ImageType’: ‘machine’,

‘Name’: ‘ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-20170721’,

‘OwnerId’: ‘099720109477’,

‘Public’: True,

‘RootDeviceName’: ‘/dev/sda1’,

‘RootDeviceType’: ‘ebs’,

‘SriovNetSupport’: ‘simple’,

‘State’: ‘available’,

‘VirtualizationType’: ‘hvm’},

{‘Architecture’: ‘x86_64’,

‘BlockDeviceMappings’: [{‘DeviceName’: ‘/dev/sda1’,

‘Ebs’: {‘DeleteOnTermination’: True,

‘Encrypted’: False,

‘SnapshotId’: ‘snap-518dbca2’,

‘VolumeSize’: 8,

‘VolumeType’: ‘gp2’}},

{‘DeviceName’: ‘/dev/sdb’,

‘VirtualName’: ‘ephemeral0’},

{‘DeviceName’: ‘/dev/sdc’,

‘VirtualName’: ‘ephemeral1’}], 

…. 

The point of the program is to contact AWS and retrieve the AMIs associated with each region.

The search is filtering on theValuesubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-20170721”

However, there is one instance AMI (ca-central1 = ami-b3d965d7) that is not being found. The issue is that the specific AMI I need to find uses the Name Value of “ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-20170414” where all other instances use “XXXXXXX-20170721” as above.

There is hundreds of AMIs, so by simply using a wildcard, all of the AMIs that are found using “XXXXXX-20170721” become incorrect.

A Word document shows the differences between the two AMIs (the one that it IS finding and the one that it SHOULD BE finding). 

Solution 

aws.py

import boto3

frompprint import pprint

import re

ec2 = boto3.client(‘ec2’)

resp = ec2.describe_regions()

region_names = sorted([region[‘RegionName’] for region in resp[‘Regions’]])

filters = [{‘Name’: ‘architecture’, ‘Values’: [‘x86_64’]}, {‘Name’: ‘virtualization-type’, ‘Values’: [‘hvm’]}, {‘Name’: ‘owner-id’, ‘Values’: [‘099720109477’]}, {‘Name’: ‘name’, ‘Values’: [(‘ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-20170414’)]}]

filters1 = [{‘Name’: ‘architecture’, ‘Values’: [‘x86_64’]}, {‘Name’: ‘virtualization-type’, ‘Values’: [‘hvm’]}, {‘Name’: ‘owner-id’, ‘Values’: [‘099720109477’]}, {‘Name’: ‘name’, ‘Values’: [(‘ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-20170721’)]}]

for region in region_names:

ec2 = boto3.client(‘ec2’, region)

resp = ec2.describe_images(Filters=filters1)

if region == “ca-central-1” :

resp = ec2.describe_images(Filters=filters)

print(‘ ‘ + region + ‘:’)

print(‘ AMI: ‘ + resp[‘Images’][0][‘ImageId’])

#pprint(resp)

#pprint(ec2.describe_images(Filters=[{‘Name’: ‘image-id’, ‘Values’: [‘ami-8c1be5f6’]}]))