Created
February 28, 2024 21:57
-
-
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
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
$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