Logo

dev-resources.site

for different kinds of informations.

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

Published at
10/22/2023
Categories
azuredevops
azurebicep
psrule
azure
Author
riosengineer
Author
12 person written this
riosengineer
open
Bicep modules with PSRule – Testing, documentation, CI Pipeline & examples

Testing your Bicep modules with PSRule is a great way to identify that your code is meeting Azure best practice.

But how do you actually test your Bicep modules with PSRule properly?

How can you structure your Git repository?

How can I configure PSRule to test the modules? Can I document what the modules do, and their params?

I’ll go into depth on this below ! πŸ˜„

All of the content shared here is uploaded to my GitHub repository which can be found here https://github.com/riosengineer/bicep-module-tests/tree/main.

The problem

PSRule cannot run reliably against your actual Bicep templates. Errors like "The parameter named 'someParam' was not set or a defaultValue was defined." may be familiar if you’ve stumbled across this post.

Why? PSRule needs actual parameter values in order to expand and run the ruleset to determine what could be deployed. The PSRule creator Bernie White explains this well in this GitHub issue: https://github.com/microsoft/PSRule/discussions/1256

With this in mind, it’s best to try and not run PSRule directly against your Bicep templates to get a more reliable CI pipeline experience.

Repository structure

Reference Docs

.
└── bicep/
    β”œβ”€β”€ modules/
    β”‚ β”œβ”€β”€ storageAccount/
    β”‚ β”‚ β”œβ”€β”€ storageAccount.bicep
    β”‚ β”‚ β”œβ”€β”€ metadata.json
    β”‚ β”‚ └── README.md
    β”‚ └── .tests/
    β”‚ └── storageAccount.tests.bicep
    └── prod/
        β”œβ”€β”€ main.bicep
        └── prd.bicepparam
Enter fullscreen mode Exit fullscreen mode

By structuring the repository like the above example we’re going to be able to target PSRule to run against the β€˜.tests’ files to get a consistent scan output on our Azure best practices.

This will mean we can get PSRule to scan the module and tell us if there’s any best practice gaps we may want to consider adding to the module.

You can see we have a sub-modules folder for in-line Bicep modules. We also have a separate folder for our actual deployments which reference our modules as seen in the example above. This means we can shift a lot of the scanning onto the tests file and off the templates that may be problematic, this is because the modules will be called by the main templates for deployments.

Tests file

The test file will comprise of only the required parameters with actual values populated. As mentioned earlier, this will ensure PSRule can fully scan the code and analyse it for us.

The test file needs to be within it’s own sub-folder of the module like the above structure hierarchy.

Here’s an example test Bicep module file:

// Test with only required parameters
module test_required_params '../storageAccount.bicep' = {
  name: 'test_required_params'
  params: {
    location: 'uksouth'
    tags: {
      Demo: 'Rios Engineer Blog'
    }
    storageName: 'riosengineerst001'
    storageSkuName: 'Standard_GRS'
    storagePleBlobName: 'someBlob'
    storagePleFileName: 'someFileshare'
    subnetId: 'test'
    virtualNetworkId: 'test'
  }
}
Enter fullscreen mode Exit fullscreen mode

BICEP

Bicep Module(s)

The Bicep modules are located within an inline β€˜modules’ folder, separating out each module like so:

└── bicep/
    └── modules/
        β”œβ”€β”€ storageAccount/
        β”œβ”€β”€ storage.bicep
        └── metadata.json

Enter fullscreen mode Exit fullscreen mode

Metadata

A great way to display the parameters of the module, along with all relevant metadata & descriptions is to have a readme present in the module folder. This helps other people understand the module, and it’s parameters. This metadata json file sits within the modules/storageAccount folder (as seen above in the structure).

Here’s an example of what the metadata.json file looks like:

    {
        "itemDisplayName": "Storage Account Module.",
        "description": "Storage Account Bicep Module",
        "summary": "Storage Account Bicep standard for deployments"
    }
Enter fullscreen mode Exit fullscreen mode

JSON

With this JSON file we can run an absolutely AWESOME readme script created by Microsoft MVP Tao Yang which auto-generates a markdown file with all the information. Tao’s blog on how this works is here: Generating README for Bicep Files – Managing Cloud and Datacenter by Tao Yang (tyang.org)

I would highly recommend you head over there and check it out.

Here’s a sample output from my example module:

Bicep readme markdown

Azure DevOps Pipeline

Next is the ADO pipeline, you’ll need to install the PSRule extension into your Organisation if you haven’t got it already for this to work. Additionally, you’ll likely need a build validation policy so this will trigger on PR automatically. I’ve written a guide about that before here.

In my example Repository in GitHub you can find the structure on all of this so far, and also the pipeline file like so:

bicep-module-tests/bicep-ci.yaml at main Β· riosengineer/bicep-module-tests (github.com)

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

jobs:
- job: 'PSRuleAnalysis'
  displayName: "Run PSRule analysis"
  steps:
  - task: bewhite.ps-rule.assert.ps-rule-assert@2
    inputs:
      source: "$(System.DefaultWorkingDirectory)/ps-rule.yaml"
      modules: PSRule.Rules.Azure, PSRule.Rules.CAF, PSRule.Rules.Kubernetes
      outputFormat: Sarif
      outputPath: "reports/ps-rule-results.sarif"
Enter fullscreen mode Exit fullscreen mode

YAML

This file needs to be part of your build validation so you can run PSRule when changes are made to existing modules, or new ones added to the repository.

PSRule module scan failure

Amending my tests file to have storageSkuName: 'Standard_LRS'we can see the pipeline gives us a best practice recommendation from PSRule:

Bicep module test Azure DevOps failure

PSRule module scan success

However, when changing the value to storageSkuName: 'Standard_GRS' we can see the pipeline is happy with the Bicep module:

Bicep module Azure DevOps pipeline

Helpful resources

Using Bicep source – PSRule for Azure

Testing Bicep Code Using PSRule in Azure Pipeline – Managing Cloud and Datacenter by Tao Yang (tyang.org)

Azure/PSRule.Rules.Azure-quickstart: Sample code you can use to quickly start using PSRule for Azure. (github.com)

The post Bicep modules with PSRule – Testing, documentation, CI Pipeline & examples appeared first on Rios Engineer.

azuredevops Article's
30 articles in total
Favicon
Creating SBOM with sbom-tool and CycloneDX on Azure DevOps
Favicon
Public IP Address in Azure | Understanding Public IP Address in Azure VM
Favicon
Terraform - Mastering Idempotency Violations - Handling Resource Conflicts and Failures in Azure
Favicon
Azure Devops Converting Classic Pipelines to Yaml Based Pipeline
Favicon
Register Azure DevOps Agents with Service Principal Secret !
Favicon
The Ultimate Guide to Azure DevOps: Key Benefits for Software Development Projects
Favicon
Azure DevOps Zero to Hero Series
Favicon
Azure DevOps Series - Azure Boards
Favicon
cp: cannot stat 'bhfonlineshop': No such file or directory. Azure devops pipeline error
Favicon
How to Maintain Issue Hierarchy Between Jira and Azure DevOps
Favicon
Azure DevOps | Using Terraform
Favicon
Azure DevOps | Installing Postman and Newman using npm
Favicon
Azure DevOps | Running JMeter Test Collection using JMeter Docker Image
Favicon
Azure DevOps | Running a Postman Collection using Newman Docker Image
Favicon
Azure DevOps | Deploy Postman Tests in Azure DevOps Test Plans
Favicon
Exploring Microsoft Azure AI Capabilities Using React, Github Actions, Azure Static Apps and Azure AI
Favicon
Azure DevOps Zero-to-Hero
Favicon
Azure pipelines - Passing variables across stages
Favicon
Terraform - Keep dependencies up to date with Dependabot (Azure DevOps version)
Favicon
Automate Azure VM Password Rotation with PowerShell and Azure DevOps
Favicon
Investigating az-cli performance on the hosted Azure Pipelines and GitHub Runners
Favicon
Azure DevOps Pipeline deployments to Azure App Services with Access Restrictions
Favicon
Step by step for build , test and deploy using azuredevops pipeline
Favicon
Step by Step instruction hosting on aks cluster for begineers
Favicon
Deploying to Azure from Azure DevOps without secrets
Favicon
VSBuild task fails on self-hosted Azure Pipelines Agent
Favicon
Azure devops pipeline for dotnet with cicd
Favicon
Bicep modules with PSRule – Testing, documentation, CI Pipeline & examples
Favicon
Conectando o VS Community ao Azure DevOps
Favicon
Como clonar um projeto no Azure DevOps

Featured ones: