top of page

AWS EC2 Snapshot Count and Aged out Snapshot count

  • Edward Walsh
  • Dec 9, 2020
  • 2 min read

So we use AWS as out cloud provider where I work, and we do EBS Snapshots. We use a service by e-cloudgate to setup our snapshot schedule and also manage moving the snapshots to different AWS Regions and also to delete them, locally and in the other regions we keep a copy. We do a rolling 14 days for EBS Snapshots. I like to keep track of my snapshots to make sure they are being pruned according to our schedule and also to see that they are actually happening. This little PowerShell script is a handy quick way to see into your Snapshots.

It does not delete or alter anything, the way it is below it just displays information on the command line. I also assume you have AWS Powershell Module installed and that you have a profile for your AWS account setup in your Powershell. You could use this script to feed into a foreach to purge aged out Snapshots if you want.

This will work in PowerShell 6+ and I have run it on my Windows PC and on my MacBook MacOS using Microsoft Visual Studio Code, which is free and available for Windows, MacOS.





# aws-ec2-snapshot-count-and-aged-out.ps1
Import-Module AWSPowerShell
## Powershell.Core safe (PoSh 6+)
## Ed Walsh
## Date: 12/9/2019
## Modified: 12/9/2020
## Get the count of Snapshots for an AWS account, along with count over set days old, and what is oldest date of snapshot.
## $AllSnapshotsForRemoval variable can be fed to a foreach to delete the aged out snapshots
## As is, this WILL NOT delete anything, it is just informational on snapshot counts and aged out counts.

$AWSAccount = "0123456789012" #Enter In your AWS Acct Number
$KeepSnapshotDays = 30
$runon = Get-Date -Format "MM/dd/yyyy HH:mm K"

$AllSnapshots = Get-EC2Snapshot -OwnerId $AWSAccount
Write-Output `r`n"Run on: $runon" "For AWS Account: $AWSAccount" "Total number of snapshots: $($AllSnapshots.Count)"
 
# Filter only snapshots older than $KeepSnapshotDays
$AllSnapshotsForRemoval = [System.Collections.ArrayList]@()
foreach ($Snapshot in $AllSnapshots) {
 if ($Snapshot.StartTime -lt (Get-Date).AddDays(-$KeepSnapshotDays)) {
 $SnapshotForRemoval = New-Object -TypeName PSObject -Property @{ Name = $Snapshot.VolumeId; Date = $Snapshot.StartTime; Snapshot = $Snapshot.SnapshotId }
 $AllSnapshotsForRemoval += $SnapshotForRemoval
    }
}
Write-Output "Snapshots older than $KeepSnapshotDays days: $($AllSnapshotsForRemoval.Count)`nOldest is: $(($allSnapshotsForRemoval.date) |Measure-Object -Minimum  | select-object -expandproperty minimum)`n"


Output will look like this:


Run on: 12/09/2020 16:46 -05:00
For AWS Account: 0123456789012
Total number of snapshots: 5096
Snapshots older than 30 days: 10
Oldest is: 03/05/2014 16:36:35

PS /Users/ewalsh> 

This was a sample and not my actual snapshot output, otherwise I would be freaking out :-) .

Comments


Featured Posts
Check back soon
Once posts are published, you’ll see them here.
Recent Posts
Archive
Search By Tags
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square

© 2024 by Ed Walsh.

  • s-linkedin
bottom of page