How do I run a command on a new EC2 Windows instance at launch?
Last updated: 2020-10-12
I want to run a custom script when I launch a new Amazon Elastic Compute Cloud (Amazon EC2) Windows instance. How can I do this?
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 (Windows Server 2016 and later).
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 Running 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, the original instance that the AMI was created from must be shut down using EC2Launch or EC2Config. From the EC2Launch 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:
5. Complete the launch wizard to start the instance.
For additional troubleshooting, EC2Launch and EC2Config log files contain the output from the standard output and standard error streams. Log files are located as follows:
- EC2Launch: C:\ProgramData\Amazon\EC2-Windows\Launch\Log\UserdataExecution.log
- EC2Config: C:\Program Files\Amazon\Ec2ConfigService\Logs\Ec2Config.log