Chapter 2: Installing Azure Bicep

In this chapter, you are going to learn how to install Azure Bicep. Cross-platform installation tips using the Azure CLI or PowerShell Core and installation on each of the major operating systems are going to be covered.

At the end, you will learn how to install nightly builds if you are up for it and want to stay ahead of the curve.

In this chapter, we are going to cover the following main topics, but you can skip to the relevant section based on your platform of choice right away:

  • Cross-platform installation
  • Installation on Windows
  • Manual installation on macOS
  • Manual installation on Linux
  • Getting nightly builds
  • Working with Bicep in a Docker container

Technical requirements

To take full advantage of this chapter, you will need to have access to a personal computer or laptop that has Visual Studio Code and PowerShell Core installed. The code used throughout this chapter is stored in this GitHub repository at https://github.com/PacktPublishing/Infrastructure-as-Code-with-Azure-Bicep/tree/main/Chapter02.

Cross-platform installation

In this section, you will learn how to leverage the Azure CLI and Azure PowerShell to install Bicep cross-platform. This will include installing the Azure CLI and Azure PowerShell on Windows, macOS, and Linux.

Installing the Azure CLI

This approach is by far the easiest and the most convenient of all the possible ways to install Bicep. You will need to install the latest version of the Azure CLI or at least version 2.20.0 or above.

Installing the Azure CLI on Windows

To install the Azure CLI on Windows, you can either download the installer from https://aka.ms/installazurecliwindows or use the following command in a PowerShell window (PowerShell must be run as an administrator):

Invoke-WebRequest -Uri https://aka.ms/installazurecliwindows -OutFile .AzureCLI.msi; Start-Process msiexec.exe -Wait -ArgumentList '/I AzureCLI.msi /quiet'; rm .AzureCLI.msi

The following is the Azure CLI installation wizard window:

Figure 2.1 – Azure CLI installation wizard

Figure 2.1 – Azure CLI installation wizard

If you already have it installed with a version lower than 2.20.0, you can run the upgrade command to get the latest version:

az upgrade

You can find out what version of the Azure CLI you have installed using the az –version command.

Note

The az upgrade command was added in version 2.11.0, so if your current version is below that, you need to uninstall it and install the latest version.

Installing the Azure CLI on Linux

To install the Azure CLI on Linux, you will need to refer to the official documentation site at https://docs.microsoft.com/en-us/cli/azure/install-azure-cli-linux since each distribution will use a different command. But for Debian/Ubuntu, the following command will suffice:

curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash

Installing the Azure CLI on macOS

On macOS, you can use Homebrew to install the Azure CLI and keep it updated. It has been tested with macOS version 10.9 and later and again, it is the most convenient approach among others. Simply run the following command to get it installed in no time:

brew update && brew install azure-cli

If you do not have Homebrew installed, feel free to install it from https://docs.brew.sh/Installation.html.

Installing Bicep using the Azure CLI

Once you have installed the Azure CLI, you are ready to move on to the next step, which is installing Bicep itself. Just run the install command and you should be good to go given you have the right version of the Azure CLI:

az bicep install

If you wanted to upgrade your Bicep, you can use the upgrade command:

az bicep upgrade

You now have everything you need to create and compile Bicep files and deploy them.

Note

When you install Bicep using the Azure CLI, it will be an independent version and would not conflict with any other versions you might have installed separately. The Bicep version can be checked using the az bicep version command.

Installing Bicep using Azure PowerShell

At the time of writing this book, Azure PowerShell does not install Bicep automatically like the Azure CLI does. However, you can install it manually and then use Azure PowerShell commands to deploy your Bicep files. We will cover manual installation on each platform shortly, but you need to be aware that you will need Azure PowerShell version 5.6.0 or above to be able to deploy Bicep files using Azure PowerShell. The version of Azure PowerShell can be checked using the following command:

Get-Module -ListAvailable -Name Azure -Refresh

If you do not have Azure PowerShell installed, refer to https://docs.microsoft.com/en-us/powershell/azure/install-az-ps and follow the instructions. If you have installed the Bicep CLI and have made sure you have Azure PowerShell installed, you should be good to deploy your Bicep files using the normal deployment commands:

New-AzResourceGroupDeployment -ResourceGroupName <resource-group-name> -TemplateFile <path-to-bicep>

Once the command is executed, you would have the resources deployed in your Azure subscription.

Installation on Windows

To install the Bicep CLI on Windows, you have a few options. You can either use Windows Installer, Chocolatey, or winget or manually use PowerShell.

Installation using Windows Installer

You can download the installer from the following URL and install it: https://github.com/Azure/bicep/releases/latest/download/bicep-setup-win-x64.exe.

The following is the Azure Bicep CLI installation wizard:

Figure 2.2 – Azure Bicep CLI wizard

Figure 2.2 – Azure Bicep CLI wizard

You do not need to have administrator rights to do so and the CLI automatically gets added to your PATH environment variable for easy access from any terminal, whether it is the Windows terminal, the PowerShell window, or a WSL terminal.

Installation using Chocolatey

Chocolatey is a fantastic package manager for Windows that allows you to install your desired software using scripts instead of Windows installers, which is ideal for automation; think NuGet but for software. If you do not have it installed, head over to https://chocolatey.org/ and get it installed. Once done, simply run the following command to install the Bicep CLI:

choco install bicep

Like the Windows Installer, Chocolatey adds the necessary directory to your PATH environment variable, which means once done, you are ready to use the Bicep CLI in any terminal.

Installation using winget

winget is Microsoft's package manager and has similar functionalities to Chocolatey. You can discover, search for, install, upgrade, and remove applications to and from your Windows. However, you can only use it with computers with Windows 10 or above. winget has been added to Windows 10 or 11 Store for convenience, which is great since it can be updated using Windows Update. winget is included in the flight or preview version of Windows App Installer, which you can download and install from Windows Store, as shown in the following figure:

Figure 2.3 – winget installer from Microsoft Store on Windows

Figure 2.3 – winget installer from Microsoft Store on Windows

You can find out more about Winget and its features on its home page at https://docs.microsoft.com/en-us/windows/package-manager/winget/.

Once installed, run the installation command to get the Bicep CLI installed:

winget install -e --id Microsoft.Bicep

Note

winget is not a replacement for Chocolatey. It lacks features such as installing software using PowerShell scripts, installing Python packages, and deploying anything other than software such as license keys and files, to name a few. In general, it can only install MSI, MSIX, and some EXE files.

Manual installation using PowerShell

You can also install it manually using a set of commands in your PowerShell window. This will be suitable if you have other scripts to run or are using these as part of Azure Virtual Machine extensions or even the Desired State Configuration setup:

# Create the install folder

$installPath = "$env:USERPROFILE.bicep"

$installDir = New-Item -ItemType Directory -Path $installPath -Force

$installDir.Attributes += 'Hidden'

# Fetch the latest Bicep CLI binary

(New-Object Net.WebClient).DownloadFile("https://github.com/Azure/bicep/releases/latest/download/bicep-win-x64.exe", "$installPathicep.exe")

# Add bicep to your PATH

$currentPath = (Get-Item -path "HKCU:Environment" ).GetValue('Path', '', 'DoNotExpandEnvironmentNames')

if (-not $currentPath.Contains("%USERPROFILE%.bicep")) { setx PATH ($currentPath + ";%USERPROFILE%.bicep") }

if (-not $env:path.Contains($installPath)) { $env:path += ";$installPath" }

# Verify you can now access the 'bicep' command.

bicep --help

# Done!

Once these commands are executed, you will have the CLI available and you're ready to get started.

Manual installation on macOS

If you want to install the Bicep CLI on macOS, you can use Homebrew, as you saw before, or via Bash scripts.

Installing Bicep via Homebrew

To install the Bicep CLI using Homebrew, simply run the following commands:

# Add the tap for bicep

brew tap azure/bicep

# Install the tool

brew install bicep

If you face any issues during installation, you can refer to the common issues on their GitHub repository at https://github.com/Azure/azure-cli/issues.

Installing Bicep via Bash

Finally, to install the Bicep CLI on macOS using Bash, you can use the following script:

# Fetch the latest Bicep CLI binary

curl -Lo bicep https://github.com/Azure/bicep/releases/latest/download/bicep-osx-x64

# Mark it as executable

chmod +x ./bicep

# Add Gatekeeper exception (requires admin)

sudo spctl --add ./bicep

# Add bicep to your PATH (requires admin)

sudo mv ./bicep /usr/local/bin/bicep

# Verify you can now access the 'bicep' command

bicep --help

# Done!

Once done, you can start using the CLI right away.

Manual installation on Linux

Installing the Bicep CLI on Linux is a bit simpler. For regular distributions, use the following script to get it installed:

# Fetch the latest Bicep CLI binary

curl -Lo bicep https://github.com/Azure/bicep/releases/latest/download/bicep-linux-x64

# Mark it as executable

chmod +x ./bicep

# Add bicep to your PATH (requires admin)

sudo mv ./bicep /usr/local/bin/bicep

# Verify you can now access the 'bicep' command

bicep --help

# Done!

However, if you have a lightweight distribution such as Alpine, use bicep-linux-musl-x64 instead of bicep-linux-x64 in the preceding script.

Getting nightly builds

If you would like to stay ahead of others and take advantage of new features, you can use the latest pre-release version of Bicep. Currently, the team will not publish the pre-release versions, so you need to go into their GitHub Actions run history and manually download the pre-release version you are after for the main branch.

Follow https://github.com/Azure/bicep/actions to view the latest action workflow. Once there, select the latest build that has been successful, as shown in the following screenshot:

Figure 2.4 – Bicep's GitHub Action run history

Figure 2.4 – Bicep's GitHub Action run history

On the build page, select the artifact you would like to download and install it according to one of the approaches we have gone through based on your operating system.

Figure 2.5 – Artifact list on nightly builds

Figure 2.5 – Artifact list on nightly builds

Warning

These versions might have known or unknown issues much like any other beta or pre-release version of any software, so be mindful of that. Additionally, I would not recommend using these versions in any production environments unless vigorously tested.

Working with Bicep in a Docker container

We have covered many ways in which you can install Azure Bicep and get started. However, sometimes you do not wish to install it locally, or you simply want to leverage a Docker container to work with your Bicep files. In this section, we will cover how to do that.

Working with an Azure CLI Docker container

Fortunately, the Azure CLI has a Docker image that you can pull and use. Run the following commands to get the image and run it locally:

#pull the image

docker pull mcr.microsoft.com/azure-cli

#run the container

docker run -it --rm mcr.microsoft.com/azure-cli

#install bicep

az bicep install

#run the deployment using Azure CLI

az deployment group create -f main.bicep -g my-rg

Currently, the Docker image does not have Bicep pre-installed, so you need to install it. But as you saw, the installation is effortless and from here on, you have a full working environment at your disposal. This approach is also a good candidate for your continuous integration (CI) or continuous delivery (CD) pipelines if you have a container-based pipeline in place.

Summary

In this chapter, you have seen many ways in which you can install Azure Bicep, from using integrated tools such as the Azure CLI and Azure PowerShell, to manually installing the Bicep CLI on Windows, macOS, and Linux. You have also been introduced to nightly builds if you want to stay ahead of others and get the advantage of new features and bus fixes that the team publishes every night. To finish off on a high note, we saw how you could use a Docker container to deploy Bicep files to your Azure environment.

In the next chapter, we will cover the authoring experience and what it feels like for developers when they want to write Bicep files, and then validate and deploy them from their local environment.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.105.194