Created
September 26, 2016 02:25
-
-
Save ned1313/1b0ffe2893cbd0a789b5c503a6276ac3 to your computer and use it in GitHub Desktop.
Create-EC2Snapshots.ps1
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
<# | |
.SYNOPSIS | |
This script is intended to tag the volumes of an existing instance and create a snapshot for those volumes. | |
.DESCRIPTION | |
The script takes an instance ID and optional region parameter. It will find the instance in the region submitted or the | |
current default region and find all volumes attached to the instance. Then it will tag those volumes, and create a snapshot. | |
The snapshot will include the volume tags, as well as a date and name tag of its own. | |
.PARAMETER instanceID | |
Required, string, the instanceID of the instance with volumes to have snapshots taken. | |
.PARAMETER region | |
Optional, string, the region in which the instance is located. If no region is specified, the script will use the | |
default region from the user's AWS profile. | |
.INPUTS | |
None | |
.OUTPUTS | |
A list of snapshot IDs created by the script. | |
.NOTES | |
Version: 1.0 | |
Author: Ned Bellavance | |
Creation Date: 9/25/2016 | |
Purpose/Change: Initial script development | |
.EXAMPLE | |
Create-EC2Snapshots -InstanceID "i-abd2f39a" | |
Tags volumes and creates a snapshot for the instance "i-abd2f39a" in the default region. | |
#> | |
#region parameters# | |
param( | |
[Parameter(Mandatory = $true)] | |
[string] $instanceID, | |
[string] $region | |
) | |
#endregion parameters# | |
#region helper functions# | |
#Import Module | |
Import-Module AWSPowerShell | |
#Function takes a resource ID, Key and Value and adds the tag | |
#Why this isn't already a function I have no idea | |
Function Add-EC2Tag { | |
param( | |
[string] $resourceID, | |
[string] $Name, | |
[string] $value | |
) | |
$tag = New-Object Amazon.EC2.Model.Tag | |
$tag.Key = $Name | |
$tag.Value = $value | |
New-EC2Tag -Resource $resourceID -Tag $tag | |
} | |
#endregion helper functions# | |
#region main# | |
#Store current default region to change it back if necessary | |
$currentDefaultRegion = (Get-AWSRegion | where{$_.IsShellDefault}).Region | |
#If a region is specified, set it to the default region | |
if($region){ | |
Set-DefaultAWSRegion -Region $region | |
} | |
#Get the instance in question | |
try{ | |
$i = Get-EC2Instance -InstanceId $instanceID | |
} | |
catch{ | |
Write-Error "Instance with ID $instanceID was not found. Error is: $Error[0]" | |
} | |
#For instance, get the attached volumes | |
$vols = $i.Instances[0].BlockDeviceMappings | |
#Create an array for the snaps to be returned as output | |
$snaps = @() | |
foreach($vol in $vols){ | |
#For each volume add tags | |
#instance id, instance name, mount point, instance region | |
$instanceName =$i.Instances[0].Tag.Where({$_.Key -eq "Name"}).Value | |
Add-EC2Tag -resourceID $vol.Ebs.VolumeId -Name "InstanceName" -value $instanceName | |
Add-EC2Tag -resourceID $vol.Ebs.VolumeId -Name "InstanceID" -value $i.Instances[0].InstanceId | |
Add-EC2Tag -resourceID $vol.Ebs.VolumeId -Name "MountPoint" -value $vol.DeviceName | |
if($region){ | |
Add-EC2Tag -resourceID $vol.Ebs.VolumeId -Name "SourceRegion" -value $region | |
} | |
else{ | |
Add-EC2Tag -resourceID $vol.Ebs.VolumeId -Name "SourceRegion" -value $currentDefaultRegion | |
} | |
#if root volume, include subnet-id and VPC-id, and set IsRootVolume tag to true | |
If($i.Instances[0].RootDeviceName -eq $vol.DeviceName){ | |
Add-EC2Tag -resourceID $vol.Ebs.VolumeId -Name "VpcId" -value $i.Instances[0].VpcId | |
Add-EC2Tag -resourceID $vol.Ebs.VolumeId -Name "SubnetId" -value $i.Instances[0].SubnetId | |
Add-EC2Tag -resourceID $vol.Ebs.VolumeId -Name "IsRootVolume" -value $true | |
} | |
else{ | |
Add-EC2Tag -resourceID $vol.Ebs.VolumeId -Name "IsRootVolume" -value $false | |
} | |
#Create a snapshot with description and copy tags from volume | |
$snap = New-EC2Snapshot -Description "Snap of $($vol.Ebs.VolumeId)" -VolumeId $vol.Ebs.VolumeId | |
$tags = Get-EC2Tag -Filter @{Name="resource-id";Values=$vol.Ebs.VolumeId} | |
foreach($tag in $tags){ | |
if($tag.Key -eq "Name"){ | |
Add-EC2Tag -resourceID $snap.SnapshotId -Name "VolumeName" -value $tag.Value | |
} | |
else{ | |
Add-EC2Tag -resourceID $snap.SnapshotId -Name $tag.Key -value $tag.Value | |
} | |
} | |
#Add tag for date and name | |
Add-EC2Tag -resourceID $snap.SnapshotId -Name "Date" -value $snap.StartTime.ToString("MM-dd-yyyy") | |
Add-EC2Tag -resourceID $snap.SnapshotId -Name "Name" -value "Snap-$($vol.Ebs.VolumeId)" | |
#Add snap to array | |
$snaps += $snap | |
} | |
#Output the snapshot IDs | |
Write-Output ($snaps | select SnapshotID) | |
if($region){ | |
Set-DefaultAWSRegion -Region $currentDefaultRegion | |
} | |
#endregion main# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment