Skip to content

Instantly share code, notes, and snippets.

@nanoDBA
Created February 13, 2025 22:22
Show Gist options
  • Save nanoDBA/c3c83c0edf8fe80c3c5faa20b275668a to your computer and use it in GitHub Desktop.
Save nanoDBA/c3c83c0edf8fe80c3c5faa20b275668a to your computer and use it in GitHub Desktop.
Retrieves AWS EC2 volume modification records (size, IOPS, volume type changes, etc) and appends the associated instance name. Uses AWS Tools for PowerShell
<#
.SYNOPSIS
Retrieves AWS EC2 volume modification records and useful metadata.
πŸš€ Because sometimes we need speedy volume modifications!
.DESCRIPTION
The Get-EC2VolumeModificationDetails function retrieves modification details
for a list of specified EC2 volumes such as size, IOPS, volume type changes,
etc. It uses AWS SDK for .NET to interact with AWS EC2 services.
πŸ€– This function fetches the modification details and the associated instance
name for each volume (if attached).
.PARAMETER Region
The AWS region where the volumes are located. Defaults to 'us-east-1'.
.PARAMETER VolumeIds
An array of EC2 volume IDs for which the modification details need to be
retrieved.
.EXAMPLE
Get-EC2VolumeModificationDetails -Region us-east-1 -VolumeIds vol-0123456789abcdef0 |
Format-Table VolumeId, EndTime, ModificationState, OriginalIops, OriginalThroughput,
OriginalSize, OriginalVolumeType, Progress, StartTime, TargetIops, TargetThroughput,
TargetSize, TargetVolumeType
.EXAMPLE
# Another usage example with multiple volume IDs, output to a list
# πŸš€
PS> Get-EC2VolumeModificationDetails -VolumeIds vol-0123456789abcdef0, vol-0abcdef1234567890
This command retrieves and displays the modification details for the
specified EC2 volumes.
.EXAMPLE
# Example usage passing here-string variables for volume IDs and properties
$VolumeIds = @"
vol-0123456789abcdef0
vol-0abcdef1234567890
"@.Split("`n").TrimEnd("`r")
$Properties = @"
VolumeId
EndTime
ModificationState
OriginalIops
OriginalThroughput
OriginalSize
OriginalVolumeType
Progress
StartTime
TargetIops
TargetThroughput
TargetSize
TargetVolumeType
InstanceName
"@.Split("`n").TrimEnd("`r")
Get-EC2VolumeModificationDetails -VolumeIds $VolumeIds `
| Format-Table $Properties
.NOTES
- Requires AWS Tools for PowerShell (AWSPowerShell or AWS.Tools.EC2, etc.)
- πŸ’‘ Tip: Ensure you have valid credentials before running this command.
- Ensure AWS SDK assemblies are available and loaded.
- Handles errors gracefully and continues processing other volumes.
#>
function Get-EC2VolumeModificationDetails {
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)]
[string]$Region = 'us-east-1',
[Parameter(Mandatory = $true)]
[string[]]$VolumeIds
)
[System.Reflection.Assembly]::LoadWithPartialName("AWSSDK.EC2") | Out-Null
foreach ($volId in $VolumeIds) {
try {
# Retrieve the volume modification record
$volumeMod = Get-EC2VolumeModification -Region $Region -VolumeId $volId
if ($volumeMod -eq $null) {
throw "No modification record found for volume '$volId'"
}
} catch {
Write-Error $_.Exception.Message
continue
}
try {
# Retrieve the volume and instance information
$volume = Get-EC2Volume -Region $Region -VolumeId $volId
$instanceId = $volume.Attachments[0].InstanceId
# Grab the instance name tag for clarity
$instanceName = (Get-EC2Tag -Region $Region -Filter `
@{ Name="key";Values="Name"},@{ Name="resource-type";Values="instance"} `
| Where-Object ResourceId -EQ $instanceId).Value
# Output the volume modification record, appended with instance name
$volumeMod | Select-Object *, @{ Name='InstanceName'; Expression={ $instanceName } }
} catch {
Write-Error "Failed to retrieve details for volume '$volId': $_.Exception.Message"
continue
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment