In this post I go through the necessary stuff for you to understand what options you have pre deployment to check the quality and errors of your Bicep code, and how to actually deploy your Bicep with Azure CLI
Building and Validating Bicep Templates
Before deploying your Bicep files, always validate them to catch issues early. Here’s how to compile a Bicep file into ARM JSON:
az bicep build --file main.bicep
This generates a corresponding main.json file, allowing you to inspect the ARM template directly.
Validate your template against Azure resources without actually deploying it:
az deployment group validate \
--resource-group myResourceGroup \
--template-file main.bicep \
--parameters @params.json
Using “What-if” for Safe Previews
The Azure CLI’s what-if operation lets you preview changes before execution, helping you avoid unintended alterations to your environment:
az deployment group what-if \
--resource-group myResourceGroup \
--template-file main.bicep \
--parameters @params.json
Deploying Bicep Files with Azure CLI
Deployments are straightforward and robust with Azure CLI. A typical deployment looks like this:
az deployment group create \
--resource-group myResourceGroup \
--template-file main.bicep \
--parameters @params.json
For deployments targeting different scopes, use the respective commands:
- Subscription scope:
az deployment sub create - Management group scope:
az deployment mg create - Tenant scope:
az deployment tenant create
Advanced Bicep CLI Commands: Linting and Decompiling
Bicep CLI includes advanced features to keep your code clean and manageable:
- Linter: Analyze and enforce best practices.
az bicep lint --file main.bicep
- Decompile existing ARM templates: Convert ARM JSON templates back to Bicep for easier readability and updates.
az bicep decompile --file main.json
CLI Tips for Automation
Integrate Azure CLI and Bicep into CI/CD pipelines for automation:
- Use environment variables for parameterization.
- Combine
validate,what-if, and conditional logic in pipelines to safely automate deployments. - Leverage Azure DevOps and GitHub Actions for a seamless workflow.
Here’s a sample Azure CLI command to deploy within a pipeline:
- script: |
az deployment group create \
--resource-group $(resourceGroup) \
--template-file main.bicep \
--parameters @params.json
displayName: 'Deploy Bicep Template'
Best Practices
- Always preview with
what-ifbefore deployment. - Regularly validate templates using
bicep lintandaz deployment validate. - Maintain clarity in naming conventions for easy troubleshooting.