DeployIfNotExists takes its template from a JSON file or a hashtable, with checked parameters¶
Context and Problem Statement¶
deployIfNotExists needs details.deployment: "the full template deployment as it would be passed to the Microsoft.Resources/deployments PUT API". That is an ARM template, the values passed to its parameters, and for subscription-level deployments a location. Templates are often hundreds of lines. In the Azure Landing Zones Library every deployment uses incremental mode. Template parameters take their values from [parameters(...)] 245 times, from [field(...)] 138 times, and are literal only twice. How does an author supply the template and its parameters?
Decision Drivers¶
- Templates are long and are often written and tested on their own, so they should be able to live in their own files.
- The values passed to template parameters are where policy and template meet, and mistakes there only show at remediation time.
- Deterministic output (ADR-0009).
Considered Options¶
- A
Deploymentkeyword inside theDeployIfNotExistsblock, taking a JSON file or a hashtable - Template options on
DeployIfNotExistsitself - Compiling Bicep with the Bicep CLI
Decision Outcome¶
Chosen option: "A Deployment keyword inside the DeployIfNotExists block, taking a JSON file or a hashtable".
DeployIfNotExists 'Microsoft.Sql/servers/databases/transparentDataEncryption' -Name current -Role 'SQL DB Contributor' {
Require 'Transparent data encryption is enabled' {
Field status | Must -Be 'Enabled'
}
Deployment -TemplateFile ./templates/sql-tde.json -Parameters @{
fullDbName = Arm "[field('fullName')]"
}
}
Deploymentis required inDeployIfNotExists, at most once, and not allowed anywhere else.-TemplateFileis a JSON file, relative to the.bylaw.ps1file's folder.-Templateis a hashtable (ordered, to keep its key order). Exactly one of the two is required. The template is emitted through the canonical writer (ADR-0009): key order as in the file or hashtable, Bylaw's formatting.-Parametersmaps template parameter names to values. Each value is emitted as{ "value": ... }. A value can be a literal,Param,ArmorResourceGroupTag. Policy parameters used here count as used (ADR-0021).- The deployment mode is always
incremental, the only mode Learn's examples and ALZ use. It isn't configurable. -Scope ResourceGrouporSubscriptionemitsdetails.deploymentScope, omitted when not set (Azure's default isResourceGroup).-Locationemitsdeployment.location. Learn: "A location property must be specified in the Deployment when using subscription level deployments", so-Scope Subscriptionwithout-Locationis a compile error.- A
.bicepfile is a compile error saying Bicep isn't supported yet. Compiling Bicep needs the Bicep CLI, and the CLI's output changes between versions, which would break deterministic output.
Checks¶
- The template file exists and is valid JSON, and the template is an object with a
resourcesarray. - Every name in
-Parametersis a parameter of the template. - Every template parameter without a
defaultValueis given in-Parameters. - Expressions inside the template aren't checked. Learn: "All functions inside the
deploymentproperty are evaluated as components of the template, not the policy", except the parameter values. - Template parameter names are matched ignoring case, as ARM does.
Details¶
-Parameterskeeps the order of an[ordered]hashtable. A plain hashtable is sorted, as the JSON writer sorts one (ADR-0009), because its own order changes from one process to the next.-Templatecan't containParamorArm: it is an ARM template, evaluated by the deployment. Policy values go in through-Parameters.- The emitted
detailsfollow the AuditIfNotExists details (ADR-0030) withroleDefinitionIds,deploymentScopeanddeploymentafter them:deploymentis{ location?, properties: { mode, template, parameters? } }. - A template file is an input of the build.
Build-Bylawnever reports it as stale output (BYL5030), even when it is inside-OutputPath.
Diagnostics¶
- Front end: BYL1032 (no
-Role), BYL1043 (no block, or noDeploymentin it), BYL1044 (neither or both of-TemplateFileand-Template), BYL1045 (a template file that is missing, isn't JSON or is Bicep; a template without aresourcesarray;ParamorArmin-Template), BYL1040 (an invalid-Scope), BYL1039 (an invalid-Location), BYL1046 (-Scope Subscriptionwithout-Location).DeploymentoutsideDeployIfNotExistsis BYL1002, and a second one BYL1004. - Validation: BYL3093 (a value passed to a parameter the template doesn't have), BYL3094 (a required template parameter that isn't passed), BYL3060 and BYL3061 for roles (ADR-0022), BYL3020 for an undeclared policy parameter.
Consequences¶
- Good, because templates can be authored and linted as ordinary ARM templates, and the policy only says what it passes to them.
- Good, because a misspelled or missing template parameter fails the build instead of the remediation task.
- Bad, because a template file is a second input to a policy. Editing it changes the policy's output, which is what an author would expect.
Confirmation¶
Unit tests cover the file and hashtable forms, each check, -Scope with and without -Location, and parameter values of each kind. A golden test uses a template file.
Pros and Cons of the Options¶
A Deployment keyword¶
- Good, because the deployment is visually separate from the existence condition, and its options are grouped.
- Bad, because there is one more keyword.
Options on DeployIfNotExists¶
- Good, because there is no extra keyword.
- Bad, because the keyword would have a dozen parameters mixing the related resource, the roles and the deployment.
Bicep¶
- Good, because many authors write Bicep rather than JSON.
- Bad, because it needs the Bicep CLI at build time, and its output isn't stable across versions. It can be added later as another
-TemplateFiletype.
More Information¶
- DeployIfNotExists effect
- Related: ADR-0009, ADR-0021, ADR-0030