Validate Azure Resource Relationships with PSRule and PowerShell Graphs
PSRule for Azure makes it straightforward to validate the properties of individual resources before deployment. But some architecture requirements describe a relationship, not a property: every Function App must connect to the expected virtual network, or every application must have exactly one private endpoint.
This article shows how to collect those relationships in a PowerShell graph while PSRule processes a Bicep deployment, then validate the completed graph at the end of the pipeline.
Why per-resource rules are not enough
Consider an architecture with a Function App, a virtual network, an integration subnet, and a private endpoint subnet. We can easily write rules that check:
- whether the Function App has public access disabled;
- whether the virtual network uses the correct address space;
- whether the expected subnets exist.
Those checks still do not prove that the Function App is connected to the correct subnet or that its private endpoint belongs to the expected virtual network. The required information is spread across several expanded ARM resources.
A graph is a natural representation of this problem:
- resources and subnets become vertices;
- references between them become edges;
- an architecture requirement becomes a path or edge-count assertion.
Prepare PSRule and the graph module
The example uses PSRule.Rules.Azure to expand and analyze Bicep and PSQuickGraph to build the dependency graph.
| |
In ps-rule.yaml, enable Bicep expansion and include the convention that will collect
relationships:
| |
Collect relationships with a convention
A PSRule
convention
can run custom PowerShell at different stages of the pipeline. Its Process block
runs once for each input object, while its End block runs after all objects have
been processed.
That lifecycle gives us a convenient two-phase approach:
- Add every relevant resource and relationship to a graph.
- Validate the graph only after the complete deployment has been seen.
The following is a simplified version of the convention. The example uses child-to-parent edges so a valid dependency chain ends at the virtual network.
| |
The sample uses global variables because the state must remain available across PSRule callback invocations. In a larger rule set, wrap this state in a single object and reset it before each run.
Validate complete dependency paths
Once PSRule reaches the End block, every relevant Bicep resource has been expanded
and processed. We can now ask questions about the deployment as a whole.
For example, the following check confirms that every Function App has a VNet integration path:
| |
We can apply the same idea to private endpoint connectivity:
| |
Throwing from the End block causes the validation run to fail. This works well in
CI, although it does not produce the same detailed assertion output as a normal
PSRule Rule block.
Run the rules against the Bicep entry point with:
| |
Export the graph for troubleshooting
The same model used for validation can produce a diagram. This is particularly
helpful when a CI check reports a missing path and you need to see which relationship
was absent. PSQuickGraph can export the graph in Graphviz DOT format; the
Graphviz dot executable can then render it as SVG.
| |
This separates the solution into three clear steps:
- PSRule expands Bicep into resource objects.
- A convention converts cross-resource references into graph edges.
- Graph queries validate the architecture after all resources are available.
The result complements normal per-resource rules instead of replacing them. Use regular PSRule assertions for local properties and graph assertions for requirements that span the deployment.
A complete Bicep project, PSRule configuration, architecture document, and runnable Codespaces environment are available in the psrule-demo repository.
Related Articles
PowerShell + DevOps Global Summit 2025: Call for Papers Now Open!
PowerShell + DevOps Global Summit 2025: Call for Papers Now Open! Calling all innovators, problem-solvers, and thought …
Read moreOnRamp2024 Program Unveiled
Navigating the Path to Proficiency: PowerShell + DevOps OnRamp2024 Program Unveiled Introduction The PowerShell + DevOps …
Read morePowerShell Escape Room
PowerShell Escape Room by Michiel Hamers by Michiel Hamers https://about.me/michielhamers/ Why on earth you want to create an …
Read more