Skip to content

Instantly share code, notes, and snippets.

@lantrix
Last active June 3, 2020 08:58
Show Gist options
  • Save lantrix/33e1966da9c9f0ea5782237d39945cdd to your computer and use it in GitHub Desktop.
Save lantrix/33e1966da9c9f0ea5782237d39945cdd to your computer and use it in GitHub Desktop.
AWS EC2 AMI and snapshot removal - with awesome stuff from @mdjx https://xkln.net/blog/powershell-7-foreach-object-parallel-memory-consumption/
Set-AWSCredential -ProfileName myprofile
Set-DefaultAWSRegion -Region ap-east-1
$images = Get-EC2Image -Owner self -Verbose
# Our parallel processing code is now inside a function that takes start end end array indexes as parameters
function ProcessFiles([int]$FromIndex, [int]$ToIndex) {
$images[$FromIndex..$ToIndex] | Where-Object {$_.CreationDate -Like "2020-04*"} | ForEach-Object -ThrottleLimit 6 -Parallel {
Write-Output -InputObject "Image $($_.ImageId) $($_.CreationDate) $($_.Name)"
Unregister-EC2Image -ImageId $($_.ImageId) -Verbose
}
}
$FromIndex = 0 # Arrays start at 0
$ToIndex = 1499 # Our initial batch goes to index 1499
$Increment = 1500 # We increment each batch by 1500 items
$End = $false # Bool for whether we're at the end of the images array
$LastItemIndex = $images.count - 1 # The index of the last item in the array
do {
Write-Host "[+] Processing AMIs from $FromIndex to $ToIndex" -ForegroundColor Green
ProcessFiles -FromIndex $FromIndex -ToIndex $ToIndex
Write-Host "[+] Running Garbage Collection" -ForegroundColor Red
[system.gc]::Collect() # Manual garbage collection following parallel processing of a batch
# We increment the FromIndex and ToIndex variables to set them for the next batch
$FromIndex = $ToIndex + 1
$ToIndex = $ToIndex + $Increment
# If the ToIndex value exceeds the index of the last item of the array, we set it to the last item
# and flip the $End flag to end batch processing.
if ($ToIndex -ge $LastItemIndex) {
$ToIndex = $LastItemIndex
$End = $true
}
} while ($End -ne $true)
Set-AWSCredential -ProfileName myprofile
Set-DefaultAWSRegion -Region ap-east-1
$images = Get-EC2Image -Owner self -Verbose
$images `
| Where-Object {$_.Description -like "*21go*" -and $_.CreationDate -Like "2020-05-02T02:*"} `
| Sort-Object -Property CreationDate -Descending `
| Format-Table -Property CreationDate, ImageId, Description | Select -First 10
$images | Where-Object {$_.CreationDate -Like "2020-03*"} | ForEach-Object -ThrottleLimit 30 -Parallel {
Write-Output -InputObject "Image: $($_.ImageId) $($_.CreationDate)"
Unregister-EC2Image -ImageId $($_.ImageId) -Verbose
}
# Remove 2020 March AMIs
$snaps = Get-EC2Snapshot -OwnerId self -Verbose
$snaps | Where-Object {$_.StartTime -gt (Get-Date -Date "1 Feb 2020")} # Show Snapshots after 1 Feb 2020
($snaps | Where-Object {$_.StartTime -lt (Get-Date -Date "1 Mar 2020")}) | Remove-EC2Snapshot # Remove Snapshots dated before 1 Jan 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment