On EC2 Windows Server 2016, automatically mount instance storage when restarting the instance
I have an AWS EC2 instance based on an Amazon-provided AMI for Microsoft Windows Server 2016. I want to use the ephemeral Instance Storage. When I Start the instance, I can login and use the Window's Disk Management tool to mount my Instance Storage as a drive letter.
However, if I Stop and then Start the instance, the Instance Storage is no longer mounted when I login. I must again use the Window's Disk Management tool to remount the Instance Storage every time I restart the instance.
How can I make the Instance Storage appear as a drive automatically?
Solution 1:
FYI here is a simpler solution based on the AWS EC2 docs
Run the following from a PowerShell window.
#initial disk 1st time:
C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeDisks.ps1
# schedule initial disk
C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeDisks.ps1 -Schedule
Solution 2:
Configure the Amazon-provided script C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeDisks.ps1
to run when the system boots.
Log in as Administrator.
Create a temporary XML file with the contents shown below.
Start the Windows Task Scheduler. (Start >> Administrative Tools >> Task Scheduler)
Select Actions, Import Task...
Select your temporary XML file.
Press OK to create the task.
Amazon Ec2 Launch - Initialize Disks.xml
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.3" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<URI>\Amazon Ec2 Launch - Initialize Disks</URI>
</RegistrationInfo>
<Triggers>
<BootTrigger>
<Enabled>true</Enabled>
</BootTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>S-1-5-18</UserId>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
<AllowHardTerminate>false</AllowHardTerminate>
<StartWhenAvailable>true</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>false</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
<Priority>0</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>C:\Windows\System32\cmd.exe</Command>
<Arguments>/C C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -NonInteractive -NoLogo -ExecutionPolicy Unrestricted -File "C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeDisks.ps1"</Arguments>
</Exec>
</Actions>
</Task>
I figured this out with clues from here