Skip to content

Instantly share code, notes, and snippets.

@tjmichael81
Last active October 16, 2015 01:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjmichael81/38d8474dcec9a4948912 to your computer and use it in GitHub Desktop.
Save tjmichael81/38d8474dcec9a4948912 to your computer and use it in GitHub Desktop.
Create snapshots for each volume attached to an instance
# Retrieve instance ID from EC2 Instance Metadata
$instanceID = (New-Object System.Net.WebClient).DownloadString("http://169.254.169.254/latest/meta-data/instance-id")
# Retrieve volumes that are attached to this instance
$volumes = Get-EC2Volume -Filter @{Name = "attachment.instance-id"; Value = $instanceID}
# Create a snapshot for each volume in the collection
foreach ($volume in $volumes){
$volID = $volume.VolumeId
$diskSnapshot = New-EC2Snapshot -VolumeId $volume.VolumeId -Description "Snapshot of $volID from $instanceID"
# Apply tags to the new shapshot
# Volume that was used for the snapshot, snapshot type
New-EC2Tag -Resource $diskSnapshot.SnapshotId -Tag @{ Key = "Volume"; "Value" = $volID}
New-EC2Tag -Resource $diskSnapshot.SnapshotId -Tag @{ Key = "Snapshot Type"; Value = "Daily"}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment