Home
%3CLINGO-SUB%20id%3D%22lingo-sub-713466%22%20slang%3D%22en-US%22%3EHow%20to%20Create%20Azure%20Monitor%20Alerts%20for%20Non-Compliant%20Azure%20Policies%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-713466%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAzure%20Policy%20allows%20organization%20to%20define%20and%20enforce%20how%20Azure%20resources%20should%20or%20should%20not%20be%20used.%20It%20allows%20for%20the%20ability%20to%20block%20resources%20with%20certain%20configurations%20from%20being%20created%2C%20or%20generate%20audit%20events%20when%20a%20particular%20configuration%20is%20used.%20It%20can%20also%20modify%20resources%20at%20creation%20stage%20to%20ensure%20it%20meets%20standards%20set%20by%20your%20organization.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAzure%20Policy%20can%20be%20used%20to%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3ERestrict%20users%20only%20use%20a%20list%20of%20Azure%20regions%20approved%20by%20your%20organization%3C%2FLI%3E%0A%3CLI%3EAudit%20if%20a%20Public%20IP%20is%20being%20used%3C%2FLI%3E%0A%3CLI%3EEnforce%20Hybrid%20Use%20Benefit%20is%20enabled%20on%20all%20Windows%20Server%20VMs%3C%2FLI%3E%0A%3CLI%3EAutomatically%20deploy%20the%20Log%20Analytics%20agent%20on%20any%20newly%20created%20VMs%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20list%20can%20go%20on%20and%20on.%20Currently%20Azure%20provides%20over%20one%20hundred%20built-in%20policy%20definitions%20that%20can%20be%20utilized%20to%20consume%20free%20of%20charge%2C%20and%20also%20provides%20the%20ability%20to%20develop%20your%20own%20policy%20definitions%20if%20none%20of%20the%20built-in%20policy%20meets%20your%20requirements.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAzure%20policy%20engine%20evaluates%20policy%20assignments%20when%20ARM%20resources%20are%20created%20or%20updated%20within%20the%20assignment%20scope.%20It%20also%20runs%20delta%20and%20full%20scans%20on%20a%20schedule%20%E2%80%93%20to%20detect%20non-compliant%20resources.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EA%20resource%20can%20be%20marked%20as%20non-compliant%20by%20audit%20policies%2C%20or%20detected%20by%20the%20periodic%20scans%20(for%20example%2C%20resources%20that%20were%20created%20before%20the%20policies%20were%20assigned).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20the%20Azure%20portal%2C%20you%20can%20view%20the%20list%20of%20non-compliant%20policies%20(as%20shown%20below)%3A%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F120046i5D3428FD31CE1F0F%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%221.png%22%20title%3D%221.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EAzure%20Monitor%20can%20generate%20alerts%20and%20trigger%20pre-defined%20actions%20when%20non-complaint%20resources%20are%20detected.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20blog%20post%20detail%20how%20to%20use%20Kusto%20query%20language%20to%20detect%20non-compliant%20resources%20by%20querying%20your%20Log%20Analytics%20workspace%2C%20and%20then%20how%20to%20create%20Azure%20monitor%20alert%20rules%20to%20take%20actions%20when%20non-compliant%20resources%20are%20detected%20by%20Azure%20Policy.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20Azure%20policy%20compliance%20status%20is%20logged%20in%20the%20Azure%20subscription%E2%80%99s%20Activity%20logs.%20The%20Azure%20Log%20Analytics%20workspace%20can%20be%20configured%20to%20collect%20Azure%20Activity%20logs%20from%20any%20subscriptions%20in%20the%20same%20tenant.%20Azure%20Monitor%20alert%20rules%20can%20then%20be%20created%20to%20execute%20queries%20in%20the%20Log%20Analytics%20workspace%20on%20a%20schedule%20and%20generate%20alerts%20when%20non-compliant%20resources%20are%20detected%20by%20the%20query.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBefore%20we%20start%20creating%20alert%20rules%2C%20we%20need%20to%20make%20sure%20Azure%20Activity%20logs%20are%20being%20collected%20by%20a%20Log%20Analytics%20workspace.%20To%20configure%20a%20Log%20Analytics%20workspace%20to%20collect%20Azure%20Activity%20logs%2C%20you%20need%20to%20navigate%20to%20the%20%3CSTRONG%3E%3CEM%3EAzure%20Activity%20log%3C%2FEM%3E%3C%2FSTRONG%3E%20blade%20of%20the%20workspace%20in%20the%20portal%2C%20and%20connect%20the%20subscriptions%20(as%20shown%20below)%3A%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F120057i8B7A24D0F4E1E7BA%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%222.png%22%20title%3D%222.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOnce%20the%20Log%20Analytics%20workspace%20started%20collecting%20logs%2C%20the%20policy%20compliance%20status%20will%20be%20collected%20to%20your%20workspace.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20following%20Kusto%20queries%20can%20be%20used%20to%20check%20these%20logs%20in%20your%20Log%20Analytics%20workspace%3A%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EGet%20a%20list%20of%20non-compliant%20policy%20with%20resource%20count%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CPRE%3EAzureActivity%20%7C%20where%20Category%20%3D%3D%20'Policy'%20and%20Level%20!%3D%20'Informational'%20%7C%20extend%20p%3Dtodynamic(Properties)%20%7C%20extend%20policies%3Dtodynamic(tostring(p.policies))%20%7C%20mvexpand%20policy%20%3D%20policies%20%7C%20summarize%20resource_count%3Dcount()%20by%20tostring(policy.policyDefinitionName)%3C%2FPRE%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F120059i1EDA91879716C8E3%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%223.png%22%20title%3D%223.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%20start%3D%222%22%3E%0A%3CLI%3EGet%20a%20list%20of%20non-compliant%20resources%20from%20a%20single%20policy%20(using%20%E2%80%9Caudit-resources-without-tags-policyDef%E2%80%9D%20definition%20as%20an%20example)%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CPRE%3Elet%20policyDefId%20%3D%20'audit-resources-without-tags-policyDef'%3B%20AzureActivity%20%7C%20where%20Category%20%3D%3D%20'Policy'%20and%20Level%20!%3D%20'Informational'%20%7C%20extend%20p%3Dtodynamic(Properties)%20%7C%20extend%20policies%3Dtodynamic(tostring(p.policies))%20%7C%20mvexpand%20policy%20%3D%20policies%20%7C%20where%20policy.policyDefinitionName%20in%20(policyDefId)%20%7C%20distinct%20ResourceId%3C%2FPRE%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F120060i60646C1EED97513F%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%224.png%22%20title%3D%224.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3COL%20start%3D%223%22%3E%0A%3CLI%3EGet%20newly%20detected%20non-compliance%20resources%20from%20a%20single%20policy%20(using%20using%20%E2%80%9Caudit-resources-without-tags-policyDef%E2%80%9D%20definition%20as%20an%20example)%3A%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CPRE%3Elet%20policyDefId%20%3D%20'audit-resources-without-tags-policyDef'%3B%20AzureActivity%20%7C%20where%20Category%20%3D%3D%20'Policy'%20and%20Level%20!%3D%20'Informational'%20%7C%20extend%20p%3Dtodynamic(Properties)%20%7C%20extend%20policies%3Dtodynamic(tostring(p.policies))%20%7C%20mvexpand%20policy%20%3D%20policies%20%7C%20where%20policy.policyDefinitionName%20in%20(policyDefId)%20and%20p.isComplianceCheck%20%3D%3D%20'False'%3C%2FPRE%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F120061iBBBD1767ACB30459%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%225.png%22%20title%3D%225.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%20As%20previously%20mentioned%2C%20the%20Azure%20Policy%20engine%20runs%20periodic%20scans%20across%20all%20resources%20(compliance%20check).%20The%20full%20scan%20runs%20every%2024%20hours.%20In%20this%20example%2C%20we%20have%20added%20a%20condition%20in%20the%20end%20to%20only%20retrieve%20non-compliant%20resources%20that%20are%20not%20detected%20by%20the%20compliance%20check%20(%3CSTRONG%3E%3CEM%3Ep.isComplianceCheck%20%3D%3D%20%E2%80%98False%E2%80%9D%3C%2FEM%3E%3C%2FSTRONG%3E).%20Without%20this%20condition%2C%20you%20will%20get%20spammed%20with%20any%20new%20and%20existing%20non-compliant%20resources%20after%20every%20compliance%20check%20scan.%20This%20will%20most%20likely%20cause%20duplicate%20alerts%20being%20generated%20too.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBased%20on%20these%20sample%20queries%2C%20you%20can%20use%20the%20following%20queries%20to%20build%20your%20alert%20rules%20based%20on%20your%20scenarios%3A%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3ETo%20alert%20on%20any%20new%20non-compliant%20resources%3A%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CPRE%3EAzureActivity%20%7C%20where%20Category%20%3D%3D%20'Policy'%20and%20Level%20!%3D%20'Informational'%20%7C%20extend%20p%3Dtodynamic(Properties)%20%7C%20extend%20policies%3Dtodynamic(tostring(p.policies))%20%7C%20mvexpand%20policy%20%3D%20policies%20%7C%20where%20p.isComplianceCheck%20%3D%3D%20'False'%3C%2FPRE%3E%0A%3COL%20start%3D%222%22%3E%0A%3CLI%3ETo%20alert%20on%20new%20non-compliant%20resources%20detected%20by%20a%20particular%20policy%3A%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CPRE%3Elet%20policyDefId%20%3D%20'%26lt%3Benter-policy-definition-id-here%26gt%3B'%3B%20AzureActivity%20%7C%20where%20Category%20%3D%3D%20'Policy'%20and%20Level%20!%3D%20'Informational'%20%7C%20extend%20p%3Dtodynamic(Properties)%20%7C%20extend%20policies%3Dtodynamic(tostring(p.policies))%20%7C%20mvexpand%20policy%20%3D%20policies%20%7C%20where%20policy.policyDefinitionName%20in%20(policyDefId)%20and%20p.isComplianceCheck%20%3D%3D%20'False'%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20alert%20rule%20can%20be%20created%20from%20the%20Azure%20portal%20or%20by%20using%20ARM%20templates.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFollow%20the%20following%20steps%20to%20create%20the%20alert%20rule%20via%20the%20portal%3A%3C%2FP%3E%0A%3CP%3E1.%20Browse%20to%20Azure%20Monitor%20and%20click%20on%20%3CSTRONG%3E%3CEM%3EAlerts%3C%2FEM%3E%3C%2FSTRONG%3E%20blade%3C%2FP%3E%0A%3CP%3E2.%20Click%20on%20%3CSTRONG%3E%3CEM%3ENew%20alert%20rules%3C%2FEM%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E3.%20On%20the%20Create%20rule%20page%2C%20select%20the%20appropriate%20subscription%20and%20the%20Log%20Analytics%20workspace%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F120063i5C2FC9BA1B85C011%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%226.png%22%20title%3D%226.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E4.%20Add%20a%20condition%2C%20on%20the%20Configure%20signal%20logic%20page%2C%20select%20%3CSTRONG%20style%3D%22font-family%3A%20inherit%3B%22%3E%3CEM%3ECustom%20log%20search%3C%2FEM%3E%3C%2FSTRONG%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20style%3D%22width%3A%20766px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F120064i1718AFC05B069037%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%227.png%22%20title%3D%227.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E5.%20Enter%20the%20search%20query%2C%20and%20configure%20the%20alert%20logic%20as%20%3CSTRONG%3E%3CEM%3ENumber%20of%20results%20greater%20than%200%3C%2FEM%3E%3C%2FSTRONG%3E%2C%20specify%20the%20period%20and%20frequency%20(or%20leave%20it%20as%20default)%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20style%3D%22width%3A%20853px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F120065i17C5A8DF9338051E%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%228.png%22%20title%3D%228.png%22%20%2F%3E%3C%2FSPAN%3E6.%20Select%20an%20existing%20action%20group%2C%20or%20create%20a%20new%20one%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E7.%20Provide%20alert%20name%2C%20severity%20and%20other%20optional%20information%20such%20as%20email%20subject%2C%20and%20finish%20the%20wizard%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F120066i39E113DADE662079%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%229.png%22%20title%3D%229.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESince%20I%20have%20configured%20the%20action%20group%20to%20send%20me%20an%20email%2C%20when%20next%20time%20a%20non-compliant%20resource%20is%20created%2C%20I%20will%20get%20an%20email%20within%20few%20minutes%20(as%20shown%20below)%3A%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20style%3D%22width%3A%20808px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F120067i775C73D88FC62129%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%2210.png%22%20title%3D%2210.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%20%3C%2FSTRONG%3EAction%20groups%20in%20Azure%20Monitor%20supports%20other%20action%20types%20such%20as%20webhook%2C%20mobile%20push%20notifications%2C%20SMS%2C%20LogicApp%2C%20Azure%20Function%2C%20ITSM%2C%20etc.%20Although%20I%20have%20only%20used%20email%20in%20this%20example%2C%20you%20can%20customise%20the%20action%20group%20to%20suit%20your%20requirements%20(i.e.%20creating%20a%20ticket%20in%20ServiceNow%20via%20the%20ITSM%20connector.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EA%20sample%20template%20is%20available%20in%20my%20GitHub%20repo%20if%20there%20is%20a%20need%20to%20create%20the%20alert%20rule%20using%20ARM%20template%3A%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Ftyconsulting%2Fazurepolicy%2Ftree%2Fmaster%2Farm-templates%2Fmonitor-alert-rule-for-non-compliant-policy%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2Ftyconsulting%2Fazurepolicy%2Ftree%2Fmaster%2Farm-templates%2Fmonitor-alert-rule-for-non-compliant-policy%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20sample%20template%20is%20a%20subscription%20level%20ARM%20template%2C%20it%20creates%20a%20resource%20group%2C%20Azure%20monitor%20action%20group%20and%20alert%20rule%20within%20the%20resource%20group.%20Since%20it%20is%20a%20subscription%20level%20template%2C%20you%20will%20need%20to%20use%20%3CSTRONG%3E%3CEM%3Enew-azdeployment%3C%2FEM%3E%3C%2FSTRONG%3E%20cmdlet%20to%20deploy%20it.%20For%20example%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3Enew-azdeployment%20-Name%20sampleAlertRule%20-Location%20australiasoutheast%20-TemplateFile%20.%5Cazuredeploy.json%20-logAnalyticsWorkspaceResourceId%20%2Fsubscriptions%2F%26lt%3Bsub-id%26gt%3B%2Fresourcegroups%2F%26lt%3Blog-analytics-rg%26gt%3B%2Fproviders%2Fmicrosoft.operationalinsights%2Fworkspaces%2Fworkspace-name%3C%2FPRE%3E%0A%3CP%3E%3CSTRONG%3ENOTE%3A%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EThe%20%E2%80%9Cnew-azdeployment%E2%80%9D%20cmdlet%20is%20part%20of%20the%20Azure%20PowerShell%20module.%20To%20execute%20this%20command%2C%20you%20must%20firstly%20install%20the%20%E2%80%9CAz%E2%80%9D%20PowerShell%20module%20and%20login%20to%20your%20Azure%20subscription%20using%20the%20following%20commands%3A%3C%2FP%3E%0A%3CPRE%3E%23Install%20Az%20module%20from%20the%20PowerShell%20gallery.%20Run%20it%20as%20administrator%0AInstall-Module%20Az%20-Repository%20PSGallery%20-Force%20-AllowClobber%0A%0A%23Login%20to%20Azure%0AConnect-AzAccount%3C%2FPRE%3E%0A%3CP%3ETo%20make%20use%20of%20the%20sample%20ARM%20template%2C%20you%20also%20need%20to%20update%20the%20%3CEM%3EPolicyDefId%3C%2FEM%3E%20from%20line%2022%20of%20the%20template%20JSON%20file.%20To%20retrieve%20the%20full%20list%20of%20available%20Azure%20policy%20definitions%20in%20your%20environment%2C%20execute%20the%20command%20below%20once%20you%20have%20signed%20in%20to%20Azure%20using%20%3CSTRONG%3EConnect-AzAccount%3C%2FSTRONG%3E%20cmdlet%3A%3C%2FP%3E%0A%3CPRE%3Eget-azpolicydefinition%20%7Cformat-table%20ResourceName%2C%20%40%7BName%3D'DisplayName'%3B%20Expression%3D%7B%24_.Properties.DisplayName%7D%7D%2C%20ResourceId%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20policy%20definition%20Id%20is%20listed%20under%20the%20ResourceName%20column%20(as%20shown%20below)%3A%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F120068i06DD12024C9143AF%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%2211.png%22%20title%3D%2211.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20post%2C%20I%20have%20used%20a%20custom%20policy%20definition%20so%20the%20policy%20Id%20is%20more%20readable%20(the%20names%20of%20built-in%20policies%20are%20all%20GUIDs).%20The%20custom%20policy%20can%20be%20found%20at%20my%20GitHub%20repo%3A%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Ftyconsulting%2Fazurepolicy%2Ftree%2Fmaster%2Fpolicy-definitions%2Faudit-resources-without-tags%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2Ftyconsulting%2Fazurepolicy%2Ftree%2Fmaster%2Fpolicy-definitions%2Faudit-resources-without-tags%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20post%2C%20I%20have%20demonstrated%20how%20to%20use%20Kusto%20search%20queries%20in%20Azure%20Log%20Analytics%20to%20detect%20non-compliant%20resources%20detected%20by%20Azure%20Policy%2C%20and%20how%20to%20create%20alert%20rules%20in%20Azure%20Monitor%20when%20a%20new%20non-compliant%20resource%20is%20detected.%20If%20you%E2%80%99d%20like%20to%20learn%20more%20about%20Azure%20Policy%20and%20Azure%20Monitor%2C%20make%20sure%20you%20check%20out%20our%20free%20eBook%20%3CSTRONG%3E%3CEM%3EInside%20Azure%20Management%3C%2FEM%3E%3C%2FSTRONG%3E%20(%3CA%20href%3D%22https%3A%2F%2Fbit.ly%2FInsideAzureMgmt%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fbit.ly%2FInsideAzureMgmt%3C%2FA%3E)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELastly%2C%20I%E2%80%99d%20like%20to%20thank%20my%20friend%20and%20fellow%20MVP%20Stanislav%20Zhelyazkov%20(%3CA%20href%3D%22https%3A%2F%2Ftwitter.com%2FStanZhelyazkov%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%40StanZhelyazkov%3C%2FA%3E)%20for%20helping%20me%20out%20with%20fine-tuning%20the%20Kusto%20queries%20used%20in%20the%20alert%20rule.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETao%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-713466%22%20slang%3D%22en-US%22%3E%3CP%3EAzure%20Policy%20allows%20organization%20to%20define%20and%20enforce%20how%20Azure%20resources%20should%20or%20should%20not%20be%20used.%20It%20allows%20for%20the%20ability%20to%20block%20resources%20with%20certain%20configurations%20from%20being%20created%2C%20or%20generate%20audit%20events%20when%20a%20particular%20configuration%20is%20used.%20It%20can%20also%20modify%20resources%20at%20creation%20stage%20to%20ensure%20it%20meets%20standards%20set%20by%20your%20organization.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20blog%20post%20detail%20how%20to%20use%20Kusto%20query%20language%20to%20detect%20non-compliant%20resources%20by%20querying%20your%20Log%20Analytics%20workspace%2C%20and%20then%20how%20to%20create%20Azure%20monitor%20alert%20rules%20to%20take%20actions%20when%20non-compliant%20resources%20are%20detected%20by%20Azure%20Policy.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-713466%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E

 

Azure Policy allows organization to define and enforce how Azure resources should or should not be used. It allows for the ability to block resources with certain configurations from being created, or generate audit events when a particular configuration is used. It can also modify resources at creation stage to ensure it meets standards set by your organization.

 

Azure Policy can be used to:

  • Restrict users only use a list of Azure regions approved by your organization
  • Audit if a Public IP is being used
  • Enforce Hybrid Use Benefit is enabled on all Windows Server VMs
  • Automatically deploy the Log Analytics agent on any newly created VMs

 

The list can go on and on. Currently Azure provides over one hundred built-in policy definitions that can be utilized to consume free of charge, and also provides the ability to develop your own policy definitions if none of the built-in policy meets your requirements.

 

Azure policy engine evaluates policy assignments when ARM resources are created or updated within the assignment scope. It also runs delta and full scans on a schedule – to detect non-compliant resources.

 

A resource can be marked as non-compliant by audit policies, or detected by the periodic scans (for example, resources that were created before the policies were assigned).

 

In the Azure portal, you can view the list of non-compliant policies (as shown below):1.png

Azure Monitor can generate alerts and trigger pre-defined actions when non-complaint resources are detected.

 

This blog post detail how to use Kusto query language to detect non-compliant resources by querying your Log Analytics workspace, and then how to create Azure monitor alert rules to take actions when non-compliant resources are detected by Azure Policy.

 

The Azure policy compliance status is logged in the Azure subscription’s Activity logs. The Azure Log Analytics workspace can be configured to collect Azure Activity logs from any subscriptions in the same tenant. Azure Monitor alert rules can then be created to execute queries in the Log Analytics workspace on a schedule and generate alerts when non-compliant resources are detected by the query.

 

Before we start creating alert rules, we need to make sure Azure Activity logs are being collected by a Log Analytics workspace. To configure a Log Analytics workspace to collect Azure Activity logs, you need to navigate to the Azure Activity log blade of the workspace in the portal, and connect the subscriptions (as shown below):2.png

 

Once the Log Analytics workspace started collecting logs, the policy compliance status will be collected to your workspace.

 

The following Kusto queries can be used to check these logs in your Log Analytics workspace:

  1. Get a list of non-compliant policy with resource count
AzureActivity | where Category == 'Policy' and Level != 'Informational' | extend p=todynamic(Properties) | extend policies=todynamic(tostring(p.policies)) | mvexpand policy = policies | summarize resource_count=count() by tostring(policy.policyDefinitionName)

3.png

 

  1. Get a list of non-compliant resources from a single policy (using “audit-resources-without-tags-policyDef” definition as an example)
let policyDefId = 'audit-resources-without-tags-policyDef'; AzureActivity | where Category == 'Policy' and Level != 'Informational' | extend p=todynamic(Properties) | extend policies=todynamic(tostring(p.policies)) | mvexpand policy = policies | where policy.policyDefinitionName in (policyDefId) | distinct ResourceId

4.png

  1. Get newly detected non-compliance resources from a single policy (using using “audit-resources-without-tags-policyDef” definition as an example):
let policyDefId = 'audit-resources-without-tags-policyDef'; AzureActivity | where Category == 'Policy' and Level != 'Informational' | extend p=todynamic(Properties) | extend policies=todynamic(tostring(p.policies)) | mvexpand policy = policies | where policy.policyDefinitionName in (policyDefId) and p.isComplianceCheck == 'False'

5.png

 

NOTE: As previously mentioned, the Azure Policy engine runs periodic scans across all resources (compliance check). The full scan runs every 24 hours. In this example, we have added a condition in the end to only retrieve non-compliant resources that are not detected by the compliance check (p.isComplianceCheck == ‘False”). Without this condition, you will get spammed with any new and existing non-compliant resources after every compliance check scan. This will most likely cause duplicate alerts being generated too.

 

Based on these sample queries, you can use the following queries to build your alert rules based on your scenarios:

  1. To alert on any new non-compliant resources:
AzureActivity | where Category == 'Policy' and Level != 'Informational' | extend p=todynamic(Properties) | extend policies=todynamic(tostring(p.policies)) | mvexpand policy = policies | where p.isComplianceCheck == 'False'
  1. To alert on new non-compliant resources detected by a particular policy:
let policyDefId = '<enter-policy-definition-id-here>'; AzureActivity | where Category == 'Policy' and Level != 'Informational' | extend p=todynamic(Properties) | extend policies=todynamic(tostring(p.policies)) | mvexpand policy = policies | where policy.policyDefinitionName in (policyDefId) and p.isComplianceCheck == 'False'

 

The alert rule can be created from the Azure portal or by using ARM templates.

 

Follow the following steps to create the alert rule via the portal:

1. Browse to Azure Monitor and click on Alerts blade

2. Click on New alert rules

3. On the Create rule page, select the appropriate subscription and the Log Analytics workspace6.png

 

4. Add a condition, on the Configure signal logic page, select Custom log search7.png

 

5. Enter the search query, and configure the alert logic as Number of results greater than 0, specify the period and frequency (or leave it as default)8.png6. Select an existing action group, or create a new one

 

7. Provide alert name, severity and other optional information such as email subject, and finish the wizard9.png

 

Since I have configured the action group to send me an email, when next time a non-compliant resource is created, I will get an email within few minutes (as shown below):10.png

 

NOTE: Action groups in Azure Monitor supports other action types such as webhook, mobile push notifications, SMS, LogicApp, Azure Function, ITSM, etc. Although I have only used email in this example, you can customise the action group to suit your requirements (i.e. creating a ticket in ServiceNow via the ITSM connector.

 

A sample template is available in my GitHub repo if there is a need to create the alert rule using ARM template: https://github.com/tyconsulting/azurepolicy/tree/master/arm-templates/monitor-alert-rule-for-non-com...

 

This sample template is a subscription level ARM template, it creates a resource group, Azure monitor action group and alert rule within the resource group. Since it is a subscription level template, you will need to use new-azdeployment cmdlet to deploy it. For example:

 

new-azdeployment -Name sampleAlertRule -Location australiasoutheast -TemplateFile .\azuredeploy.json -logAnalyticsWorkspaceResourceId /subscriptions/<sub-id>/resourcegroups/<log-analytics-rg>/providers/microsoft.operationalinsights/workspaces/workspace-name

NOTE:

The “new-azdeployment” cmdlet is part of the Azure PowerShell module. To execute this command, you must firstly install the “Az” PowerShell module and login to your Azure subscription using the following commands:

#Install Az module from the PowerShell gallery. Run it as administrator
Install-Module Az -Repository PSGallery -Force -AllowClobber

#Login to Azure
Connect-AzAccount

To make use of the sample ARM template, you also need to update the PolicyDefId from line 22 of the template JSON file. To retrieve the full list of available Azure policy definitions in your environment, execute the command below once you have signed in to Azure using Connect-AzAccount cmdlet:

get-azpolicydefinition |format-table ResourceName, @{Name='DisplayName'; Expression={$_.Properties.DisplayName}}, ResourceId

 

The policy definition Id is listed under the ResourceName column (as shown below):11.png

 

In this post, I have used a custom policy definition so the policy Id is more readable (the names of built-in policies are all GUIDs). The custom policy can be found at my GitHub repo: https://github.com/tyconsulting/azurepolicy/tree/master/policy-definitions/audit-resources-without-t...

 

In this post, I have demonstrated how to use Kusto search queries in Azure Log Analytics to detect non-compliant resources detected by Azure Policy, and how to create alert rules in Azure Monitor when a new non-compliant resource is detected. If you’d like to learn more about Azure Policy and Azure Monitor, make sure you check out our free eBook Inside Azure Management (https://bit.ly/InsideAzureMgmt)

 

Lastly, I’d like to thank my friend and fellow MVP Stanislav Zhelyazkov (@StanZhelyazkov) for helping me out with fine-tuning the Kusto queries used in the alert rule.

 

Tao