I use windows instances on Amazon EC2 frequently for different tasks, and I wanted a quick and easy way to be able fire up one or more instances to execute a specific program. One caveat is that Amazon has a service that configures aspects of the instance after sysprep and after it starts up for the first time. This configuration needs at least 1 reboot. The indicator for when you can actually proceed with what you need to do is that "Windows is ready to use" will be output to the Ec2ConfigLog and to the console. So I created a service, which I call EC2WinInit, that will wait for the "Windows is ready to use" message and then will kick off any arbitrary executable you want. In addition, I created several other utilites to use in conjunction with this service that can run a program in an interactive session or download and execute arbitrary packages that you create from either HTTP or S3. This gives a lot of flexibility for automating Windows EC2 instances.
NOTE: I know that amazon has added the ability to add a <script> or <powershell> command to user data when launching an instance. I wanted something a bit more flexible that had the option of baking-in configuration scripts/packages.
Here are the utilities that I created:
- EC2WinInit service - This is a windows service that will monitor the instance for the "Windows is ready to use" message. Once found, it will execute whatever program is specified in its config file.
- ConfigInteractive.exe - This program will create a new administrator user on the machine named " WinInitUser " with a random password. It will then set the AutoAdminLogon registry keys to login as this user, and set a Run or RunOnce key to whatever program is specified and then reboot the machine. You can use this if you need to auto-execute a program in an interactive session. Simply specify this exe as the program to run in the EC2WinInit service.
- DAR.exe - This program takes a URL as a command line parameter and it will download, extract, and execute the file specified in the url. The url specified can be either http or s3. If the file specified is a zip file, DAR will extract the file. If a "run.cmd" batch file is included in the zip file, it will be executed. If a "dar.xml" file is included in the zip file, it will be parsed and the program specified in the XML will be execute. It is also possible to include a .NET DLL (implementing a specific interface) and this program will load and call the assembly.
- DARFromMetadata.exe - this program will read the EC2 instance user data, look for a tag and then a tag within it, and execute DAR.exe with the url specified in the user data.
Installing the Service and Basic Usage
- Download the utilities here: EC2WinInit.zip
- Launch a windows instance and configure it as desired
Create a folder on the machine for the EC2WinInit service such as
- Extract the zip file downloaded in step 1 into the folder
Open an administrator command prompt run the following command (This will install the service):
sc create EC2WinInitService binPath= "C:\Program Files\EC2WinInit\EC2WinInitService.exe" start= auto
Also from the command prompt, run the following command
Open the c:\Program Files\EC2WinInit\EC2WinInitService.exe.config file
Here you can customize the program that will be run when "Windows is ready to use". This program will be executed by the service in non-interactive mode. If you need to run something in an interactive session, leave the command to be "DAR\ConfigInteractive.exe". If you want to download a DAR package specified in the metadata, change the command to be "DAR\ DARFromMetadata.exe". Or specify you own command and command arguments.
If you specified "DAR\ConfigInteractive.exe" in the step above, edit the ConfigInteractive.exe.config file location in the directory. Customize the RunCommand and/or RunOnceCommand and the respective arguments to your liking. If you want to download a DAR package specified in the metadata, leave the RunOnceCommand to be "DAR\DARFromMetadata.exe".
- Run "ec2configservice settings" from the start menu
The following are recommended settings for the ec2config service:
- Check off "set the computer name of the instance to ip-<hex Internal IP> name."
- Check off "set a random password on next boot"
- Check off "enable SetPassword feature"
- Click the Bundle tab and "Run sysprep and Shutdown Now"
- After the machine shuts down, create an AMI image
Creating a dar package
NOTE: you do not need to use the DAR program to use the EC2WinInit service. This just provides extra functionality if you want it.
To create a dar package, simply create a run.cmd file or a dar.xml file (see below) and zip it up along with any other files you will need for your script/program to run. Place this file on a web server or s3 bucket, and when you run DAR.exe with the url as a paramter, the zip file will be downloaded, extracted and executed.
Example dar.xml file
<?xml version="1.0" encoding="utf-8"?>
<DarManifest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
Example DAR package
Click here to download an example dar package
Specifying a dar package in user data
The DARFromMetadata.exe will look for specific tags specified in user data of the instance and launch DAR.exe with the url specified in the user data. To use this, simply include the following string in your instance user data: