Breyta

Deila með


Configure Terraform in Azure Cloud Shell with Bash

Terraform enables the definition, preview, and deployment of cloud infrastructure. Using Terraform, you create configuration files using HCL syntax. The HCL syntax allows you to specify the cloud provider - such as Azure - and the elements that make up your cloud infrastructure. After you create your configuration files, you create an execution plan that allows you to preview your infrastructure changes before they're deployed. Once you verify the changes, you apply the execution plan to deploy the infrastructure.

This article presents you with the options to authenticate to Azure for use with Terraform.

In this article, you learn how to:

  • Configure Cloud Shell
  • Display current Azure account
  • Understand common Terraform and Azure authentication scenarios
  • Authenticate via a Microsoft account from Cloud Shell (using Bash or PowerShell)
  • Authenticate via a Microsoft account from Windows (using Bash or PowerShell)
  • Create a service principal using the Azure CLI
  • Create a service principal using Azure PowerShell
  • Specify service principal credentials in environment variables
  • Specify service principal credentials in a Terraform provider block

1. Configure your environment

  • Azure subscription: If you don't have an Azure subscription, create a free account before you begin.

2. Open Cloud Shell

  1. If you already have a Cloud Shell session open, you can skip to the next section.

  2. Browse to the Azure portal

  3. If necessary, log in to your Azure subscription and change the Azure directory.

  4. Open Cloud Shell.

    Open Cloud Shell from the top menu in the Azure portal.

  5. If you haven't previously used Cloud Shell, configure the environment and storage settings.

  6. Select the command-line environment.

    Select the CLI you want to use in Cloud Shell.

3. Install latest version of Terraform in Azure Cloud Shell

Cloud Shell automatically updates to the latest version of Terraform. However, the updates come within a couple of weeks of release. This article shows you how to download and install the current version of Terraform.

  1. Determine the version of Terraform being used in Cloud Shell.

    terraform version
    
  2. If the Terraform version installed in Cloud Shell isn't the latest version, you see a message indicating that the version of Terraform is out of date.

  3. If you're fine working with the indicated version, skip to the next section. Otherwise, continue with the following steps.

  4. Browse to the Terraform downloads page.

  5. Scroll down to the Linux download links.

  6. Move your mouse over the 64-bit link. This link is for the latest 64-bit Linux AMD version, which is appropriate for Cloud Shell.

  7. Copy the URL.

  8. Run curl, replacing the placeholder with the URL from the previous step.

    curl -O <terraform_download_url>
    
  9. Unzip the file.

    unzip <zip_file_downloaded_in_previous_step>
    
  10. If the directory doesn't exist, create a directory named bin.

    mkdir bin
    
  11. Move the terraform file into the bin directory.

    mv terraform bin/    
    
  12. Close and restart Cloud Shell.

  13. Verify that the downloaded version of Terraform is first in the path.

    terraform version
    

4. Verify the default Azure subscription

When you log in to the Azure portal with a Microsoft account, the default Azure subscription for that account is used.

Terraform automatically authenticates using information from the default Azure subscription.

Run az account show to verify the current Microsoft account and Azure subscription.

az account show

Any changes you make via Terraform are on the displayed Azure subscription. If that's what you want, skip the rest of this article.

Troubleshoot Terraform on Azure

Troubleshoot common problems when using Terraform on Azure

Next steps