Local Deploy with Bicep
In the latest release of Bicep (0.36.1), Microsoft introduced a new feature called “Local Deploy”. This feature allows running Bicep deployments without talking to Azure. When you initially think about this, it seems odd, the whole point of Bicep is to deploy resource to Azure, why would you want to not talk to Azure? The reason for this is extensions.
Local Deploy is an Experimental feature, and several of the extensions we will look at are either in preview, or community contributions. Please bear this in mind when using these tools.
Extensions
Extensions allow for the extension of Bicep to add additional features so that it can be used to deploy things other than Azure resources. There are two Microsoft published extensions availible currently:
- Microsoft Graph for creation of Entra ID resources
- Kubernetes extension for creating Kubernetes resource, in preview
There are also several community created extensions that can be used:
- GitHub: Manage GitHub resources.
- Local: Run bash or powershell scripts locally.
- Http: Make HTTP requests.
- KeyVault data plane: Manage KeyVault data plane operations (secrets, certificates etc).
None of these extensions need to access the Azure API, and so Local Deploy allows for running them without connectivity to Azure, or for some such as the Local extension, without any connectivity at all.
Running Locally
Let’s create a a Bicep project that uses the Local extension to run a simple script.
BicepConfig
Local Deploy is an experimental feature, so we need to enable it in our BicepConfig file. If you don’t already have one created, you can can do so with the VS Code extension.
When the file is created, we need to add the expiremental section, and enable local deploy.
"experimentalFeaturesEnabled": {
"localDeploy": true
},
For extensions that are not published by Microsoft we also need to enable them in the config file. We’ll enable the Local extension. This is using the version publixhed by the author.
"extensions": {
"local": "br:bicepextdemo.azurecr.io/extensions/local:0.1.3"
},
Scripts
Next we’re going to create the scripts we want to run. For simple code you can just run it inline, but we can also put the code in an external file and run this. We’re going to create two scripts, PowerShell and Bash, which we’ll choose depending on the OS we are using.
script.ps1
Write-Host "Hello $INPUT_NAME!"
script.sh
#!/bin/bash
echo "Hello $INPUT_NAME!"
Bicep
Finally, we’re going to create the Bicep and BicepParams files.
In our Bicep file, we:
- Take parameters to choose whether to use Bash or PowerShell, and a Name parameter that we will pass into the script.
- We then turn on local-deploy mode, and we use the extension.
- We use the script resource, provided by the extension to run our script, importing the actual script content from the file.
- Finally, we send the output of the script to a Bicep output
targetScope = 'local'
extension local
param name string
param platform 'bash' | 'powershell'
resource sayHelloWithBash 'Script' = if (platform == 'bash') {
type: 'bash'
script: replace(loadTextContent('./script.sh'), '$INPUT_NAME', name)
}
resource sayHelloWithPowerShell 'Script' = if (platform == 'powershell') {
type: 'powershell'
script: replace(loadTextContent('./script.ps1'), '$INPUT_NAME', name)
}
output stdout string = (platform == 'bash') ? sayHelloWithBash.stdout : sayHelloWithPowerShell.stdout
In our BicepParams file, we pass in the required parameters.
using 'main.bicep'
param name = 'Sam'
param platform = 'bash'
Execute
Now everything is setup, we are ready to deploy. Instead of using the az deployment
commands we would usually use, we need to deploy using the bicep CLI directly.
bicep local-deploy main.bicepparam
Note that when I tested this, the local-deploy
command didn’t work when using the Azure CLI version of the Bicep CLI (az bicep
).
The Bicep Extension for VS Code also now has a deployment pane that can be used to run this. Select the .bicepparam file, and in the top right look for the deploy icon.
This gives us a graphical means of running the local deployment and viewing the results.
Summary
Local Deploy may be a bit of a niche feature, but as extensions become more widespread and using Bicep for deploying non-Azure resources it will become more useful. Not needing to have connectivity to the Azure API, and not being subject to its rate limites and pre-flight checks, when you don’t need them, will make execution quicker and less error prone.