You are on page 1of 16

Automating Amazon EC2 Cloud

Scheduled Startup/Shutdown of Amazon EC2 Instances

Author:JasonAlloway Company:AllTechSolutionsLLC

Table of Contents
Introduction ManuallychangingtheImagerunningstate AutomatingtheImagerunningstate ObtainandConfigureasupportedJAVAJRE ObtainandConfiguretheAmazonEC2APITools SchedulingaStopInstanceTaskinWindowsTaskScheduler Appendix

Introduction
Oneoftheobviousbenefitsofcloudcomputingisthepayasyougomodelandonlypayingfor whatyouuse.AmazonEC2Cloudimagesofferusersgreatflexibilityinthisregardbyallowing instancestobeeasilyturnedofforterminatedwhennotinuse.SinceAmazoninstanceusage ismeteredyoumayhavelargechunksoftimewhereyoudonotwishtoleavetheclock running,andprefertobringtheinstancedown(whichwillstopthebilling).Thisdocument providesstepbystepdirectionsforschedulingautomatedinstancestopsandstarts,soyoucan easilyreducetheoverallcostoftheinstance.Althoughthefocusofthisdocumentwasinitiating scheduledstopandstartactionsfromaWindowsWorkstation.Unixcroncommandscanalso beutilizedtoachievethesameresult.

Manually changing the Image running state


TheTheAmazonEC2dashboardallowsausertoeasilyinitiatestop,start,andterminate instanceactionsfromawebbrowsersessionbynavigatingtoINSTANCES,selectingthe checkboxnexttotheinstance,andthenselectingtheACTIONSdropdownmenu.Theoptions toTerminate,Reboot,Stop,andStartappearinthemenu:

Automating the Image running state


Itmaybeimpracticalforausertomanuallystop,andstarttheAmazonEC2imagesmanually foravarietyofreasons.FortunatelyAmazonmakesavailableasetofCLI(CommandLine Interface)commandsthatwhencombinedwithaschedulereasilyautomatesstopandstart actionsatfixeddates&times.AsofwritingtheCLIToolsareasubsetoftheAmazonEC2API Toolsv1.6.7.3.Thedownloadlocationisintheappendixsectionofthisdocument. Thefollowingtechnologiesareusedinthisdocument: AmazonEC2APIToolsv1.6.7.3 AmazonEC2MachineImageamid0f89fb9(ubuntu-precise-12.04-amd64-server) MicrosoftTaskScheduler1.0 JAVAJREv1.7.0_17 Windowsv7Professionalworkstation

Obtain and Configure a supported JAVA JRE


IfyouneedtodetermineifyoualreadyhaveaJREinstalledusetheWindowsControlPanelto notethelocationwhereitisinstalled

Create2newSystemEnvironmentVariableentriesontheworkstationthatwillbeusedtorun thescheduledAmazonCLIcommands.FirstcreateanewSystemVariablecalled JAVA_HOME.ThevaluewillbethedirectorycontainingyourJAVAJREbinaries.Forexample:

Note:Ifyouhavespacesinthepaththenenclosethevalueindoublequotationmarks Note:Donotincludethebindirectory.TheAmazonCLIToolswillnotworkifbinisincluded Testtheentrybyopeninganewcommandpromptandissuingthecommand: %JAVA_HOME%\bin\javaversion YoushouldseetheJAVAVersiondisplayed

Next,editthePATHSystemVariablebyadding:%JAVA_HOME%\bin

Note:EnsuretheentryappearsbeforeanymentionofotherJREsonthesystemPath

TesttheconfigurationbyopeninganewCMDpromptandissuingthecommand:javaversion YoushouldseetheJAVAversiondisplayed

Obtain and Configure the Amazon EC2 API Tools


TheAmazonEC2APIToolsarecurrentlymadeavailableasaZIParchive.Afterdownloading themfromAmazon,simplyextractthearchivetoadestinationfolderonyourworkstation. SeveralmoreSystemEnvironmentVariableswillneedtobecreated.FirstcreateanewSystem VariablecalledEC2_HOME.ThevaluewillbethedirectorythatcontainstheAmazonEC2 supportinglibrariesthatwerejustunarchivedForexample:

TestthenewconfigurationbyopeninganewCMDpromptandissuingthecommand: dir%EC2_HOME% YoushouldseethecontentsoftheAmazonEC2APIToolsoutputtothescreen:

Next,edittheSystemEnvironmentVariablePathandaddthevalue:%EC2_HOME%\bin TestthenewconfigurationbyopeninganewCMDpromptandissuingthecommand: ec2describeregions YoushouldseetheAmazonEC2Cloudregionsoutputtothescreen:

Next,definetheAWS_ACCESS_KEYandAWS_SECRET_KEYSystemEnvironmentVariables. Thevaluesforthese2variablescanberetrievedfromtheAmazonEC2WebServicessiteat http://aws.amazon.com,clickMYACCOUNT/CONSOLE,andselectSECURITY CREDENTIALS:

ExpandtheAccessKeyssectiononthefollowingscreen:

HereyoucannoteyourAccessKeyID,butasofthetimeofwritingyoumustfollowthelinkin thelowerrighttoobtaintheSecretAccessKey:

Usethese2valuestoaddtheSystemEnvironmentVariables:

Finally,bydefaulttheAmazonEC2CLIToolsusetheuseast1AmazonCloudregionwiththe serviceendpointurl:ec2.useast1.amazonaws.com.Ifyourinstance(s)resideinadifferent regiondefinetheSystemEnvironmentVariableEC2_URL.Forexample:

AtthispointtheAmazonEC2CLIToolsareinstalledandcommandscanbeissuedfromthe CMDpromptinanydirectory.

Scheduling a Stop Instance Task in Windows Task Scheduler


LaunchtheWindowsTaskScheduler,andchooseCREATEBASICTASK:

TheCreateBasicTaskWizardwillLaunchandguideyouthroughcreatingthetaskcreationand scheduling.WhenpromptedenteranameanddescriptionandclickNEXT:

SelectyourtriggerfrequencyandclickNEXT,forthisexamplewearechoosingDaily:

Youwillbepromptedtoprovidemoredetailonthethedates,time,andfrequencyyouwantthe tasktorun.ClickNEXT:

ChoosetoStartaprogram,andclickNEXT:

ProvidetheAmazonEC2CLIcommandthatwillstoparunninginstanceintheProgram/Script field,andtheAmazonEC2instanceIDoftheinstancethatwillbetargetedintheAddarguments (optional)field.Thecommandis: ec2stopinstances ClickNEXT

Note:TheInstanceIDcanberetrievedfromtheAmazonEC2Dashboard ReviewthedetailsofthetaskandclickFINISH:

Congratulations,theconfigurationneededtostoparunninginstanceofanAmazonEC2Cloud imageisnowcomplete,andthenewtaskwillappearintheTaskSchedulerLibrary.Tovalidate theconfiguration,thetaskcanbeexecutedimmediatelywhilestillintheTaskSchedulerUI. Rightclickthenewtask,andselectRUN:

ReturntotheAmazonEC2Dashboardandverifythestatushaschangedfromrunningto stoppedbyrefreshingthescreen:

Atasktoscheduleatasktostartastoppedinstancecanbecreatedusingthesamesteps. Simplyreplacethecommandec2stopinstanceswithec2startinstances. Note:InorderfortheAmazonEC2commandstoexecutesuccessfullyfromtheWindows workstation,TheAmazonCloudmustbereachableviaafunctioninginternetconnection.Ifyou areoperatingbehindaproxy,thereareadditionalstepsneededtocompletenotcoveredinthis document.

Appendix
ThedownloadlocationfortheAmazonEC2APIToolsasofwritingishere: http://aws.amazon.com/developertools/351 ThemostrecentfulllistofallAmazonCLICommandsislocatedhere: http://docs.aws.amazon.com/AWSEC2/latest/CommandLineReference/commandreference.ht ml TheOracleJAVAJREDownloadsiteishere: http://www.oracle.com/technetwork/java/javase/downloads/index.html

You might also like