Tutorial: Configure Azure Cloud Shell for Terraform

Terraform works well from a Bash command line in macOS, Windows, or Linux. Running your Terraform configurations in the Bash experience of Azure Cloud Shell has some unique advantages. This tutorial shows how to write Terraform scripts that deploy to Azure by using Cloud Shell.

Use Azure Cloud Shell

Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. You can use either Bash or PowerShell with Cloud Shell to work with Azure services. You can use the Cloud Shell preinstalled commands to run the code in this article without having to install anything on your local environment.

To start Azure Cloud Shell:

Option Example/Link
Select Try It in the upper-right corner of a code block. Selecting Try It doesn't automatically copy the code to Cloud Shell. Example of Try It for Azure Cloud Shell
Go to https://shell.azure.com, or select the Launch Cloud Shell button to open Cloud Shell in your browser. Launch Cloud Shell in a new window
Select the Cloud Shell button on the top-right menu bar in the Azure portal. Cloud Shell button in the Azure portal

To run the code in this article in Azure Cloud Shell:

  1. Start Cloud Shell.

  2. Select the Copy button on a code block to copy the code.

  3. Paste the code into the Cloud Shell session by selecting Ctrl+Shift+V on Windows and Linux or by selecting Cmd+Shift+V on macOS.

  4. Select Enter to run the code.

Automatic credential configuration

Terraform is installed and immediately available in Cloud Shell. Terraform scripts authenticate with Azure when logged in to Cloud Shell to manage infrastructure without any additional configuration. Automatic authentication bypasses two manual processes:

  • Creating an Active Directory service principal
  • Configuring the Azure Terraform provider variables

Use modules and providers

Azure Terraform modules require credentials to access and modify Azure resources. To use Terraform modules in Cloud Shell, add the following code:

# Configure the Microsoft Azure Provider
provider "azurerm" {

Cloud Shell passes required values for the azurerm provider through environment variables when you use any of the terraform CLI commands.

Other Cloud Shell developer tools

Files and shell states persist in Azure Storage between Cloud Shell sessions. Use Azure Storage Explorer to copy and upload files to Cloud Shell from your local computer.

The Azure CLI is available in Cloud Shell and is a great tool for testing configurations and checking your work after terraform apply or terraform destroy finishes.

Next steps