I want to run a custom script when I launch a new Amazon Elastic Compute Cloud (Amazon EC2) Windows instance.
Short description
To run a script that starts when the instance launches, add the script to user data. User data is processed by EC2Config (Windows Server 2012 R2 and earlier) or EC2Launch or EC2Launch V2 (Windows Server 2016 and later).
Resolution
When you add the script to user data, you must enclose it within a special tag. This tag determines whether the commands run in a command prompt window or use Windows PowerShell. For more information, see Run commands on your Windows instance at launch.
When you launch a new EC2 Windows instance, you can specify user data during configuration to run a custom script at startup.
Important: If you launch an instance from a custom AMI, then you must shut down the original instance that created the AMI. To do this, use EC2Launch, EC2Launch V2, or EC2Config. From the EC2Launch, EC2Launch V2, or EC2Config settings, choose Shutdown with Sysprep or Shutdown without Sysprep.
1. Open the Amazon EC2 console, and then choose AMIs from the navigation pane.
2. Select an AMI, and then choose Launch.
3. Select an instance type, and then choose Next: Configure Instance Details.
4. For Advanced Details, enter your custom script in the User data text box. Be sure to use the correct tag.
Note: To run user data scripts every time you reboot or restart the instance, add the following command:
<persist>true</persist>
5. Complete the launch wizard to start the instance.
For additional troubleshooting, EC2Launch, EC2Launch V2, and EC2Config log files contain the output from the standard output and standard error streams. You can find log files in the following locations:
- EC2Launch: C:\ProgramData\Amazon\EC2-Windows\Launch\Log\UserdataExecution.log
- EC2Launch V2: C:\ProgramData\Amazon\EC2Launch\log\agent.log
- EC2Config: C:\Program Files\Amazon\Ec2ConfigService\Logs\Ec2Config.log
Related information
How do I run a command on an existing EC2 Windows instance when I reboot or start the instance?