Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sam-Martin/914dde4b9a9438388082 to your computer and use it in GitHub Desktop.
Save Sam-Martin/914dde4b9a9438388082 to your computer and use it in GitHub Desktop.
Generate AMI hashtable for cloudformation
# Ensure you amend this value to reflect the AMI you're interested in
$imageType = "Windows_Server-2012-R2_RTM-English-64Bit-Core"
$resultsArchitectureFirst = @{'64'=@();'32'=@()};
$resultsRegionFirst = @{};
# Loop through all regions and find the appropriate image
foreach($region in $(Get-AWSRegion).Region){
if(!($image = $(Get-EC2Image -Region $region -Filters $(New-Object Amazon.EC2.Model.Filter -Property @{Name = "name"; Values = "$ImageType*"}) -Owners 801119661308 | select -first 1))){
continue;
}
# Figure out the architecture of the OS into the format we want
if($image.architecture -eq 'x86_64'){
$architecture = '64';
}else{
$architecture = '32';
}
$resultsArchitectureFirst.$architecture += new-object PSObject -Property @{
"Region"=$region;
"ImageID"=$image.imageID
}
$resultsRegionFirst.Add($region,@{
$architecture = $image.imageID;
})
}
$resultsArchitectureFirst | ConvertTo-Json
$resultsRegionFirst | ConvertTo-Json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment