Skip to content

Instantly share code, notes, and snippets.

@lantrix
Last active August 29, 2015 14:08
Show Gist options
  • Save lantrix/9e9d51e92cf5d001c70b to your computer and use it in GitHub Desktop.
Save lantrix/9e9d51e92cf5d001c70b to your computer and use it in GitHub Desktop.
AWS Instance Volume Creation Scripts
<#
.SYNOPSIS
Creates a number of NTFS Volume in AWS, attaches to an instance, formats, brings them online.
.EXAMPLE
_createVolumes.ps1 -zone ap-southeast-2a -type standard -instance i-12345678 -allocation 64
This command creates volumes for instance i-12345678.
The Disks will be 64KB NTFS allocation.
#>
#No params supplied
#Input Parameters
param(
[parameter(mandatory=$true)][string]$zone, #AWS Availability Zone
[parameter(mandatory=$true)][string]$type, #EBS volume type
[parameter(mandatory=$false)][int]$iops, #IOPS for iops types
[parameter(mandatory=$true)][int]$allocation, #Disk allocation size
[parameter(mandatory=$true)][string]$instance, #For Instance to attach to
[parameter(mandatory=$true)][string]$name, #For tag name
[parameter(mandatory=$false)][string]$owner, #For tag owner
[parameter(mandatory=$false)][string]$stream, #For tag stream
[parameter(mandatory=$false)][string]$project, #For tag project
[parameter(mandatory=$false)][string]$environmentName #For tag environment name
)
# AWS-provided PowerShell module
if(Get-Module AWSPowerShell){
Remove-Module AWSPowerShell
}
Import-Module AWSPowerShell -ErrorAction Stop #Required
#lowercase type
$type = $type.ToLower()
#validate disk type
If ("standard", "gp2", "iops" -NotContains $type) {
Throw "$($type) is not a valid type for Volumes! See http://docs.aws.amazon.com/sdkfornet/latest/apidocs/items/TEC2_VolumeType_NET3_5.html"
}
#validate disk type
If ($type -eq "iops") {
if ($iops -eq $null) {
Throw "-iops is required when -type iops is provided. The vvalue is the number of I/O operations per second (IOPS) to provision for the volume."
}
}
#validate allocation size
If (4, 8, 16, 32, 64 -NotContains $allocation) {
Throw "$($allocation) is not a valid NTFS cluster size! See http://support.microsoft.com/kb/140365"
}
#Validate Zone
$validzone = $false
$a = Get-EC2Region
foreach ($region in $a) {
try {
$b = Get-EC2AvailabilityZone -Region $region.RegionName
foreach ($available in $b) {
if ($available.ZoneName -eq $zone) { $validzone = $true }
}
} catch {
#ignore regions we dont have access to
}
}
If (!$validzone) {
Throw "$($zone) is not a valid AWS EC2 availability zone."
}
#Validate Instance exists
try {
$instanceCheck = Get-EC2Instance -Instance $instance
if ($instanceCheck.Instances.State.Name -ne "Running") {
Throw
}
} catch {
Throw "$($instance) is not a valid or running AWS EC2 instance."
}
#Bulk Tags
$newvoltags = @()
#mandatory param for tag
$t = New-Object Amazon.EC2.Model.Tag
$t.Key = 'Name'
$t.Value = $name
$newvoltags += $t
[bool]$owner | Out-Null
if ($owner) {
$t = New-Object Amazon.EC2.Model.Tag
$t.Key = 'Owner'
$t.Value = $owner
$newvoltags += $t
}
[bool]$stream | Out-Null
if ($stream) {
$t = New-Object Amazon.EC2.Model.Tag
$t.Key = 'Stream'
$t.Value = $stream
$newvoltags += $t
}
[bool]$project | Out-Null
if ($project) {
$t = New-Object Amazon.EC2.Model.Tag
$t.Key = 'Project'
$t.Value = $project
$newvoltags += $t
}
[bool]$environmentName | Out-Null
if ($environmentName) {
$t = New-Object Amazon.EC2.Model.Tag
$t.Key = 'Environment Name'
$t.Value = $environmentName
$newvoltags += $t
}
$newvoltags
#new SQL2014 volumes (size in GiB)
############################################################################################
$size = 1331
$device0 = "xvdg" #E
$size0 = $size * 0.5 #Equally divide disks for striping
$device1 = "xvdh" #E
$size1 = $size * 0.5 #Equally divide disks for striping
$thisvolume0 = New-EC2Volume -Size $size0 -VolumeType $type -AvailabilityZone $zone #create
$thisvolume1 = New-EC2Volume -Size $size1 -VolumeType $type -AvailabilityZone $zone #create
New-EC2Tag -Resources @( $thisvolume0.VolumeId, $thisvolume1.VolumeId ) -Tags $newvoltags #bulk tags
New-EC2Tag -Resources $thisvolume0.VolumeId -Tags @( @{ Key="Letter"; Value="E0" }, @{ Key="Device"; Value=$device0 } ) #tag with vol specifics #specific tags
New-EC2Tag -Resources $thisvolume1.VolumeId -Tags @( @{ Key="Letter"; Value="E1" }, @{ Key="Device"; Value=$device1 } ) #tag with vol specifics #specific tags
#wait
Start-Sleep -s 10
$attach0 = Add-EC2Volume -VolumeId $thisvolume0.VolumeId -InstanceId $instance -Device $device0 #attach
$attach1 = Add-EC2Volume -VolumeId $thisvolume1.VolumeId -InstanceId $instance -Device $device1 #attach
#wait
Start-Sleep -s 10
& .\createStripedVolume.ps1 -letter F -allocation 8 -label MyDisk
#############################################################################################
$size = 10
$device = "xvdi" #F
$thisvolume = New-EC2Volume -Size $size -VolumeType $type -AvailabilityZone $zone #create
New-EC2Tag -Resources $thisvolume.VolumeId -Tags $newvoltags #bulk tags
New-EC2Tag -Resources $thisvolume.VolumeId -Tags @( @{ Key="Letter"; Value="F" }, @{ Key="Device"; Value=$device } ) #tag with vol specifics #specific tags
#wait
Start-Sleep -s 10
$attach = Add-EC2Volume -VolumeId $thisvolume.VolumeId -InstanceId $instance -Device $device #attach
#wait
Start-Sleep -s 10
& .\createSingleVolume.ps1 -letter G -allocation 16 -label "My New Disk"
#############################################################################################
$size = 50
$device = "xvdj" #G
$thisvolume = New-EC2Volume -Size $size -VolumeType $type -AvailabilityZone $zone #create
New-EC2Tag -Resources $thisvolume.VolumeId -Tags $newvoltags #bulk tags
New-EC2Tag -Resources $thisvolume.VolumeId -Tags @( @{ Key="Letter"; Value="G" }, @{ Key="Device"; Value=$device } ) #tag with vol specifics #specific tags
#wait
Start-Sleep -s 10
$attach = Add-EC2Volume -VolumeId $thisvolume.VolumeId -InstanceId $instance -Device $device #attach
#wait
Start-Sleep -s 10
& .\createSingleVolume.ps1 -letter G -allocation 16 -label "My New Disk"
#############################################################################################
$size = 10
$device = "xvdk" #H
$thisvolume = New-EC2Volume -Size $size -VolumeType $type -AvailabilityZone $zone #create
New-EC2Tag -Resources $thisvolume.VolumeId -Tags $newvoltags #bulk tags
New-EC2Tag -Resources $thisvolume.VolumeId -Tags @( @{ Key="Letter"; Value="H" }, @{ Key="Device"; Value=$device } ) #tag with vol specifics #specific tags
#wait
Start-Sleep -s 10
$attach = Add-EC2Volume -VolumeId $thisvolume.VolumeId -InstanceId $instance -Device $device #attach
#wait
Start-Sleep -s 10
& .\createSingleVolume.ps1 -letter G -allocation 16 -label "My New Disk"
#############################################################################################
$size = 2870
$device0 = "xvdl" #J
$size0 = $size * (1/3) #Equally divide disks for striping
$device1 = "xvdm" #J
$size1 = $size * (1/3)
$device2 = "xvdn" #J
$size2 = $size * (1/3)
$thisvolume0 = New-EC2Volume -Size $size0 -VolumeType $type -AvailabilityZone $zone #create
$thisvolume1 = New-EC2Volume -Size $size1 -VolumeType $type -AvailabilityZone $zone #create
$thisvolume2 = New-EC2Volume -Size $size2 -VolumeType $type -AvailabilityZone $zone #create
New-EC2Tag -Resources @( $thisvolume0.VolumeId, $thisvolume1.VolumeId, $thisvolume2.VolumeId, $thisvolume3.VolumeId ) -Tags $newvoltags #bulk tags
New-EC2Tag -Resources $thisvolume0.VolumeId -Tags @( @{ Key="Letter"; Value="J0" }, @{ Key="Device"; Value=$device0 } ) #tag with vol specifics #specific tags
New-EC2Tag -Resources $thisvolume1.VolumeId -Tags @( @{ Key="Letter"; Value="J1" }, @{ Key="Device"; Value=$device1 } ) #tag with vol specifics #specific tags
New-EC2Tag -Resources $thisvolume2.VolumeId -Tags @( @{ Key="Letter"; Value="J2" }, @{ Key="Device"; Value=$device2 } ) #tag with vol specifics #specific tags
#wait
Start-Sleep -s 10
$attach0 = Add-EC2Volume -VolumeId $thisvolume0.VolumeId -InstanceId $instance -Device $device0 #attach
$attach1 = Add-EC2Volume -VolumeId $thisvolume1.VolumeId -InstanceId $instance -Device $device1 #attach
$attach2 = Add-EC2Volume -VolumeId $thisvolume2.VolumeId -InstanceId $instance -Device $device2 #attach
#############################################################################################
$size = 10
$device = "xvdo" #K
$thisvolume = New-EC2Volume -Size $size -VolumeType $type -AvailabilityZone $zone #create
New-EC2Tag -Resources $thisvolume.VolumeId -Tags $newvoltags #bulk tags
New-EC2Tag -Resources $thisvolume.VolumeId -Tags @( @{ Key="Letter"; Value="K" }, @{ Key="Device"; Value=$device } ) #tag with vol specifics #specific tags
#wait
Start-Sleep -s 10
$attach = Add-EC2Volume -VolumeId $thisvolume.VolumeId -InstanceId $instance -Device $device #attach
#wait
Start-Sleep -s 10
& .\createSingleVolume.ps1 -letter G -allocation 16 -label "My New Disk"
#############################################################################################
$size = 466
$device = "xvdp" #M
$thisvolume = New-EC2Volume -Size $size -VolumeType $type -AvailabilityZone $zone #create
New-EC2Tag -Resources $thisvolume.VolumeId -Tags $newvoltags #bulk tags
New-EC2Tag -Resources $thisvolume.VolumeId -Tags @( @{ Key="Letter"; Value="M" }, @{ Key="Device"; Value=$device } ) #tag with vol specifics #specific tags
#wait
Start-Sleep -s 10
$attach = Add-EC2Volume -VolumeId $thisvolume.VolumeId -InstanceId $instance -Device $device #attach
#wait
Start-Sleep -s 10
& .\createSingleVolume.ps1 -letter G -allocation 16 -label "My New Disk"
#############################################################################################
$size = 60
$device = "xvdq" #N
$thisvolume = New-EC2Volume -Size $size -VolumeType $type -AvailabilityZone $zone #create
New-EC2Tag -Resources $thisvolume.VolumeId -Tags $newvoltags #bulk tags
New-EC2Tag -Resources $thisvolume.VolumeId -Tags @( @{ Key="Letter"; Value="N" }, @{ Key="Device"; Value=$device } ) #tag with vol specifics #specific tags
#wait
Start-Sleep -s 10
$attach = Add-EC2Volume -VolumeId $thisvolume.VolumeId -InstanceId $instance -Device $device #attach
#wait
Start-Sleep -s 10
& .\createSingleVolume.ps1 -letter G -allocation 16 -label "My New Disk"
<#
.SYNOPSIS
Creates a striped NTFS volume (only 1 available) from only one (available) Raw Disk on a Windows Server
.EXAMPLE
createSingleVolume.ps1 -letter F -allocation 64 -label "My Disk"
This command creates a single Volume from a single available disk.
The Disk will be 64KB NTFS allocation, Drive F: with the label of "My Disk"
#>
#No params supplied
#Input Parameters
param(
[parameter(mandatory=$true)][ValidateLength(1,1)][string]$letter, #Drive Letter
[parameter(mandatory=$true)][int]$allocation, #Blocksize in KB
[parameter(mandatory=$true)][ValidateLength(1,32)][string]$label #drive label
)
#validate allocation size
If (4, 8, 16, 32, 64 -NotContains $allocation) {
Throw "$($allocation) is not a valid NTFS cluster size! See http://support.microsoft.com/kb/140365"
}
$allocation = $allocation * 1024 #Convert to Bytes
# All parameters are valid
write-host "Creating single Volume from available raw disk for $($letter) drive"
#Get started
$disk = get-disk |where partitionstyle -eq 'raw'
if ($disk.Count -gt 1) {throw "Too many RAW disks (need ONLY 1)"}
if ($disk.Count -eq 0) {throw "No RAW disk Available (Need ONLY 1)"}
#Prepare diskpart Script for disk
Initialize-Disk -PartitionStyle MBR -Number $disk.Number #Bring Online and Initialize
$a = "select disk $($disk.Number)"+[char][int](13)+[char][int](10); #Select Disk
$a += "attributes disk clear readonly noerr "+[char][int](13)+[char][int](10); #set attributes
$a += "convert dynamic noerr "+[char][int](13)+[char][int](10); #convert to dynamic
$a += "create volume simple disk=$($disk.Number)"+[char][int](13)+[char][int](10); #Select Disk
$a += "format fs=NTFS unit=$($allocation) label=`"$label`" quick "+[char][int](13)+[char][int](10); #Format Vol
$a += "assign letter=$letter "+[char][int](13)+[char][int](10); #Assign Drive Letter
$a += "exit"+[char][int](13)+[char][int](10);
$a | Set-Content c:\diskpart.txt -Encoding ASCII
#Execute script for disk
& Diskpart /s c:\diskpart.txt
Remove-Item c:\diskpart.txt
<#
.SYNOPSIS
Creates a striped NTFS volume from two (2) or more (available) Raw Disks to a Windows Server
.EXAMPLE
createStripedVolume.ps1 -letter F -allocation 64 -label "My Disk"
This command creates a Striped Volume from two or more raw avaialble disks.
The Disk will be 64KB NTFS allocation, Drive F: with the label of "My Disk"
#>
#No params supplied
#Input Parameters
param(
[parameter(mandatory=$true)][ValidateLength(1,1)][string]$letter, #Drive Letter
[parameter(mandatory=$true)][int]$allocation, #Blocksize in KB
[parameter(mandatory=$true)][ValidateLength(1,32)][string]$label #drive label
)
#validate allocation size
If (4, 8, 16, 32, 64 -NotContains $allocation) {
Throw "$($allocation) is not a valid NTFS cluster size! See http://support.microsoft.com/kb/140365"
}
$allocation = $allocation * 1024 #Convert to Bytes
# All parameters are valid
write-host "Creating striped Volume Set from available raw disks for $($letter) drive"
$disks = get-disk |where partitionstyle -eq 'raw'
if ($disks.Count -lt 2) {throw "Not Enough RAW disks Available (Need >= 2)"}
#Prepare diskpart Script for each disk
foreach ($disk in $disks) {
Initialize-Disk -PartitionStyle MBR -Number $disk.Number #Bring Online and Initialize
$a = "select disk $($disk.Number)"+[char][int](13)+[char][int](10); #Select Disk
$a += "attributes disk clear readonly noerr "+[char][int](13)+[char][int](10); #set attributes
$a += "convert dynamic noerr "+[char][int](13)+[char][int](10); #convert to dynamic
$a += "exit"+[char][int](13)+[char][int](10);
$a | Set-Content c:\diskpartA_$($disk.Number).txt -Encoding ASCII
}
#Execute script for each disk
foreach ($disk in $disks) {
& Diskpart /s c:\diskpartA_$($disk.Number).txt
Remove-Item c:\diskpartA_$($disk.Number).txt
}
#Prepare striped Volume
$striped = "create volume stripe disk="
foreach ($disk in $disks) {
#join volumes together for strip set
$striped = $striped + $disk.Number + ","
}
$striped = $striped -replace ".$" #remove last ,
$striped = $striped +[char][int](13)+[char][int](10); #add CR/LF
$striped += "format fs=NTFS unit=$allocation label=`"$label`" quick "+[char][int](13)+[char][int](10); #Format Vol
$striped += "assign letter=$($letter) "+[char][int](13)+[char][int](10); #Assign Drive Letter
$striped += "exit"+[char][int](13)+[char][int](10);
$striped | Set-Content c:\diskpartB.txt -Encoding ASCII
#Execute striped Volume
& Diskpart /s c:\diskpartB.txt
Remove-Item c:\diskpartB.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment