%3CLINGO-SUB%20id%3D%22lingo-sub-1479819%22%20slang%3D%22en-US%22%3ETagging%20Azure%20Resources%20with%20a%20Creator%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1479819%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CEM%3E%3CFONT%20color%3D%22%23FF0000%22%3E%3CSTRONG%3EUpdate%3A%3C%2FSTRONG%3E%3C%2FFONT%3E%20Objects%20created%20by%20a%20Service%20Principal%20will%20tag%20the%20objects%20with%20a%20GUID%20instead%20of%20a%20name%20by%20default.%20You%20can%20fix%20this%20behavior%20by%20giving%20the%20Managed%20Identity%20the%20Application%20Developer%20role%20in%20Azure%20Active%20Directory.%26nbsp%3B%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHello%20everyone%2C%20my%20name%20is%26nbsp%3B%3CA%20href%3D%22http%3A%2F%2Fwww.linkedin.com%2Fin%2Fanthony-watherston%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAnthony%20Watherston%3C%2FA%3E%26nbsp%3Band%20I%20am%20a%20Senior%20Premier%20Field%20Engineer%20in%20Melbourne%20Australia.%20A%20question%20was%20recently%20raised%20by%20a%20colleague%20about%20how%20we%20can%20tag%20an%20Azure%20resource%20with%20the%20name%20of%20the%20person%20who%20created%20it.%20This%20would%20prove%20especially%20helpful%20in%20the%20case%20of%20resources%20that%20are%20older%20than%20the%20default%20activity%20log%20retention%20time%20where%20the%20owner%20cannot%20be%20identified%20from%20the%20initial%20creation%20event.%20Azure%20Policy%20would%20normally%20be%20used%20to%20update%20tags%20however%20policies%20can%20only%20act%20on%20the%20properties%20of%20the%20resource%20itself%20%E2%80%93%20the%20caller%20is%20only%20exposed%20through%20the%20activity%20log%20event%20which%20creates%20the%20resource.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EEnter%20Event%20Grid%20Subscriptions%20and%20Azure%20Functions.%20Each%20time%20an%20activity%20occurs%20in%20an%20Azure%20subscription%20an%20event%20is%20generated%2C%20and%20we%20can%20subscribe%20to%20that%20event%20using%20an%20Event%20Grid%20Subscription%20%E2%80%93%20part%20of%20the%20information%20captured%20in%20this%20event%20will%20be%20the%20user%20who%20is%20making%20the%20call.%20The%20subscription%20will%20help%20me%20to%20identify%20that%20an%20event%20has%20occurred%2C%20and%20because%20it%20is%20only%20going%20to%20deploy%20a%20tag%20the%20code%20should%20be%20minimal%20so%20I%20can%20use%20an%20Azure%20Function.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%2C%20my%20flow%20looks%20like%20this%3A%20-%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Anthony_W_0-1592784724912.png%22%20style%3D%22width%3A%20734px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F199950iD60DB2863382B733%2Fimage-dimensions%2F734x175%3Fv%3D1.0%22%20width%3D%22734%22%20height%3D%22175%22%20title%3D%22Anthony_W_0-1592784724912.png%22%20alt%3D%22Anthony_W_0-1592784724912.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%20clone%20or%20download%20the%20project%20at%20-ERR%3AREF-NOT-FOUND-%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fanwather%2FTagWithCreator%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2Fanwather%2FTagWithCreator%3C%2FA%3E%20%26nbsp%3Band%20check%20out%20the%20code%20but%20let%20us%20go%20through%20the%20main%20parts.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20built%20my%20function%20using%20PowerShell%20because%20it%20is%20easiest%20for%20me%20%E2%80%93%20you%20can%20create%20a%20function%20by%20following%20the%20quick%20start%20guide%20at%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fanwather%2FTagWithCreator%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSTRONG%3EQuickstart%3A%20Create%20a%20function%20in%20Azure%20using%20Visual%20Studio%20Code%3C%2FSTRONG%3E%3C%2FA%3E%20but%20instead%20of%20selecting%20a%20HTTP%20trigger%20I%20chose%20an%20Event%20Grid%20trigger.%20The%20extension%20provided%20in%20Visual%20Studio%20Code%20will%20create%20all%20the%20necessary%20files%20and%20even%20provide%20you%20with%20a%20sample%20of%20what%20an%20Event%20Grid%20event%20looks%20like%20in%20a%20file%20called%20sample.dat.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20main%20script%20code%20is%20at%20%2Ffunctions%2FTagWithCreator%2Frun.ps1.The%20events%20are%20passed%20into%20the%20function%20as%20an%20object%20already%20so%20I%20can%20just%20get%20the%20fields%20I%20want%20out%20of%20the%20event%20and%20assign%20them%20to%20variables%20to%20use%20later.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24caller%26nbsp%3B%3D%26nbsp%3B%24eventGridEvent.data.claims.name%0AWrite-Host%26nbsp%3B%22Caller%3A%26nbsp%3B%24caller%22%0A%24resourceId%26nbsp%3B%3D%26nbsp%3B%24eventGridEvent.data.resourceUri%0AWrite-Host%26nbsp%3B%22ResourceId%3A%26nbsp%3B%24resourceId%22%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20script%20checks%20if%20a%20tag%20exists%20on%20the%20object%20before%20applying%20%E2%80%93%20if%20it%20needs%20to%20make%20change%20then%20I%20am%20using%20the%20Update-AzTag%20cmdlet%20to%20apply%20the%20tag.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3Eif%20(!(%24tags.TagsProperty.ContainsKey('Creator'))%20-or%20(%24null%20-eq%20%24tags))%20%7B%0A%20%20%20%20%24tag%20%3D%20%40%7B%0A%20%20%20%20%20%20%20%20Creator%20%3D%20%24caller%0A%20%20%20%20%7D%0A%20%20%20%20Update-AzTag%20-ResourceId%20%24resourceId%20-Operation%20Merge%20-Tag%20%24tag%0A%20%20%20%20Write-Host%20%22Added%20creator%20tag%20with%20user%3A%20%24caller%22%0A%7D%0Aelse%20%7B%0A%20%20%20%20Write-Host%20%22Tag%20already%20exists%22%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAs%20far%20as%20the%20code%20is%20concerned%20there%20is%20nothing%20else%20that%20is%20any%20different%20to%20a%20normal%20PowerShell%20script%20but%20be%20aware%20that%20it%20executes%20using%20PowerShell%206%20so%20any%20Windows%20PowerShell%20commands%20that%20are%20specific%20to%20Windows%20will%20not%20work.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20Azure%20Function%20handles%20all%20the%20connections%20to%20Azure%20using%20a%20managed%20identity%20which%20will%20be%20created%20when%20we%20deploy%20it%20%E2%80%93%20I%20also%20assign%20permissions%20to%20that%20identity%20so%20it%20can%20make%20the%20changes%20to%20the%20resources.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBy%20default%2C%20the%20scaffolded%20Azure%20Function%20will%20download%20the%20entire%20Az%20module%20on%20each%20cold%20start%20of%20the%20function%20which%20adds%20time%20to%20the%20execution%2C%20so%20to%20improve%20performance%20I%20specify%20each%20module%20and%20version%20required%20in%20requirements.psd1%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%23%20This%20file%20enables%20modules%20to%20be%20automatically%20managed%20by%20the%20Functions%20service.%0A%23%20See%20https%3A%2F%2Faka.ms%2Ffunctionsmanageddependency%20for%20additional%20information.%0A%23%0A%40%7B%0A%20%20%20%20'Az.Accounts'%20%20%3D%20'1.8.0'%0A%20%20%20%20'Az.Resources'%20%3D%20'2.0.1'%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EApart%20from%20writing%20the%20actual%20code%20in%20run.ps1%20and%20changing%20the%20requirements.psd1%20file%20there%20are%20no%20other%20changes%20to%20be%20made%20to%20the%20scaffolded%20code%20%E2%80%93%20easy!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EDeploying%20the%20Azure%20Function%20requires%20the%20following%20resources%20in%20Azure%20%E2%80%93%20a%20Storage%20Account%2C%20App%20Service%20Plan%2C%20Function%20App%2C%20Application%20Insights%2C%20and%20the%20Event%20Grid%20Subscription.%20The%20first%20four%20objects%20are%20created%20using%20an%20Azure%20Resource%20Manager%20template%20and%20I%20have%20put%20instructions%20to%20create%20the%20Event%20Grid%20Subscription%20later.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20steps%20to%20deploy%20the%20ARM%20template%20and%20function%20code%20are%20as%20below%3A%20-%3C%2FP%3E%0A%3CP%3E1.%20Ensure%20that%20you%20have%20the%20latest%20version%20of%20the%20Az%20PowerShell%20modules%20available.%3C%2FP%3E%0A%3CP%3E2.%20Connect%20to%20Azure%20using%20Connect-AzAccount%3C%2FP%3E%0A%3CP%3E3.%20Modify%20the%20%2Fenvironment%2Fdeploy.ps1%20script%20and%20change%20the%20values%20where%20indicated.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24resourceGroupName%20%3D%20%22awfunctionsdev%22%20%20%20%20%20%20%20%20%20%20%20%20%23%20%26lt%3B--%20REPLACE%20the%20variable%20values%20with%20your%20own%20values.%0A%24location%20%3D%20%22australiasoutheast%22%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20%26lt%3B--%20Ensure%20that%20the%20location%20is%20a%20valid%20Azure%20location%0A%24storageAccountName%20%3D%20%22awfunctionsdev%22%20%20%20%20%20%20%20%20%20%20%20%23%20%26lt%3B--%20Ensure%20the%20storage%20account%20name%20is%20unique%0A%24appServicePlanName%20%3D%20%22AustraliaSoutheastPlan%22%20%20%20%23%20%26lt%3B--%0A%24appInsightsName%20%3D%20%22awfunctionsdev%22%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20%26lt%3B--%0A%24functionName%20%3D%20%22awfunctionsdev%22%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20%26lt%3B--%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E4.%20From%20a%20PowerShell%20prompt%20change%20into%20the%20%2Fenvironment%20directory.%3C%2FP%3E%0A%3CP%3E5.%20Run%20the%20deploy.ps1%20script.%20The%20output%20should%20be%20like%20below.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222.gif%22%20style%3D%22width%3A%20962px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F199956i54CD39D71BA6EE0F%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%222.gif%22%20alt%3D%222.gif%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20script%20will%20create%20a%20resource%20group%2C%20deploy%20the%20ARM%20(Azure%20Resource%20Manager)%20template%2C%20change%20the%20permissions%20for%20the%20managed%20identity%20created%20for%20the%20app%20service%20and%20finally%20compress%20and%20publish%20the%20Azure%20Function.%20All%20that%20is%20left%20to%20do%20is%20create%20the%20Event%20Grid%20Subscription%20and%20start%20sending%20events.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20create%20the%20subscription%2C%20follow%20the%20steps%20below%3A%20-%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EIn%20the%20Azure%20Portal%20click%20in%20the%20search%20bar%20at%20the%20top%20and%20enter%20%E2%80%9Cevent%20grid%20subscriptions%E2%80%9D%20%E2%80%93%20select%20the%20item%20that%20appears%20with%20that%20name.%3C%2FLI%3E%0A%3CLI%3EClick%20the%20%2B%20button%20next%20to%20Event%20subscription%3C%2FLI%3E%0A%3CLI%3EFill%20out%20the%20details%20as%20in%20the%20image%20below%20with%20a%20couple%20of%20points%20to%20note%3A%20-%0A%3COL%20class%3D%22lia-list-style-type-lower-alpha%22%3E%0A%3CLI%3E%E2%80%9CTopic%20Type%E2%80%9D%20should%20be%20subscription%20and%20select%20your%20subscription%20%E2%80%93%20you%20will%20have%20to%20do%20this%20for%20each%20subscription%20you%20want%20to%20monitor.%3C%2FLI%3E%0A%3CLI%3E%E2%80%9CFilter%20To%20Event%20Types%E2%80%9D%20%E2%80%93%20you%20should%20only%20select%20%E2%80%9CResource%20Write%20Success%E2%80%9D%3C%2FLI%3E%0A%3CLI%3ESelect%20%E2%80%9CAzure%20Function%E2%80%9D%20as%20the%20%E2%80%9CEndpoint%20Type%E2%80%9D%20and%20then%20follow%20the%20pop-out%20blade%20to%20select%20your%20function.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3C%2FLI%3E%0A%3CLI%3EClick%20on%20%E2%80%9CFilters%E2%80%9D%3C%2FLI%3E%0A%3CLI%3EIn%20the%20%E2%80%9CAdvanced%20Filters%E2%80%9D%20section%20add%20a%20key%20and%20value%20as%20below.%20This%20will%20stop%20the%20subscription%20from%20sending%20events%20for%20when%20a%20tag%20is%20written%20and%20retriggering%20the%20function%20app.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Anthony_W_2-1592784725026.png%22%20style%3D%22width%3A%20736px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F199951iF2C8F62EFEE9B374%2Fimage-dimensions%2F736x756%3Fv%3D1.0%22%20width%3D%22736%22%20height%3D%22756%22%20title%3D%22Anthony_W_2-1592784725026.png%22%20alt%3D%22Anthony_W_2-1592784725026.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Anthony_W_3-1592784725028.png%22%20style%3D%22width%3A%20731px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F199953i8F86905F2FCD4FAF%2Fimage-dimensions%2F731x232%3Fv%3D1.0%22%20width%3D%22731%22%20height%3D%22232%22%20title%3D%22Anthony_W_3-1592784725028.png%22%20alt%3D%22Anthony_W_3-1592784725028.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ENote%3A%3C%2FSTRONG%3E%20There%20is%20also%20an%20ARM%20template%20to%20deploy%20the%20Event%20Grid%20subscription%20in%20the%20GitHub%20repository%20in%20the%20-ERR%3AREF-NOT-FOUND-eventgrid-arm%20branch.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETime%20to%20test%20%E2%80%93%20I%20can%20create%20a%20simple%20virtual%20machine%20using%20the%20New-AzVM%20command.%20As%20resources%20are%20created%2C%20they%20will%20send%20events%20which%20are%20picked%20up%20by%20the%20Event%20Grid%20Subscription.%20Each%20event%20is%20then%20sent%20to%20the%20Azure%20Function%20endpoint%20which%20applies%20the%20tag%20if%20it%20does%20not%20already%20exist.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Anthony_W_4-1592784725031.png%22%20style%3D%22width%3A%20735px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F199955iE498985BD51ADD30%2Fimage-dimensions%2F735x180%3Fv%3D1.0%22%20width%3D%22735%22%20height%3D%22180%22%20title%3D%22Anthony_W_4-1592784725031.png%22%20alt%3D%22Anthony_W_4-1592784725031.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%2C%20there%20it%20is%20%E2%80%93%20a%20way%20to%20tag%20resources%20with%20the%20name%20of%20the%20creator%2C%20it%20is%20important%20to%20point%20out%20a%20few%20caveats%20around%20this%20solution.%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EIt%20will%20not%20apply%20a%20tag%20if%20a%20creator%20tag%20already%20exists%3C%2FLI%3E%0A%3CLI%3EIf%20you%20deploy%20objects%20using%20a%20service%20principal%2C%20you%20may%20not%20see%20a%20name%20appear%20in%20the%20tag%20%E2%80%93%20instead%2C%20it%20will%20show%20the%20application%20Id%20for%20the%20service%20principal.%3C%2FLI%3E%0A%3CLI%3EIf%20an%20object%20is%20updated%20by%20any%20process%20this%20can%20cause%20the%20tag%20to%20be%20applied%20%E2%80%93%20it%20does%20not%20know%20if%20a%20resource%20is%20pre-existing%20before%20the%20function%20was%20implemented.%20If%20you%20wanted%20to%20deploy%20this%20in%20your%20environment%2C%20you%20could%20apply%20a%20policy%20to%20tag%20every%20object%20with%20a%20Creator%20tag%20and%20a%20default%20value%20before%20implementing%20the%20function.%20That%20way%20if%20someone%20makes%20changes%20to%20an%20already%20existing%20object%20the%20function%20will%20not%20alter%20the%20tag.%20You%20would%20then%20remediate%20all%20objects%20and%20disable%20the%20policy%20before%20creating%20the%20Event%20Grid%20subscription.%3C%2FLI%3E%0A%3CLI%3EIf%20you%20wanted%20to%20scope%20this%20to%20only%20tag%20virtual%20machines%20when%20they%20are%20created%20you%20could%20add%20another%20filter%20to%20the%20Event%20Grid%20subscription%2C%20like%20the%20image%20below.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Anthony_W_5-1592784725034.png%22%20style%3D%22width%3A%20732px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F199954iE94AA54FA718378A%2Fimage-dimensions%2F732x225%3Fv%3D1.0%22%20width%3D%22732%22%20height%3D%22225%22%20title%3D%22Anthony_W_5-1592784725034.png%22%20alt%3D%22Anthony_W_5-1592784725034.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EMy%20testing%20cases%20are%20small%20and%20in%20no%20way%20should%20reflect%20your%20own%20testing.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThanks%20for%20reading%20this%20post%2C%20if%20there%20are%20any%20issues%20or%20you%20would%20like%20to%20contribute%20to%20the%20code%20please%20submit%20a%20PR%20(pull%20request)%20or%20issue%20on%20the%20-ERR%3AREF-NOT-FOUND-Github%20page.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CFONT%20size%3D%221%202%203%204%205%206%207%22%3EDisclaimer%3A%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%221%202%203%204%205%206%207%22%3EThe%20sample%20scripts%20are%20not%20supported%20under%20any%20Microsoft%20standard%20support%20program%20or%20service.%20The%20sample%20scripts%20are%20provided%20AS%20IS%20without%20warranty%20of%20any%20kind.%20Microsoft%20further%20disclaims%20all%20implied%20warranties%20including%2C%20without%20limitation%2C%20any%20implied%20warranties%20of%20merchantability%20or%20of%20fitness%20for%20a%20particular%20purpose.%20The%20entire%20risk%20arising%20out%20of%20the%20use%20or%20performance%20of%20the%20sample%20scripts%20and%20documentation%20remains%20with%20you.%20In%20no%20event%20shall%20Microsoft%2C%20its%20authors%2C%20or%20anyone%20else%20involved%20in%20the%20creation%2C%20production%2C%20or%20delivery%20of%20the%20scripts%20be%20liable%20for%20any%20damages%20whatsoever%20(including%2C%20without%20limitation%2C%20damages%20for%20loss%20of%20business%20profits%2C%20business%20interruption%2C%20loss%20of%20business%20information%2C%20or%20other%20pecuniary%20loss)%20arising%20out%20of%20the%20use%20of%20or%20inability%20to%20use%20the%20sample%20scripts%20or%20documentation%2C%20even%20if%20Microsoft%20has%20been%20advised%20of%20the%20possibility%20of%20such%20damages.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1479819%22%20slang%3D%22en-US%22%3E%3CP%3EHow%20can%20we%20tag%20an%20Azure%20resource%20with%20the%20name%20of%20the%20person%20who%20created%20it%3F%20Let's%20find%20out...%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%221.png%22%20style%3D%22width%3A%20878px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F199949iB2078A4CA64B4B15%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%221.png%22%20alt%3D%221.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1479819%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAnthonyWatherston%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1482801%22%20slang%3D%22en-US%22%3ERe%3A%20Tagging%20Azure%20Resources%20with%20a%20Creator%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1482801%22%20slang%3D%22en-US%22%3E%3CP%3ENice%20one%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F397109%22%20target%3D%22_blank%22%3E%40Anthony_W%3C%2FA%3E%3C%2FP%3E%3CP%3EIs%20it%20a%20good%20opportunity%20to%20add%20%22created%20date%22%20as%20another%20tag%20at%20the%20same%20time%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1482813%22%20slang%3D%22en-US%22%3ERe%3A%20Tagging%20Azure%20Resources%20with%20a%20Creator%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1482813%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F707456%22%20target%3D%22_blank%22%3E%40JamesCave%3C%2FA%3E%26nbsp%3Babsolutely%20-%20very%20easy%20to%20do%20although%20I%20already%20have%20Azure%20Policy%20which%20does%20this%20for%20me.%20You%20could%20do%20it%20via%20the%20function%20though%20at%20the%20same%20time%20-%20just%20another%20line%20of%20code.%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3EUpdate-AzTag%20-ResourceId%20%24resourceId%20-Tag%20%40%7BCreatedDate%3D%24(Get-Date)%7D%20-Operation%20Merge%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1788567%22%20slang%3D%22en-US%22%3ERe%3A%20Tagging%20Azure%20Resources%20with%20a%20Creator%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1788567%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F397109%22%20target%3D%22_blank%22%3E%40Anthony_W%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20is%20fantastic!%26nbsp%3B%20Thank%20you.%26nbsp%3B%20I%20have%20deployed%20this%20into%20our%20tenant.%26nbsp%3B%20The%20only%20issue%20I%20am%20having%20is%20that%20all%20osdisks%2C%20either%20deployed%20from%20the%20marketplace%20or%20Devops%20are%20being%20tagged%20with%20the%20function%20name.%26nbsp%3B%20Have%20you%20seen%20this%20issue%3F%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1794054%22%20slang%3D%22en-US%22%3ERe%3A%20Tagging%20Azure%20Resources%20with%20a%20Creator%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1794054%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F709058%22%20target%3D%22_blank%22%3E%40lmpalermo%3C%2FA%3E%26nbsp%3B-%20I%20haven't%20seen%20that%2C%20are%20they%20being%20deployed%20in%20the%20same%20deployment%20as%20a%20virtual%20machine%3F%20My%20disks%20are%20tagged%20with%26nbsp%3B%3CSPAN%3E05250873-b7a8-4392-b112-cf3c65d72ee1%20which%20is%20the%20application%20Id%20for%20the%20Compute%20Resource%20Provider.%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Microsoft

 

Update: Objects created by a Service Principal will tag the objects with a GUID instead of a name by default. You can fix this behavior by giving the Managed Identity the Application Developer role in Azure Active Directory. 

 

Hello everyone, my name is Anthony Watherston and I am a Senior Premier Field Engineer in Melbourne Australia. A question was recently raised by a colleague about how we can tag an Azure resource with the name of the person who created it. This would prove especially helpful in the case of resources that are older than the default activity log retention time where the owner cannot be identified from the initial creation event. Azure Policy would normally be used to update tags however policies can only act on the properties of the resource itself – the caller is only exposed through the activity log event which creates the resource.

 

Enter Event Grid Subscriptions and Azure Functions. Each time an activity occurs in an Azure subscription an event is generated, and we can subscribe to that event using an Event Grid Subscription – part of the information captured in this event will be the user who is making the call. The subscription will help me to identify that an event has occurred, and because it is only going to deploy a tag the code should be minimal so I can use an Azure Function.

 

So, my flow looks like this: -

Anthony_W_0-1592784724912.png

 

You can clone or download the project at https://github.com/anwather/TagWithCreator  and check out the code but let us go through the main parts.

 

I built my function using PowerShell because it is easiest for me – you can create a function by following the quick start guide at Quickstart: Create a function in Azure using Visual Studio Code but instead of selecting a HTTP trigger I chose an Event Grid trigger. The extension provided in Visual Studio Code will create all the necessary files and even provide you with a sample of what an Event Grid event looks like in a file called sample.dat.

 

The main script code is at /functions/TagWithCreator/run.ps1.The events are passed into the function as an object already so I can just get the fields I want out of the event and assign them to variables to use later.

 

 

 

 

$caller = $eventGridEvent.data.claims.name
Write-Host "Caller: $caller"
$resourceId = $eventGridEvent.data.resourceUri
Write-Host "ResourceId: $resourceId"

 

 

 

 

The script checks if a tag exists on the object before applying – if it needs to make change then I am using the Update-AzTag cmdlet to apply the tag.

 

 

 

 

if (!($tags.TagsProperty.ContainsKey('Creator')) -or ($null -eq $tags)) {
    $tag = @{
        Creator = $caller
    }
    Update-AzTag -ResourceId $resourceId -Operation Merge -Tag $tag
    Write-Host "Added creator tag with user: $caller"
}
else {
    Write-Host "Tag already exists"
}

 

 

 

 

As far as the code is concerned there is nothing else that is any different to a normal PowerShell script but be aware that it executes using PowerShell 6 so any Windows PowerShell commands that are specific to Windows will not work.

 

The Azure Function handles all the connections to Azure using a managed identity which will be created when we deploy it – I also assign permissions to that identity so it can make the changes to the resources.

 

By default, the scaffolded Azure Function will download the entire Az module on each cold start of the function which adds time to the execution, so to improve performance I specify each module and version required in requirements.psd1

 

 

 

 

# This file enables modules to be automatically managed by the Functions service.
# See https://aka.ms/functionsmanageddependency for additional information.
#
@{
    'Az.Accounts'  = '1.8.0'
    'Az.Resources' = '2.0.1'
}

 

 

 

 

Apart from writing the actual code in run.ps1 and changing the requirements.psd1 file there are no other changes to be made to the scaffolded code – easy!

 

Deploying the Azure Function requires the following resources in Azure – a Storage Account, App Service Plan, Function App, Application Insights, and the Event Grid Subscription. The first four objects are created using an Azure Resource Manager template and I have put instructions to create the Event Grid Subscription later.

 

The steps to deploy the ARM template and function code are as below: -

1. Ensure that you have the latest version of the Az PowerShell modules available.

2. Connect to Azure using Connect-AzAccount

3. Modify the /environment/deploy.ps1 script and change the values where indicated.

 

 

 

$resourceGroupName = "awfunctionsdev"            # <-- REPLACE the variable values with your own values.
$location = "australiasoutheast"                 # <-- Ensure that the location is a valid Azure location
$storageAccountName = "awfunctionsdev"           # <-- Ensure the storage account name is unique
$appServicePlanName = "AustraliaSoutheastPlan"   # <--
$appInsightsName = "awfunctionsdev"              # <--
$functionName = "awfunctionsdev"                 # <--

 

 

 

 

4. From a PowerShell prompt change into the /environment directory.

5. Run the deploy.ps1 script. The output should be like below.

 

2.gif

 

This script will create a resource group, deploy the ARM (Azure Resource Manager) template, change the permissions for the managed identity created for the app service and finally compress and publish the Azure Function. All that is left to do is create the Event Grid Subscription and start sending events.

 

To create the subscription, follow the steps below: -

  1. In the Azure Portal click in the search bar at the top and enter “event grid subscriptions” – select the item that appears with that name.
  2. Click the + button next to Event subscription
  3. Fill out the details as in the image below with a couple of points to note: -
    1. “Topic Type” should be subscription and select your subscription – you will have to do this for each subscription you want to monitor.
    2. “Filter To Event Types” – you should only select “Resource Write Success”
    3. Select “Azure Function” as the “Endpoint Type” and then follow the pop-out blade to select your function.
  4. Click on “Filters”
  5. In the “Advanced Filters” section add a key and value as below. This will stop the subscription from sending events for when a tag is written and retriggering the function app.

 

Anthony_W_2-1592784725026.png

 

Anthony_W_3-1592784725028.png

 

Note: There is also an ARM template to deploy the Event Grid subscription in the GitHub repository in the eventgrid-arm branch.

 

Time to test – I can create a simple virtual machine using the New-AzVM command. As resources are created, they will send events which are picked up by the Event Grid Subscription. Each event is then sent to the Azure Function endpoint which applies the tag if it does not already exist.

 

Anthony_W_4-1592784725031.png

 

So, there it is – a way to tag resources with the name of the creator, it is important to point out a few caveats around this solution.

  • It will not apply a tag if a creator tag already exists
  • If you deploy objects using a service principal, you may not see a name appear in the tag – instead, it will show the application Id for the service principal.
  • If an object is updated by any process this can cause the tag to be applied – it does not know if a resource is pre-existing before the function was implemented. If you wanted to deploy this in your environment, you could apply a policy to tag every object with a Creator tag and a default value before implementing the function. That way if someone makes changes to an already existing object the function will not alter the tag. You would then remediate all objects and disable the policy before creating the Event Grid subscription.
  • If you wanted to scope this to only tag virtual machines when they are created you could add another filter to the Event Grid subscription, like the image below.

 

Anthony_W_5-1592784725034.png

 

  • My testing cases are small and in no way should reflect your own testing.

 

Thanks for reading this post, if there are any issues or you would like to contribute to the code please submit a PR (pull request) or issue on the Github page.

 

Disclaimer:

The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.

 

4 Comments
Visitor

Nice one @Anthony_W

Is it a good opportunity to add "created date" as another tag at the same time?

Microsoft

@JamesCave absolutely - very easy to do although I already have Azure Policy which does this for me. You could do it via the function though at the same time - just another line of code. 

Update-AzTag -ResourceId $resourceId -Tag @{CreatedDate=$(Get-Date)} -Operation Merge
New Contributor

@Anthony_W 

 

This is fantastic!  Thank you.  I have deployed this into our tenant.  The only issue I am having is that all osdisks, either deployed from the marketplace or Devops are being tagged with the function name.  Have you seen this issue?  

Microsoft

@lmpalermo - I haven't seen that, are they being deployed in the same deployment as a virtual machine? My disks are tagged with 05250873-b7a8-4392-b112-cf3c65d72ee1 which is the application Id for the Compute Resource Provider.