Implementing Security Controls with Infrastructure as Code
This lesson explains how to deliver controls with Bicep, protect them with deployment stacks, and check templates before deployment.
Learning outcomes
After completing this lesson, you will be able to:
- Explain why code is a security control.
- Read a Bicep template that sets security properties.
- Explain how to keep secrets out of templates.
- Describe deployment stacks and deny settings.
Why code is a security control
A setting that someone clicks in the portal has no review, no history and no guarantee that the next environment will match. Infrastructure as code (IaC) changes this. A template is stored in source control, a colleague reviews a pull request, a pipeline deploys it the same way each time, and any change leaves a record. A secure baseline can then be a shared module, so that every team receives the same settings by default.
Microsoft supports ARM templates in JSON, and Bicep, a more readable language that compiles to them. Terraform is another common option. The principles are the same in each case: state what should exist, and let the tool reach that state.
Deploying security resources
A template can deploy security controls as well as workloads. The following Bicep file creates a storage account with hardened settings, assigns a role to a managed identity, and adds a lock. Every value that matters is visible to a reviewer.
param location string = resourceGroup().location
param identityPrincipalId string
resource sa 'Microsoft.Storage/storageAccounts@2023-05-01' = {
name: 'st${uniqueString(resourceGroup().id)}'
location: location
sku: { name: 'Standard_ZRS' }
kind: 'StorageV2'
properties: {
allowBlobPublicAccess: false
allowSharedKeyAccess: false
minimumTlsVersion: 'TLS1_2'
supportsHttpsTrafficOnly: true
publicNetworkAccess: 'Disabled'
}
}
var blobReader = '2a2b9908-6ea1-4ae2-8e65-a410df84e7d1' // Storage Blob Data Reader
resource reader 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
scope: sa
name: guid(sa.id, identityPrincipalId, blobReader)
properties: {
principalId: identityPrincipalId
principalType: 'ServicePrincipal'
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', blobReader)
}
}
resource noDelete 'Microsoft.Authorization/locks@2020-05-01' = {
scope: sa
name: 'no-delete'
properties: { level: 'CanNotDelete' }
}Policy definitions, initiatives and assignments can also be deployed from Bicep, at subscription or management group scope, so that governance itself is managed as code.
Handling secrets in templates
A template must never hold a secret, because it sits in source control and because deployment history keeps parameter values. In Bicep, mark a parameter with the @secure() decorator so that its value is not logged, and use a Key Vault reference in a parameter file so that the deployment retrieves the value at run time. That needs the vault's setting for Azure Resource Manager template deployment and the Key Vault permission for the deployer. Better still, use managed identities so that no secret is passed at all.
Secrets and templates
Deployment stacks and deny settings
Templates set a state at the moment of deployment, and they do not stop someone from changing it later in the portal. A deployment stack is a Resource Manager resource that manages a group of resources as a unit. It offers two relevant features.
- Deny settings protect managed resources from change.
denyDeleteblocks deletion, anddenyWriteAndDeletealso blocks changes, unless a principal is on the exclusion list. - Action on unmanage sets what happens when a resource leaves the template: detach it, which is the default, or delete it.
Deny settings are implemented with deny assignments, so they work where a role does not, and they bind even an Owner, unlike a lock that an Owner could simply remove. Azure Blueprints, which once filled this need, is retiring on 31 January 2027, and deployment stacks are its successor. The following command creates a stack at resource group scope.
az stack group create --name secure-baseline --resource-group RG --template-file main.bicep \
--deny-settings-mode denyWriteAndDelete --action-on-unmanage detachAllDeployment stacks
Checking templates before deployment
The earlier a problem is found, the cheaper it is to fix. Several checks can run in a pipeline before anything reaches Azure.
| Check | What it finds | When it runs |
|---|---|---|
| Bicep linter | Style and best-practice problems, such as hard-coded secrets | While authoring and at build |
| What-if operation | The changes that a deployment would make | Before deployment |
| Defender for Cloud DevOps security | Misconfigurations in templates and secrets in code | In the pipeline or repository |
| Azure Policy | Rule violations, enforced at deployment | During the Resource Manager request |
Run az deployment group what-if before each production change. Treat policy as the final safety net rather than the first check, since a failed deployment at the end of a pipeline wastes more time than a failed linter at the beginning.
Checks before deployment
Stopping configuration drift
A central team deploys a baseline to each subscription, including Log Analytics settings and NSGs. Subscription Owners sometimes change these resources in the portal, which causes drift. The central team must block these changes while still being able to update the baseline from its pipeline. What should you use?
Deploy the baseline as a deployment stack with deny settings of denyWriteAndDelete, and list the pipeline's service principal in the excluded principals. The deny assignment binds Owners, and the pipeline can still update the stack.
A ReadOnly lock would block drift, but an Owner can remove a lock. Azure Policy can audit or deny specific properties, but it would need a separate rule for every setting. Azure Blueprints is retiring, so it is not a suitable new design.
Why can a deployment stack stop an Owner when a lock cannot?
Show answer
Deny settings use deny assignments, which an Owner cannot override, whereas an Owner can simply remove a lock.
Exam Tips
Remember for the exam
- Keep secrets out of templates; use Key Vault references or managed identities.
- Deployment stacks with deny settings bind even Owners.
- Use what-if before a production deployment.
- Blueprints retires on 31 January 2027, and deployment stacks replace it.