Amazon EC2

From HeadBackup
Revision as of 19:28, 10 December 2009 by Andrew (talk | contribs) (→‎Start)
Jump to navigationJump to search

Amazon EC2 instance start, status and stop batch files

I currently use these batch files to start a Wowza server EC2 instance on Vista 32 bit edition. I have created shortcuts to the batch files using the following syntax so that the command prompts stay open after a user double clicks the shortcut.

C:\Windows\System32\cmd.exe /k c:\ec2\start.bat

The tricky part here was reading in the instance ID in order to automatically assign the static IP after the instance launch. These batch files assume that you will only launch one instance at a time.

Start

The following batch file will start an Amazon instance and automatically assign your elastic (static) IP address to it after the instance ID is assigned by Amazon. Just replace any environment or instance preferences and change to your own elastic IP. The most tricky part of this is the for command which first launches the instance and then extracts the instance id from line 2 (line 1 is skipped), field (token) 2. The instance id is then stored in a variable and used to assign the elastic IP address to it after a delay.

@echo off
set JAVA_HOME="c:\Program Files (x86)\Java\jre6"
set EC2_HOME=c:\ec2
set PATH=%PATH%;%EC2_HOME%\bin
set EC2_PRIVATE_KEY=c:\ec2\keys\private.pem
set EC2_CERT=c:\ec2\keys\509.pem

echo to check status run status.bat (startup usually take a couple of minutes)

for /f "skip=1 tokens=2" %%i in ('c:\ec2\bin\ec2-run-instances ami-af51b0c6 -k wowza-keypair -t m1.small -f c:\ec2\includes\live.zip') do set INSTANCE=%%i
echo.
echo waiting 30 seconds before assigning static IP
timeout /t 30 /nobreak
c:\ec2\bin\ec2-associate-address -i %INSTANCE% 123.123.123.123

The timeout commands works on Vista but you may need to replace it with a something else on other OS flavors (ping perhaps?). At first I did not have a delay at all, but ran into sporadic trouble where the instance ID might not be recognized by ec2 if you try to assign an IP to it too soon.

Status

This batch file simply checks the current instance status.

@echo off
set JAVA_HOME="c:\Program Files (x86)\Java\jre6"
set EC2_HOME=c:\ec2
set PATH=%PATH%;%EC2_HOME%\bin
set EC2_PRIVATE_KEY=c:\ec2\keys\private.pem
set EC2_CERT=c:\ec2\keys\509.pem

echo The instance id is i-something

c:\ec2\bin\ec2-describe-instances

Stop

This one finds the instance ID and then stops that instance. If you have multiple instances running it will stop each of them one by one (it will also generate some errors, but worked when I tested it).

@echo off
set JAVA_HOME="c:\Program Files (x86)\Java\jre6"
set EC2_HOME=c:\ec2
set PATH=%PATH%;%EC2_HOME%\bin
set EC2_PRIVATE_KEY=c:\ec2\keys\private.pem
set EC2_CERT=c:\ec2\keys\509.pem

echo After you stop the server verify that the status shows terminated with the status.bat command

for /f "skip=1 tokens=2" %%i in ('c:\ec2\bin\ec2-describe-instances') do c:\ec2\bin\ec2-terminate-instances %%i