amazon ec 2 cloud automation: scheduled startup/shutdown of amazon ec2 instances
DESCRIPTION
One of the obvious benefits of cloud computing is the pay as you go model; and only paying for what you use. Amazon EC2 Cloud images offer users great flexibility in this regard by allowing instances to be easily turned off or terminated when not in-use. Since Amazon instance usage is metered you may have large chunks of time where you do not wish to “leave the clock running”, and prefer to bring the instance down (which will stop the billing). This document provides step by step directions for scheduling automated instance stops and starts, so you can easily reduce the overall cost of the instance. Although the focus of this document was initiating scheduled stop and start actions from a Windows Workstation. Unix cron commands can also be utilized to achieve the same result.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×.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