Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attach EBS Volume to Windows EC2 with Powershell

I've seen a lot of questions answered about adding EBS volumes Linux, but not Windows. Say you've discovered that your disk is running low on space (maybe via CloudWatch) and want to add another EBS volume. Can this be done with Powershell?

I'd prefer not to use diskpart.exe since it's more difficult to parse its results (not being a native Powershell command).

like image 940
Jonathan Grubb Avatar asked Dec 29 '16 16:12

Jonathan Grubb


1 Answers

Hoping this helps someone out there. The AWS stuff was easy, but took me a while to track down all the things for Windows to use it.

This answer is stripped down for brevity, so make sure:

  1. you've handled the AWS Powershell API exceptions
  2. your volumes are "available" before to try to attach them to an EC2
  3. the volume shows "in-use" once you've attached it

2 and 3 can be done via the Get-EC2Volume API.

Create the EBS Volume:

$volume = New-EC2Volume -Size $sizeInGB -AvailabilityZone $az -VolumeType $vType

Attach the Volume to the EC2:

Add-EC2Volume -InstanceId $toInstanceId -VolumeId $volume.Id -Device $devId -Region $region

Windows side:

locate the ebs volume you just attached

$diskNumber = (Get-Disk | ? { 
    ($_.OperationalStatus -eq "Offline") -and ($_."PartitionStyle" -eq "RAW") }).Number

initialize the disk

Initialize-Disk -Number $diskNumber -PartitionStyle "MBR"

create max-space partition, assign drive letter, make "active"

$part = New-Partition -DiskNumber $diskNumber -UseMaximumSize -IsActive -AssignDriveLetter

format the new drive

Format-Volume -DriveLetter $part.DriveLetter -Confirm:$FALSE

Enjoy!

like image 162
Jonathan Grubb Avatar answered Sep 20 '22 09:09

Jonathan Grubb