+ All Categories
Transcript
  • Automating Amazon EC2 CloudScheduled Startup/Shutdown of Amazon EC2 Instances

    Author:JasonAllowayCompany:AllTechSolutionsLLC

  • Table of Contents

    IntroductionManuallychangingtheImagerunningstateAutomatingtheImagerunningstate

    ObtainandConfigureasupportedJAVAJREObtainandConfiguretheAmazonEC2APIToolsSchedulingaStopInstanceTaskinWindowsTaskScheduler

    Appendix

  • IntroductionOneoftheobviousbenefitsofcloudcomputingisthepayasyougomodelandonlypayingforwhatyouuse.AmazonEC2Cloudimagesofferusersgreatflexibilityinthisregardbyallowinginstancestobeeasilyturnedofforterminatedwhennotinuse.SinceAmazoninstanceusageismeteredyoumayhavelargechunksoftimewhereyoudonotwishtoleavetheclockrunning,andprefertobringtheinstancedown(whichwillstopthebilling).Thisdocumentprovidesstepbystepdirectionsforschedulingautomatedinstancestopsandstarts,soyoucaneasilyreducetheoverallcostoftheinstance.AlthoughthefocusofthisdocumentwasinitiatingscheduledstopandstartactionsfromaWindowsWorkstation.Unixcroncommandscanalsobeutilizedtoachievethesameresult.

    Manually changing the Image running stateTheTheAmazonEC2dashboardallowsausertoeasilyinitiatestop,start,andterminateinstanceactionsfromawebbrowsersessionbynavigatingtoINSTANCES,selectingthecheckboxnexttotheinstance,andthenselectingtheACTIONSdropdownmenu.TheoptionstoTerminate,Reboot,Stop,andStartappearinthemenu:

  • Automating the Image running stateItmaybeimpracticalforausertomanuallystop,andstarttheAmazonEC2imagesmanuallyforavarietyofreasons.FortunatelyAmazonmakesavailableasetofCLI(CommandLineInterface)commandsthatwhencombinedwithaschedulereasilyautomatesstopandstartactionsatfixeddates&times.AsofwritingtheCLIToolsareasubsetoftheAmazonEC2APIToolsv1.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 JREIfyouneedtodetermineifyoualreadyhaveaJREinstalledusetheWindowsControlPaneltonotethelocationwhereitisinstalled

  • Create2newSystemEnvironmentVariableentriesontheworkstationthatwillbeusedtorunthescheduledAmazonCLIcommands.FirstcreateanewSystemVariablecalledJAVA_HOME.ThevaluewillbethedirectorycontainingyourJAVAJREbinaries.Forexample:

  • Note:IfyouhavespacesinthepaththenenclosethevalueindoublequotationmarksNote: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 ToolsTheAmazonEC2APIToolsarecurrentlymadeavailableasaZIParchive.AfterdownloadingthemfromAmazon,simplyextractthearchivetoadestinationfolderonyourworkstation.

    SeveralmoreSystemEnvironmentVariableswillneedtobecreated.FirstcreateanewSystemVariablecalledEC2_HOME.ThevaluewillbethedirectorythatcontainstheAmazonEC2supportinglibrariesthatwerejustunarchivedForexample:

  • TestthenewconfigurationbyopeninganewCMDpromptandissuingthecommand:

    dir%EC2_HOME%

    YoushouldseethecontentsoftheAmazonEC2APIToolsoutputtothescreen:

    Next,edittheSystemEnvironmentVariablePathandaddthevalue:%EC2_HOME%\bin

    TestthenewconfigurationbyopeninganewCMDpromptandissuingthecommand:

    ec2describeregions

    YoushouldseetheAmazonEC2Cloudregionsoutputtothescreen:

  • Next,definetheAWS_ACCESS_KEYandAWS_SECRET_KEYSystemEnvironmentVariables.Thevaluesforthese2variablescanberetrievedfromtheAmazonEC2WebServicessiteathttp://aws.amazon.com,clickMYACCOUNT/CONSOLE,andselectSECURITYCREDENTIALS:

    ExpandtheAccessKeyssectiononthefollowingscreen:

  • HereyoucannoteyourAccessKeyID,butasofthetimeofwritingyoumustfollowthelinkinthelowerrighttoobtaintheSecretAccessKey:

    Usethese2valuestoaddtheSystemEnvironmentVariables:

  • Finally,bydefaulttheAmazonEC2CLIToolsusetheuseast1AmazonCloudregionwiththeserviceendpointurl:ec2.useast1.amazonaws.com.Ifyourinstance(s)resideinadifferentregiondefinetheSystemEnvironmentVariableEC2_URL.Forexample:

    AtthispointtheAmazonEC2CLIToolsareinstalledandcommandscanbeissuedfromtheCMDpromptinanydirectory.

    Scheduling a Stop Instance Task in Windows Task SchedulerLaunchtheWindowsTaskScheduler,andchooseCREATEBASICTASK:

  • TheCreateBasicTaskWizardwillLaunchandguideyouthroughcreatingthetaskcreationandscheduling.WhenpromptedenteranameanddescriptionandclickNEXT:

    SelectyourtriggerfrequencyandclickNEXT,forthisexamplewearechoosingDaily:

  • Youwillbepromptedtoprovidemoredetailonthethedates,time,andfrequencyyouwantthetasktorun.ClickNEXT:

    ChoosetoStartaprogram,andclickNEXT:

  • ProvidetheAmazonEC2CLIcommandthatwillstoparunninginstanceintheProgram/Scriptfield,andtheAmazonEC2instanceIDoftheinstancethatwillbetargetedintheAddarguments(optional)field.Thecommandis:

    ec2stopinstances

    ClickNEXT

    Note:TheInstanceIDcanberetrievedfromtheAmazonEC2Dashboard

    ReviewthedetailsofthetaskandclickFINISH:

  • Congratulations,theconfigurationneededtostoparunninginstanceofanAmazonEC2Cloudimageisnowcomplete,andthenewtaskwillappearintheTaskSchedulerLibrary.Tovalidatetheconfiguration,thetaskcanbeexecutedimmediatelywhilestillintheTaskSchedulerUI.Rightclickthenewtask,andselectRUN:

    ReturntotheAmazonEC2Dashboardandverifythestatushaschangedfromrunningtostoppedbyrefreshingthescreen:

    Atasktoscheduleatasktostartastoppedinstancecanbecreatedusingthesamesteps.Simplyreplacethecommandec2stopinstanceswithec2startinstances.

    Note:InorderfortheAmazonEC2commandstoexecutesuccessfullyfromtheWindowsworkstation,TheAmazonCloudmustbereachableviaafunctioninginternetconnection.Ifyouareoperatingbehindaproxy,thereareadditionalstepsneededtocompletenotcoveredinthisdocument.

  • AppendixThedownloadlocationfortheAmazonEC2APIToolsasofwritingishere:http://aws.amazon.com/developertools/351

    ThemostrecentfulllistofallAmazonCLICommandsislocatedhere:http://docs.aws.amazon.com/AWSEC2/latest/CommandLineReference/commandreference.html

    TheOracleJAVAJREDownloadsiteishere:http://www.oracle.com/technetwork/java/javase/downloads/index.html


Top Related