Skip to content

Instantly share code, notes, and snippets.

@MatthewJDavis
Last active June 24, 2021 22:11
Show Gist options
  • Select an option

  • Save MatthewJDavis/d20816b36f253fe5c71b070708c94d50 to your computer and use it in GitHub Desktop.

Select an option

Save MatthewJDavis/d20816b36f253fe5c71b070708c94d50 to your computer and use it in GitHub Desktop.
<#
Gets the attached volumes of the specified instance and shows the latest snapshots for each volume
#>
# Case sensitive!
$InstanceName = 'AWS-IE-DC-01'
# Create an EC2 filter to find the instance with the corresponding Name tag (if you don't Name your instances, use another tag value)
$instanceFilter = New-Object Amazon.EC2.Model.Filter
$instanceFilter.Name = 'tag:Name'
$instanceFilter.Value = $InstanceName
# Get EC2 instance volumeIDs
$volumeIDs = (Get-EC2Instance -Filter $instanceFilter).Instances.blockdevicemappings.ebs.volumeid
# Use the volumeID(s) to get the latest snapshopID(s)
$snapshots = @()
# Get the latest snapshot for each volume
foreach ($volumeID in $volumeIDs) {
$snapshotFilter = New-Object Amazon.EC2.Model.Filter
$snapshotFilter.Name = 'volume-id'
$snapshotFilter.value = $volumeID
$snapshotId = Get-EC2Snapshot -Filter $snapshotFilter | Select-Object -First 1
$snapshots += $snapshotId
}
Write-Output -InputObject $snapshots
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment