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:
- you've handled the AWS Powershell API exceptions
- your volumes are "available" before to try to attach them to an EC2
- 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!