%3CLINGO-SUB%20id%3D%22lingo-sub-1296880%22%20slang%3D%22en-US%22%3EAuto%20Startup%20and%20Shutdown%20of%20Azure%20VMs%20Using%20Azure%20Automation%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1296880%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EIntroduction%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EBusinesses%20always%20focus%20on%20performance%20and%20cost.%20It%20does%20not%20matter%20if%20the%20business%20is%20an%20enterprise%2C%20medium%20sized%20or%20a%20startup%20company.%20All%20of%20us%20like%20to%20save%20money%20and%20get%20good%20deal.%20With%20the%20cloud%20the%20cost%20can%20add%20up%20very%20quickly%20and%20easy.%20Customers%20are%20always%20looking%20for%20automating%20ways%20to%20both%20advise%20or%20help%20them%20monitor%20their%20cloud%20usage.%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Fazure.microsoft.com%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%22%3EAzure%3C%2FA%3E%20provides%20many%20resources%20to%20help%20customers%20monitor%20their%20usage.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOne%20of%20the%20ways%20to%20save%20money%20is%20to%20shutdown%20VMs%20if%20they%20are%20not%20needed.%20Azure%20provides%20a%20feature%20called%20%3CA%20href%3D%22https%3A%2F%2Fazure.microsoft.com%2Fen-us%2Fupdates%2Fset-auto-shutdown-within-a-couple-of-clicks-for-vms-using-azure-resource-manager%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EVM%20Auto%20Shutdown%3C%2FA%3E.%20However%20when%20we%20deal%20with%20a%20large%20scale%20of%20VMs%20count%20for%20example%20if%20you%20have%20thousands%20of%20Resource%20Groups%20that%20have%20hundreds%20of%20VMs%2C%20it%20will%20be%20very%20difficult%20to%20go%20to%20each%20VM%20individually%20and%20schedule%20it%20to%20shutdown.%20Of%20course%2C%20you%20can%20script%20it%2C%20but%20then%20you%20will%20have%20to%20maintain%20the%20script%20and%20modify%20it%20every%20time%20your%20user%20wants%20to%20change%20the%20shutdown%20time.%20You%20will%20also%20need%20to%20maintain%20the%20process%20for%20starting%20the%20VMs%20back%20up.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOne%20of%20the%20great%20Azure%20offering%20is%20%3CA%20href%3D%22https%3A%2F%2Fazure.microsoft.com%2Fen-us%2Fservices%2Fautomation%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20Automation%3C%2FA%3E.%20In%20this%20PoC%20we%20will%20demonstrate%20how%20to%20use%20Webapp%20with%20REST%20API%20to%20automated%20Azure%20Tasks.%20%26nbsp%3BWe%20will%20utilize%20many%20different%20Azure%20services.%20This%20demo%20will%20focus%20on%20start%20and%20stop%20VMs%20for%20multi%20resource%20groups%20with%20multi-schedules%20or%20OnDemand.%3C%2FP%3E%0A%3CP%3EThe%20source%20code%20for%20this%20demo%20can%20be%20found%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fmsalemcode%2Fazure_VM_Scheduler%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehere%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EGetting%20Started%3C%2FSTRONG%3E%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EDesign%20Diagram%3C%2FLI%3E%0A%3CLI%3ESoftware%20Requirement%3C%2FLI%3E%0A%3CLI%3EHow%20to%20install%20it%3C%2FLI%3E%0A%3CLI%3EReference%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%3CSTRONG%3EDesign%20Diagram%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EThe%20following%20diagram%20show%20how%20the%20different%20services%20will%20interact%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EWeb%20App%3A%20where%20user%20can%20login%20with%20their%20AD%20login%20and%20see%20their%20resource%20groups%3C%2FLI%3E%0A%3CLI%3ERest%20API%3A%20It%20will%20communicate%20with%20automation%20services%3C%2FLI%3E%0A%3CLI%3EKey%20Vault%3A%20to%20store%20secret%20keys%3C%2FLI%3E%0A%3CLI%3EAutomation%20services%3A%20It%20will%20manage%20the%20start%20and%20stop%20VMs%20based%20on%20the%20defined%20schedule%3C%2FLI%3E%0A%3CLI%3EAAD%3A%20to%20authenticate%20users%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22magdysalem_0-1586466816766.jpeg%22%20style%3D%22width%3A%20464px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F183489iDBEFA5DFB09924F5%2Fimage-dimensions%2F464x370%3Fv%3D1.0%22%20width%3D%22464%22%20height%3D%22370%22%20title%3D%22magdysalem_0-1586466816766.jpeg%22%20alt%3D%22magdysalem_0-1586466816766.jpeg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EAutomation%20Account%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EThe%20automation%20account%20will%20manage%20the%20runbook%20and%20schedule%20and%20it.%20Automation%20account%20will%20create%3CU%3E%26nbsp%3BService%20Principle%20Account%3C%2FU%3E.%20It%20is%20very%20important%20to%20capture%20this%20SP.%3C%2FP%3E%0A%3CP%3EThe%20Automation%20Service%20Principle%20account%26nbsp%3Bmust%20assign%20contributor%20role%20over%20any%20subscription%20where%20user%20can%20schedule%20stop%2Fstart%20for%20his%20VM(s)%3C%2FP%3E%0A%3CP%3E%3CU%3EWe%20will%20need%20to%20capture%20SP%20ApplicationID%20and%20Secret%20and%20store%20it%20in%20Azure%20Key%20Vault%3C%2FU%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EAzure%20Runbooks%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3Ewe%20will%20need%20two%20runbooks.%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EOne%20for%20start%20VM%20calls%26nbsp%3BStart-AzureV2VMs%26nbsp%3Band%20source%20code%20can%20be%20found%20under%26nbsp%3B%3CFONT%20color%3D%22%230000FF%22%3Edocs%5Cstart-script.ps%3C%2FFONT%3E%3C%2FLI%3E%0A%3CLI%3EOne%20for%20stop%20VM%20calls%26nbsp%3BStop-AzureV2VMsand%20source%20code%20can%20be%20found%20under%26nbsp%3B%3CFONT%20color%3D%22%230000FF%22%3Edocs%5Cstop-script.ps%3C%2FFONT%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EAzure%20Key%20Vault%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EThe%20key%20vault%20will%20used%20to%20store%20Azure%20Management%20API%20endpoint%2C%20Also%20other%20sensitive%20configuration%20stored%20there%20like%20web%20app%20SP%20for%20graph%20and%20resources%2C%20automation%20account%20SP.%20Please%20refer%20to%20README%20under%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fmagdysalem2%2Fazure_vms_automation_management%2Fblob%2Fmaster%2FREADME.md%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Erepo%3C%2FA%3E%20to%20get%20full%20list%20of%20required%20keys%20and%20it%20is%20expected%20value.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EAzure%20Container%20Registry%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EACR%20is%20required%20to%20store%20the%20automation%20api%20app%20container%20and%20web%20app%20container.%26nbsp%3B%3CU%3EPlease%20enable%20username%20and%20password%26nbsp%3B%3C%2FU%3Eso%20user%20can%20user%20docker%20to%20login%20to%20the%20ACR%20username%20and%20password%20to%20build%20container%20and%20push%20the%20code.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EAD%20App%20Registration%20for%20automation%20web%20app%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3ERegister%20AAD%20app%20and%20grant%20permission%20for%20Azure%20Graph%20API%20User%20read%20profile.%20This%20SP%20will%20allow%20the%20Web%20App%20to%20get%20user%20profile%20information.%20Grant%20also%20Azure%20Management%20resource%20to%20allow%20the%20webapp%20to%20authenticate%20with%20AAD.%26nbsp%3B%3CSTRONG%3Eplease%20record%20the%20APPID%20and%20Secret%20information%26nbsp%3B%3C%2FSTRONG%3Eto%20add%20to%20Azure%20Key%20Vault%20also%20to%20grant%20access%20policy%20to%20Azure%20Key%20Vault.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EAzure%20App%20Plan%20for%20Linux%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EThe%20resource%20will%20host%20the%20Web%20and%20Rest%20API%20apps.%20so%20the%20resource%20required%20to%20be%20at%20least%2014GB%20for%20better%20performance.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EAzure%20Web%20App%20for%20containers%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EAutomation%20REST%20API%20app%3A%20Linux%20base%20web%20app%20to%20host%20the%20automation%20API%20container.%26nbsp%3B%3CSTRONG%3EIdentity%20must%20be%20enabled%3C%2FSTRONG%3E%26nbsp%3Bthe%20system%20will%20generate%20a%20GUID%20once%20Identity%20is%20recorded.%20please%20record%20the%20GUID%20to%20enable%20it%20with%26nbsp%3B%3CSTRONG%3EAzure%20Key%20Vault%3C%2FSTRONG%3E.%20The%20web%20API%20will%20run%20using%20automation%20SP%20identity.%20The%20web%20API%20will%20read%20the%20identity%20from%20Azure%20Key%20Vault.%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSTRONG%3EThe%20following%20app%20settings%20need%20to%20be%20added%3A%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CTABLE%20width%3D%22646%22%3E%0A%3CTHEAD%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22217%22%3E%3CP%3E%3CSTRONG%3ESetting%20Name%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CSTRONG%3EDescription%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTHEAD%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22217%22%3E%3CP%3EAUTHENTICATION_ENDPOINT%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Flogin.microsoftonline.com%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Flogin.microsoftonline.com%2F%3C%2FA%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22217%22%3E%3CP%3EKEY_VAULT%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3EKV%20endpoint%20URL%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22217%22%3E%3CP%3ERESOURCE%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fmanagement.core.windows.net%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fmanagement.core.windows.net%2F%3C%2FA%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22217%22%3E%3CP%3ERESOURCE_KV%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fvault.azure.net%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fvault.azure.net%3C%2FA%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22217%22%3E%3CP%3EWEBSITES_PORT%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3EWeb%20app%20port%20default%20to%205000%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAutomation%20web%20app%3A%20Linux%20base%20web%20app%20to%20host%20the%20automation%20web%20container.%26nbsp%3B%3CSTRONG%3EIdentity%20must%20be%20enabled%3C%2FSTRONG%3E%26nbsp%3Bthe%20system%20will%20generate%20a%20GUID%20once%20identity%20is%20recorded.%20please%20record%20the%20GUID%3C%2FP%3E%0A%3CP%3ETo%20enable%20it%20with%26nbsp%3B%3CSTRONG%3EAzure%20Key%20Vault%3C%2FSTRONG%3E.%20The%20web%20app%20will%20run%20using%20Graph%20SP%20identity.%20The%20web%20API%20will%20read%20the%20identity%20from%20Azure%20Key%20Vault.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EThe%20following%20app%20settings%20need%20to%20be%20added%3A%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CTABLE%20width%3D%22597%22%3E%0A%3CTHEAD%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22240%22%3E%3CP%3E%3CSTRONG%3ESetting%20Name%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CSTRONG%3EDescription%3C%2FSTRONG%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTHEAD%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22240%22%3E%3CP%3EAPI_VERSION%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E1.0%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22240%22%3E%3CP%3EAUTHENTICATION_ENDPOINT%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Flogin.microsoftonline.com%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Flogin.microsoftonline.com%2F%3C%2FA%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22240%22%3E%3CP%3EENCRYPTION_KEY%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3EWeb%20application%20auto%20generation%20encryption%20key%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22240%22%3E%3CP%3EKEY%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3EWeb%20SP%20for%20Graph%20secret%20key%20--%20it%20will%20be%20removed%20in%20future%20release%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22240%22%3E%3CP%3ECLIENT%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3EWeb%20SP%20for%20Graph%20AppID%20--%20it%20will%20be%20removed%20in%20future%20release%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22240%22%3E%3CP%3EKEY_VAULT%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3EKV%20endpoint%20URL%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22240%22%3E%3CP%3EOAUTHLIB_INSECURE_TRANSPORT%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3ETrue%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22240%22%3E%3CP%3EOAUTHLIB_RELAX_TOKEN_SCOPE%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3ETrue%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22240%22%3E%3CP%3ERESOURCE%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fmanagement.core.windows.net%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fmanagement.core.windows.net%2F%3C%2FA%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22240%22%3E%3CP%3ERESOURCE_GRAPH%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fgraph.microsoft.com%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgraph.microsoft.com%3C%2FA%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22240%22%3E%3CP%3ERESOURCE_KV%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fvault.azure.net%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fvault.azure.net%3C%2FA%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22240%22%3E%3CP%3EREST_API_ENDPOINT%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3EAutomation%20REST%20API%20web%20app%20URL%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22240%22%3E%3CP%3ESUBSCRIPTION_ID%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3EWeb%20App%20Subscription%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22240%22%3E%3CP%3ETENANT%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3E%3CTENANTNAME%3E.onmicrosoft.com%3C%2FTENANTNAME%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22240%22%3E%3CP%3ETENANT_ID%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3Eyour%20tenant%20ID%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22240%22%3E%3CP%3EWEBSITES_PORT%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3EWeb%20app%20port%20default%20to%208000%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%3CBR%20%2F%3EOnce%20the%20web%20app%20is%20generated%20please%20record%20the%20web%20url%20and%20modify%20the%20AAD%20SP%20for%20web%20app%20by%20adding%20the%20web%20app%20url%20under%20%22Redirect%20URLS%22%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EAzure%20Storage%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EAzure%20storage%20Account%20will%20be%20needed%20to%20enable%20log%20stream.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ESoftware%20Requirement%3C%2FSTRONG%3E%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EREST%20API%20will%20be%20built%20in%20Python%20and%20container%20after%3C%2FLI%3E%0A%3CLI%3EWeb%20App%20will%20be%20in%20Python%20Django%3C%2FLI%3E%0A%3CLI%3EApp%20plans%20for%20Linux%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EHow%20to%20install%20it%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E1-%20Make%20sure%20the%20assets%20installed%20and%20configured%3C%2FP%3E%0A%3CP%3E2-%20All%20the%20app%20setting%20and%20key%20vault%20secrets%20in%20place%3C%2FP%3E%0A%3CP%3E3-%20Log%20into%20Azure%20ACR%20using%20%60docker%20login%60%20and%20provide%20the%20username%20and%20password.%3C%2FP%3E%0A%3CP%3E4-%20clone%20git%20repo%3C%2FP%3E%0A%3CP%3E5-%20change%20directory%20to%20%22%3CEM%3Esrc%5Capi%3C%2FEM%3E%22%20and%20run%20docker%20build%20file%20%22%3CEM%3Edocker%20build%20-t%20%3CACR%20url%3D%22%22%3E%5C%3CAUTOMATION-API-WEB-NAME%3E%3Atag%20.%20%22%3C%2FAUTOMATION-API-WEB-NAME%3E%3C%2FACR%3E%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E6-%20Run%20docker%20push%26nbsp%3B%20%3CEM%3E%22docker%20push%20%3CACR%20url%3D%22%22%3E%5C%3CAUTOMATION-API-WEB-NAME%3E%3Atag%22%3C%2FAUTOMATION-API-WEB-NAME%3E%3C%2FACR%3E%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E7-%20From%20the%20portal%20go%20to%20webapp%20instance%20and%20modify%20the%20Container%20Setting%20to%20reflect%20the%20container%20name%20and%20tag.%3C%2FP%3E%0A%3CP%3E8-%20Restart%20the%20WebAPP%20%2F%20Web%20API%3C%2FP%3E%0A%3CP%3E9-%20In%20many%20case%20clear%20the%20cache%20to%20see%20the%20new%20changes.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EReference%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Faspnet%2FHome%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAutomation%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FMicrosoft%2Fvscode%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EApp%20Services%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FMicrosoft%2FChakraCore%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EKey%20Vault%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EActive%20Directory%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FUL%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1296880%22%20slang%3D%22en-US%22%3E%3CP%3EOne%20of%20the%20great%20Azure%20offering%20is%20%3CA%20href%3D%22https%3A%2F%2Fazure.microsoft.com%2Fen-us%2Fservices%2Fautomation%2F%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%20target%3D%22_blank%22%3EAzure%20Automation%3C%2FA%3E.%20In%20this%20blog%20we%20will%20demonstrate%20how%20to%20use%20Webapp%20with%20REST%20API%20to%20automated%20schedule%20to%20stop%20and%20start%20Azure%20VM.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1296880%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EMagdySalem%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

 

Introduction

Businesses always focus on performance and cost. It does not matter if the business is an enterprise, medium sized or a startup company. All of us like to save money and get good deal. With the cloud the cost can add up very quickly and easy. Customers are always looking for automating ways to both advise or help them monitor their cloud usage. Azure provides many resources to help customers monitor their usage.

 

One of the ways to save money is to shutdown VMs if they are not needed. Azure provides a feature called VM Auto Shutdown. However when we deal with a large scale of VMs count for example if you have thousands of Resource Groups that have hundreds of VMs, it will be very difficult to go to each VM individually and schedule it to shutdown. Of course, you can script it, but then you will have to maintain the script and modify it every time your user wants to change the shutdown time. You will also need to maintain the process for starting the VMs back up.

 

One of the great Azure offering is Azure Automation. In this PoC we will demonstrate how to use Webapp with REST API to automated Azure Tasks.  We will utilize many different Azure services. This demo will focus on start and stop VMs for multi resource groups with multi-schedules or OnDemand.

The source code for this demo can be found here

 

Getting Started

  1. Design Diagram
  2. Software Requirement
  3. How to install it
  4. Reference

Design Diagram

The following diagram show how the different services will interact

  1. Web App: where user can login with their AD login and see their resource groups
  2. Rest API: It will communicate with automation services
  3. Key Vault: to store secret keys
  4. Automation services: It will manage the start and stop VMs based on the defined schedule
  5. AAD: to authenticate users

magdysalem_0-1586466816766.jpeg

 

Automation Account

The automation account will manage the runbook and schedule and it. Automation account will create Service Principle Account. It is very important to capture this SP.

The Automation Service Principle account must assign contributor role over any subscription where user can schedule stop/start for his VM(s)

We will need to capture SP ApplicationID and Secret and store it in Azure Key Vault

 

Azure Runbooks

we will need two runbooks.

  • One for start VM calls Start-AzureV2VMs and source code can be found under docs\start-script.ps
  • One for stop VM calls Stop-AzureV2VMsand source code can be found under docs\stop-script.ps

 

Azure Key Vault

The key vault will used to store Azure Management API endpoint, Also other sensitive configuration stored there like web app SP for graph and resources, automation account SP. Please refer to README under repo to get full list of required keys and it is expected value.

 

Azure Container Registry

ACR is required to store the automation api app container and web app container. Please enable username and password so user can user docker to login to the ACR username and password to build container and push the code.

 

AD App Registration for automation web app

Register AAD app and grant permission for Azure Graph API User read profile. This SP will allow the Web App to get user profile information. Grant also Azure Management resource to allow the webapp to authenticate with AAD. please record the APPID and Secret information to add to Azure Key Vault also to grant access policy to Azure Key Vault.

 

Azure App Plan for Linux

The resource will host the Web and Rest API apps. so the resource required to be at least 14GB for better performance.

 

Azure Web App for containers

Automation REST API app: Linux base web app to host the automation API container. Identity must be enabled the system will generate a GUID once Identity is recorded. please record the GUID to enable it with Azure Key Vault. The web API will run using automation SP identity. The web API will read the identity from Azure Key Vault.

The following app settings need to be added:

Setting Name

Description

AUTHENTICATION_ENDPOINT

https://login.microsoftonline.com/

KEY_VAULT

KV endpoint URL

RESOURCE

 https://management.core.windows.net/

RESOURCE_KV

https://vault.azure.net

WEBSITES_PORT

Web app port default to 5000

 

Automation web app: Linux base web app to host the automation web container. Identity must be enabled the system will generate a GUID once identity is recorded. please record the GUID

To enable it with Azure Key Vault. The web app will run using Graph SP identity. The web API will read the identity from Azure Key Vault.

 

The following app settings need to be added:

Setting Name

Description

API_VERSION

1.0

AUTHENTICATION_ENDPOINT

https://login.microsoftonline.com/

ENCRYPTION_KEY

Web application auto generation encryption key

KEY

Web SP for Graph secret key -- it will be removed in future release

CLIENT

Web SP for Graph AppID -- it will be removed in future release

KEY_VAULT

KV endpoint URL

OAUTHLIB_INSECURE_TRANSPORT

True

OAUTHLIB_RELAX_TOKEN_SCOPE

True

RESOURCE

https://management.core.windows.net/

RESOURCE_GRAPH

https://graph.microsoft.com

RESOURCE_KV

https://vault.azure.net

REST_API_ENDPOINT

Automation REST API web app URL

SUBSCRIPTION_ID

Web App Subscription

TENANT

<tenantname>.onmicrosoft.com

TENANT_ID

your tenant ID

WEBSITES_PORT

Web app port default to 8000


Once the web app is generated please record the web url and modify the AAD SP for web app by adding the web app url under "Redirect URLS"

 

Azure Storage

Azure storage Account will be needed to enable log stream.

 

Software Requirement

  1. REST API will be built in Python and container after
  2. Web App will be in Python Django
  3. App plans for Linux

 

How to install it

1- Make sure the assets installed and configured

2- All the app setting and key vault secrets in place

3- Log into Azure ACR using `docker login` and provide the username and password.

4- clone git repo

5- change directory to "src\api" and run docker build file "docker build -t <ACR URL>\<automation-api-web-name>:tag . "

6- Run docker push  "docker push <ACR URL>\<automation-api-web-name>:tag"

7- From the portal go to webapp instance and modify the Container Setting to reflect the container name and tag.

8- Restart the WebAPP / Web API

9- In many case clear the cache to see the new changes.

 

Reference