Skip to content

Instantly share code, notes, and snippets.

@lantrix
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save lantrix/97bc8f3d494681652ba8 to your computer and use it in GitHub Desktop.

Select an option

Save lantrix/97bc8f3d494681652ba8 to your computer and use it in GitHub Desktop.
Spin up an AWS test instance with powershell
#setup variables
$ami = "ami-e95c31d3" #public Windows server 2012 R2
$subnet = "subnet-e1a4f488" #VPC Subnet for instance
$group = "sg-e5f3e889" #whatever security group
$folder = 'C:\Users\me\OneDrive'
$allVolumesAvailable = $False
$readyCount = 0
$maxRetries = 20
$sleepBetweenChecks = 10
$Retries = 0
#Create instance
try {
$instance = New-EC2Instance -ImageId $ami -KeyName MyKey -InstanceType t2.small -SubnetId $subnet
} catch {
throw "Need to have instance created before continuing"
}
write-host "New Instance: $($instance.Instances.InstanceId)"
write-host "New Instance: $($instance.Instances.PrivateIpAddress)"
#Wait for Instance to be running
while ((Get-EC2InstanceStatus -InstanceIds $instance.Instances.InstanceId).InstanceState.Name.Value -ne "running") {
write-host "Waiting for instance to be running"
sleep $sleepBetweenChecks
}
#Create Volums
$newVolumes = @()
$vol1 = New-EC2Volume -VolumeType standard -Size 1 -AvailabilityZone ap-southeast-2a
$newVolumes += $vol1
$vol2 = New-EC2Volume -VolumeType standard -Size 1 -AvailabilityZone ap-southeast-2a
$newVolumes += $vol2
$vol3 = New-EC2Volume -VolumeType standard -Size 1 -AvailabilityZone ap-southeast-2a
$newVolumes += $vol3
#Wait for Volumes to be available before attaching
#If they have only just been created we will have to wait for them to be completed ('available').
$totalVolumes = $newVolumes.Count
while(($readyCount -lt $totalVolumes) -and ($Retries -lt $maxRetries)){
Write-Host "Determining if new volumes are ready to be attached"
foreach($newVolume in $newVolumes){
Write-Host "Volume $($newVolume.VolumeId) $($newVolume.Size)GB state: $($newVolume.State.Value)"
if($newVolume.State.Value -eq 'available'){
$readyCount++
} else {
$NewVolumes = Get-EC2Volume -VolumeIds $NewVolumes.VolumeId
$readyCount = 0
Write-Host "$(Get-Date) Volumes are not yet available. Sleeping for $sleepBetweenChecks second(s). Check $Retries of $maxRetries"
}
}
Start-Sleep $sleepBetweenChecks
$Retries++
}
if($Retries -eq $maxRetries){
Throw "Volumes are in an unexpected state. Cannot continue!"
}
Write-Host "All volumes are now available."
#Create Tags
$newtags = @()
$t = New-Object Amazon.EC2.Model.Tag
$t.Key = 'Owner'
$t.Value = 'Lantrix'
$newtags += $t
$t = New-Object Amazon.EC2.Model.Tag
$t.Key = 'Name'
$t.Value = 'DiskTest'
$newtags += $t
#Tag all resources
New-EC2Tag -Resources @( $instance.Instances.InstanceId, $vol1.VolumeId, $vol2.VolumeId, $vol3.VolumeId ) -Tags $newtags
#Attach Volumes to instance
Add-EC2Volume -InstanceId $instance.Instances.InstanceId -VolumeId $vol1.VolumeId -Device 'xvdd'
Add-EC2Volume -InstanceId $instance.Instances.InstanceId -VolumeId $vol2.VolumeId -Device 'xvde'
Add-EC2Volume -InstanceId $instance.Instances.InstanceId -VolumeId $vol3.VolumeId -Device 'xvdf'
#Wait for password data
$password = $null
while ($password -eq $null) {
try { $password = Get-EC2PasswordData -InstanceId $instance.Instances.InstanceId -PemFile $folder\MyKey.pem -Decrypt }
catch {
"$(Get-Date) Waiting for PasswordData to be available"
Sleep -Seconds 10
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment