Skip to content

Instantly share code, notes, and snippets.

@barretts
Created February 28, 2024 21:57
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 barretts/0c00d9311cf44da841fb54e668601e41 to your computer and use it in GitHub Desktop.
Save barretts/0c00d9311cf44da841fb54e668601e41 to your computer and use it in GitHub Desktop.
get all AMIs in a region and collect their last used date information valid from 2017 on
$amiInfo = aws ec2 describe-images --owners self --query 'Images[*].{ImageId:ImageId, CreationDate:CreationDate, Name:Name}' --output json | ConvertFrom-Json
$amiResults = @()
# $count = $amiInfo.Length;
# Write-Host "getting data for $count AMIs"
foreach ($ami in $amiInfo) {
$amiId = $ami.ImageId
$creationDate = $ami.CreationDate
$repositoryName = $ami.Name
#Write-Host "get last launched time for $amiId $repositoryName $creationDate"
$lastLaunchedTimeJson = aws ec2 describe-image-attribute --image-id $amiId --attribute lastLaunchedTime --output json | ConvertFrom-Json
$lastLaunchedTime = $lastLaunchedTimeJson.LastLaunchedTime.Value
#Write-Host "last launched time for $amiId is $lastLaunchedTime"
$amiObject = [PSCustomObject]@{
AMI_ID = $amiId
AMI_Name = $repositoryName
Creation_Date = $creationDate
Last_Launch_Time = $lastLaunchedTime
}
$amiResults += $amiObject
#Write-Host "continue"
}
#Write-Host "writing to CSV file"
$amiResults | Export-Csv -Path 'lastLaunchedTime_AMI_Info.csv' -NoTypeInformation
Write-Host "AMI information exported to lastLaunchedTime_AMI_Info.csv"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment