Azure Spring Clean: Azure best practice for Bicep with PSRule

Photo of author

Dan Rios

6 min read

This is my first time getting involved with a great initiative called Azure Spring Clean by Thomas Thornton & Joe Carlyle – which I’m excited to be apart of!

For those unaware, the event promotes and encourages well managed Azure tenants. This is done yearly in March where across 5 days there are 5 different base topics, of which different content is published. You can read and find out more here.

In this post I’ll detail how an awesome tool called PSRule, created by Microsoft’s Bernie White can be used to guard against Azure Bicep misconfigurations, Azure best practice and align to the pillars of the Microsoft Well-Architected & Cloud Adoption Frameworks.

What’s PSRule?

PSRule for Azure is an open source project that features a curated list of pre-built tests with complimentary documentation that help us configure Azure resources.

The tests allow us to scan and check our Azure Bicep template before we deploy to ensure they are following Azure best practices, including the Cloud Adoption Framework (CAF) and Well-Architected Framework (WAF) alignment. In addition,

PSRule for Azure features hundreds of rules for various resources and their configurations which can be found here. In addition, direct integration to your DevOps processes using GitHub or Azure DevOps with minimal configuration.

PSRule offers a ton of flexibility with the base YAML configuration file so we are able to ignore specific rules, files, folders and much more.

Getting started – Azure best practice with Bicep

Local scan

We can install PSRule locally via the PowerShell gallery, alternatively if you use VSCode there is an extension which can be found here which can be used via this guide.

A local scan can be a great way to check your templates without waiting for continuous integration pipelines to tell you what’s wrong, therefore shifting left the check for quicker identification and remediation. Alternatively if your team is small and still trying to adopt CI/CD and DevOps practices then this is a good mode 1 to grow from.


Install-Module -Name 'PSRule' -Repository PSGallery -Scope CurrentUser

Create a file ‘ps-rule.yaml‘ and place this example configuration within your root directory, or repository.

Note the modules in the YAML file. We’re able to pull in the Cloud Adoption Framework ruleset to the scans as well as Azure best practices and Kubernetes best practices.
  # Enable automatic expansion of bicep source files.
  # Enable automatic expansion of param files
  # Set timeout for expanding Bicep source files.

  # Ignore warnings for resources and objects that don't have any rules.
  unprocessedObject: Ignore
  ruleSuppressed: Ignore

  # Ignore common files that don't need analysis.
  - "**/bicepconfig.json"
  - "*.md"
  - "*.ps1"

  - PSRule.Rules.Azure
  - PSRule.Rules.CAF
  - PSRule.Rules.Kubernetes

  # Ignore the following rules for all objects
  - Azure.Resource.UseTags
  - Azure.ACR.MinSku

  culture: ["en-GB"]

Running the scan

We have a two main options to run the scan, with a detailed output or a summary. We can specify the input path to our bicep file we want to scan.

Invoke method: 

Invoke-PSRule -InputPath .\main.bicep -As Detail
Invoke-PSRule -InputPath .\main.bicep -As Summary

or Assert method:

Assert-PSRule -InputPath .\main.bicep -As Detail
Assert-PSRule -InputPath .\main.bicep -As Summary

More info on these two methods and their inputs here, they are similar features however.

Output example

 ~  Assert-PSRule -InputPath .\main.bicep -As Detail
    ____  _____ ____        __
   / __ \/ ___// __ \__  __/ /__
  / /_/ /\__ \/ /_/ / / / / / _ \
 / ____/___/ / _, _/ /_/ / /  __/
/_/    /____/_/ |_|\__,_/_/\___/

Using PSRule v2.9.0
Using PSRule.Rules.Azure v1.32.1

Explore documentation:
Contribute and find source:
Report issues:
-> appinsights-name : Microsoft.Insights/components [3/3]

    [PASS] Azure.Resource.AllowedRegions (AZR-000167)
    [PASS] Azure.AppInsights.Workspace (AZR-000069)
    [PASS] Azure.AppInsights.Name (AZR-000070)

 -> 864e505983a1f-webVnet : Microsoft.Resources/deployments [7/7]

    [PASS] Azure.Deployment.OutputSecretValue (AZR-000279)
    [PASS] Azure.Deployment.AdminUsername (AZR-000284)
    [PASS] Azure.Deployment.SecureParameter (AZR-000408)
    [PASS] Azure.Deployment.SecureValue (AZR-000316)
    [PASS] Azure.Template.ParameterStrongType (AZR-000227)
    [PASS] Azure.Template.ExpressionLength (AZR-000228)
    [PASS] Azure.Deployment.Name (AZR-000359)

 -> webapp-vnet : Microsoft.Network/virtualNetworks [4/5]

    [PASS] Azure.Resource.AllowedRegions (AZR-000167)
    [FAIL] Azure.VNET.UseNSGs (AZR-000263)
    | Template: main.bicep:522:13

    | Consider assigning a network security group (NSG) to each virtual network
    | subnet.

    | REASON:
    | - The subnet (webapp-snet) has no NSG associated.

    | HELP:
    | -

In the output, we can see the PSRule scan has highlighted that the virtual network has no network security group associated to it. It then provides a reason and a help link directly to the PSRule rule.

The awesome part about the scan output here is that not only do we get a nice report on an Azure recommendation, but the documentation links directly to how we can remediate this particular suggestion – if we wish.

The help URL provides an Azure Bicep snippet that will enable this rule to pass on the next run, giving us a really simple way to retrospectively go back and amend our Azure Bicep template to correct any security, compliance or best practice configurations that were missed.

On the help documentation it’ll show us the Well-Architected Framework pillar alignment that this particular rule is referencing:

PSRule security pillar rule

You can view all rules by pillar from here.

Pipeline integrations

Similarly, it’s super simple to integrate to your Azure DevOps pipeline or GitHub Actions. Making sure we have the ps-rule.yaml file in the repository root.

GitHub Action

If you’re unfamiliar with how to setup a GitHub Action, all you’ll need to do is create a file within .github/workflows like psrule-pipeline.yaml and paste the below YAML, saving this.

# Analyze repository with PSRule for Azure

# For PSRule for Azure documentation see:

# For action details see:

name: PSRule analysis

# Run for main or PRs against main
    - main
    - main

    name: Analyze repository
    runs-on: ubuntu-latest

    - name: Checkout
      uses: actions/[email protected]

    - name: Run PSRule analysis

      uses: Microsoft/[email protected]
        modules: 'PSRule.Rules.Azure, PSRule.Rules.CAF, PSRule.Rules.Kubernetes'

Azure DevOps Pipeline

To set this up, create a file within your root Azure DevOps GIT repository like psrule-pipeline.yaml and paste the code in, committing to the repository.

Once committed, you can setup a build policy so all code being merged into the main branch can get scanned. I have blogged a guide on how to this: The ultimate Bicep validation pipeline guide: ADO Edition – Rios Engineer which will go into much more detail than is in scope for this article.

trigger: none
name: $(SourceBranchName)_$(date:yyyyMMdd)$(rev:.r)
  vmImage: "ubuntu-latest"

- job: 'PSRuleAnalysis'
  displayName: "Run PSRule analysis"
  - task:
      source: "$(System.DefaultWorkingDirectory)/ps-rule.yaml"
      modules: PSRule.Rules.Azure, PSRule.Rules.CAF, PSRule.Rules.Kubernetes
      outputFormat: Sarif
      outputPath: "reports/ps-rule-results.sarif"

Output example


Summarised – Main benefits of using PSRule with Azure Bicep

✅ Framework alignments for both WAF & CAF

✅ Azure best practices rules

✅ Helps guard against misconfigurations or user error when creating Bicep templates (e.g. forgot to associate NSG to a subnet)

✅ Enables consistent configuration for your Bicep deployments

✅ Can be used for Azure Policy as well

Quick start lab – GitHub Repository Example

I’ve put together an extensive GitHub repository that details a full repository structure, with examples and YAML files for both Azure DevOps and GitHub for you to take and lab or use as a base to start:

Bicepify/bicep-cicd-examples/module-tests-with-psrule at main · riosengineer/Bicepify (

I’ve also previously written other blogs that cover some more in-depth guides on these subjects which may be of interest:

Bicep modules with PSRule – Testing, documentation, CI Pipeline & examples – Rios Engineer

The ultimate Bicep validation pipeline guide: ADO Edition – Rios Engineer

Leave a comment

Skip to content