Vinnie
Vinnie

Reputation: 489

How to start a program on instance start on AWS EC2

I have a AWS EC2 Windows (2008 R2 instance) which I want to start-stop using command/script from my local machine and schedule as per my usage. I also want couple of my programs running on the EC2 instance to get start when instance starts. These programs currently are started using a bat file present in the instance.

I did following till now for the same:

1- I have an AWS user created in AWS IAM and using auth_id and key for that user for using EC2 apis and command line utilities.

2- To start and stop instance I'm using command line utilities from EC2 Util.

start ->ec2-start-instances i-instanceID

stop  ->ec2-stop-instances i-instanceID

3- To schedule it I've added this to my windows scheduler.

4- Added user data for the instance in the AWS management console. My user data looks like this:

<script>
C:\Services\my_application.lnk
</script>

5- I can see the user data is present in my EC2 instance at C:\ProgramFiles\Amazon\Ec2ConfigServer\Scripts\UserScript

6- In C:\Program Files\Amazon\Ec2ConfigService\Settings\confi.xml the values of Ec2SetPassword and Ec2HandleUserData were changed to enabled and added true was added as well.

I'm facing following issues:

1- The user data scripts does not execute every time the instance is started. I'm not able to figure out why. 2- The changes made in Ec2ConfigService\Settings\confi.xml are getting reverted to the default values when the instance is restarted.

I feel this is common use case, and would like to know the best practices and approach taken for automating EC2 operations. I also need help in starting programs on my instance- where am I going wrong or missing, what else needs to be done etc?

Upvotes: 1

Views: 5829

Answers (1)

Pete - MSFT
Pete - MSFT

Reputation: 4309

userdata is only executed the very first time that the instance is created. This is by design.

You've got a couple of options - all of which use your userdata script

  1. Copy the my_application.lnk to the startup folder
  2. Register the application in the registry "run" start key (http://blogs.msdn.com/b/powershell/archive/2006/04/25/how-to-access-or-modify-startup-items-in-the-window-registry.aspx)
  3. Register it with the task scheduler to configure it to execute on startup (http://technet.microsoft.com/en-us/library/bb490996.aspx)

Upvotes: 2

Related Questions