Attach EBS Volume to Windows EC2 with Powershell Attach EBS Volume to Windows EC2 with Powershell powershell powershell

Attach EBS Volume to Windows EC2 with Powershell


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!