Amazon EC2

From HeadBackup
Jump to: navigation, search

Intro

We use Amazon EC2 for the Collegedale Community Church Webcast.

We created batch file wrappers for the java based instance tools to start, monitor and stop a Wowza Media Server instance on an elastic (static) IP address. We pass our custom configuration parameters in a .zip file specified at instance launch time.

Amazon EC2 instance start, status and stop batch files

Problem: Need a user friendly way to start and stop an Amazon EC2 instance which uses a static IP. The IP assignment has to be done after the instance start command is issued using the instance id.

Solution: I created batch files on Windows Vista 32 bit edition to start, stop and check the status of a Wowza server EC2 instance. 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 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 but could be modified to launch multiple instances.

Alternative Solution: If you prefer to do something with automated cronjobs on Linux check out this post: http://blog.ianbeyer.com/archives/201

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 command works on Vista but you may need to replace it with 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 immediately. IP assignment does seem to work before the instance has finished starting though.

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