Last active
October 16, 2015 01:54
-
-
Save tjmichael81/38d8474dcec9a4948912 to your computer and use it in GitHub Desktop.
Create snapshots for each volume attached to an instance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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