Created
November 13, 2014 13:55
-
-
Save hagbarddenstore/6c1a08cc3b8dbe6062f8 to your computer and use it in GitHub Desktop.
Application to list different machines images in different regions. Used to keep an updated list of AMI ids.
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
/** | |
* Install dependencies via NuGet: Install-Package AWSSDK | |
*/ | |
namespace FindAmazonMachineImages | |
{ | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Amazon; | |
using Amazon.EC2; | |
using Amazon.EC2.Model; | |
public class Program | |
{ | |
public static void Main() | |
{ | |
var regions = new[] | |
{ | |
RegionEndpoint.USEast1, | |
RegionEndpoint.USWest1, | |
RegionEndpoint.USWest2, | |
RegionEndpoint.EUWest1, | |
RegionEndpoint.EUCentral1, | |
RegionEndpoint.SAEast1, | |
RegionEndpoint.APSoutheast1, | |
RegionEndpoint.APSoutheast2, | |
RegionEndpoint.APNortheast1 | |
}; | |
var queries = new[] | |
{ | |
FindImageQuery.Ubuntu1404, | |
FindImageQuery.Windows2008, | |
FindImageQuery.Windows2008R2, | |
FindImageQuery.Windows2012, | |
FindImageQuery.Windows2012R2 | |
}; | |
foreach (var region in regions) | |
{ | |
Console.WriteLine("Region: {0}", region.DisplayName); | |
foreach (var query in queries) | |
{ | |
var image = FindImage(region, query); | |
Console.WriteLine("ImageId: {0}Name: {1}", image.ImageId.PadRight(15), image.Name); | |
} | |
} | |
Console.WriteLine("Press Enter to exit..."); | |
Console.ReadLine(); | |
} | |
private static Image FindImage(RegionEndpoint region, FindImageQuery query) | |
{ | |
var aws = new AmazonEC2Client(region); | |
var request = new DescribeImagesRequest | |
{ | |
Filters = new List<Filter> | |
{ | |
new Filter("architecture", new List<string> { query.Architecture.Value }), | |
new Filter("image-type", new List<string> { "machine" }), | |
new Filter("owner-id", new List<string> { query.OwnerId }), | |
new Filter("virtualization-type", new List<string> { query.VirtualizationType }), | |
new Filter("name", new List<string> { query.Name }) | |
} | |
}; | |
var response = aws.DescribeImages(request); | |
var image = response.Images.OrderByDescending(_ => _.Name).First(); | |
return image; | |
} | |
private class FindImageQuery | |
{ | |
public static readonly FindImageQuery Ubuntu1404 = new FindImageQuery("ubuntu/images/hvm/ubuntu-trusty-14.04-amd64-server-*", CanonicalOwnerId); | |
public static readonly FindImageQuery Windows2008 = new FindImageQuery("Windows_Server-2008-SP2-English-64Bit-Base-*", AmazonOwnerId); | |
public static readonly FindImageQuery Windows2008R2 = new FindImageQuery("Windows_Server-2008-R2_SP1-English-64Bit-Base-*", AmazonOwnerId); | |
public static readonly FindImageQuery Windows2012 = new FindImageQuery("Windows_Server-2012-RTM-English-64Bit-Base-*", AmazonOwnerId); | |
public static readonly FindImageQuery Windows2012R2 = new FindImageQuery("Windows_Server-2012-R2_RTM-English-64Bit-Base-*", AmazonOwnerId); | |
private const string CanonicalOwnerId = "099720109477"; | |
private const string AmazonOwnerId = "801119661308"; | |
public FindImageQuery(string name, string ownerId) | |
: this(name, ownerId, ArchitectureValues.X86_64, VirtualizationType.Hvm) | |
{ | |
} | |
public FindImageQuery(string name, string ownerId, ArchitectureValues architecture, VirtualizationType virtualizationType) | |
{ | |
Name = name; | |
OwnerId = ownerId; | |
Architecture = architecture; | |
VirtualizationType = virtualizationType; | |
} | |
public ArchitectureValues Architecture { get; private set; } | |
public string OwnerId { get; private set; } | |
public VirtualizationType VirtualizationType { get; private set; } | |
public string Name { get; private set; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment