Amazon EBS Snapshot is point in time copy of Amazon EBS(Elastic Block Storage) volumes attached with EC2 instance. We usually take snapshot just to make a baseline system by freezing the state in a snapshot and using it as a source to deploy other similar system. Sometimes, we do take snapshot as a way of backup of system. However, snapshot is not a pure system backup. It takes a point in time snapshot of volume changes in an EBS volume. Please refer here for more information.
It is always important to have a good inventory of snapshots as each those have some cost associated. The script I am providing below will help you to generate a quick inventory of EBS snapshots created in your Account. I tried to include all properties you might need as part of any standard EBS snapshot inventory. Feel free to add your stuff as require. This script assumes you have already setup your AWS credentials and AWS Powershell SDK to run this script on your working system. If you want to get an inventory of EC2 instances details, refer my post here. If you face any issue setting those, please refer following documents to set those:
- https://docs.aws.amazon.com/powershell/latest/userguide/specifying-your-aws-credentials.html
- https://aws.amazon.com/powershell/
Let me know if this is helpful!
$date = Get-Date -UFormat "%m-%d-%Y"
$currentDir = $(Get-Location).Path
$oFile = "$($currentDir)\EC2Snapshot_Details_$($date).csv"
if(Test-Path $oFile){Remove-Item $oFile -Force}
"Snapshot Id,Snapshot Name,Description, Encrypted, Owner Id, Start Time,Volume Size,Volume Id" | Out-File $oFile -Append -Encoding ascii
Get-EC2Snapshot | ForEach-Object {
$snapshotId = $snapshotName = $desc = $enc = $ownerId = $startTime = $volSize = $volId = ""
$snapshotId = $_.SnapshotId
$snapshotName = $_.CreationDate
$tags = $_.Tags
if(!([string]::IsNullOrEmpty($tags))){
if($tags.Key -eq "Name"){$snapshotName = $tags | Where-Object { $_.Key -eq "Name" } | Select-Object -expand Value}
}
$desc = $_.Description.Replace(",", " ")
$enc = $_.Encrypted
$ownerId = $_.OwnerId
$startTime = $_.StartTime
$volSize = $_.VolumeSize
$volId = $_.VolumeId
"$snapshotId,$snapshotName ,$desc, $enc, $ownerId, $startTime ,$volSize ,$volId" | Out-File $oFile -Append -Encoding ascii
}
Exit
Before running aws scripts, set up your aws credentials following provided documentation above.
Download above script and save it with a .ps1 file extension. Open aws powershell sdk tool console. Once, it is open run the script saved previously.
Once the script execution completes, it will generate a CSV file output like below screenshot: