You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: Install and configure Terraform to provision VMs and other infrastructure in Azure | Microsoft Docs
2
+
title: Install and configure Terraform for use with Azure | Microsoft Docs
3
3
description: Learn how to install and configure Terraform to create Azure resources
4
4
services: virtual-machines-linux
5
5
documentationcenter: virtual-machines
6
6
author: echuvyrov
7
-
manager: jtalkar
7
+
manager: jeconnoc
8
8
editor: na
9
9
tags: azure-resource-manager
10
10
@@ -14,22 +14,25 @@ ms.devlang: na
14
14
ms.topic: article
15
15
ms.tgt_pltfrm: vm-linux
16
16
ms.workload: infrastructure
17
-
ms.date: 10/23/2017
17
+
ms.date: 06/19/2018
18
18
ms.author: echuvyrov
19
19
---
20
20
21
21
# Install and configure Terraform to provision VMs and other infrastructure into Azure
22
22
23
-
Terraform provides an easy way to define, preview, and deploy cloud infrastructure by using a [simple templating language](https://www.terraform.io/docs/configuration/syntax.html). This article describes the necessary steps to use Terraform to provision resources in Azure.
23
+
Terraform provides an easy way to define, preview, and deploy cloud infrastructure by using a [simple templating language](https://www.terraform.io/docs/configuration/syntax.html). This article describes the necessary steps to use Terraform to provision resources in Azure.
24
24
25
-
> [!TIP]
26
-
To learn more about how to use Terraform with Azure, visit the [Terraform Hub](/azure/terraform). Terraform is installed by default in the [Cloud Shell](/azure/terraform/terraform-cloud-shell). By using Cloud Shell, you can skip the install/setup portions of this document.
25
+
To learn more about how to use Terraform with Azure, visit the [Terraform Hub](/azure/terraform).
Terraform is installed by default in the [Cloud Shell](/azure/terraform/terraform-cloud-shell). If you choose to install Terraform locally, complete the next step, otherwise continue to [Set up Terraform access to Azure](#set-up-terraform-access-to-azure).
27
30
28
31
## Install Terraform
29
32
30
-
To install Terraform, [download](https://www.terraform.io/downloads.html) the package appropriate for your operating system into a separate install directory. The download contains a single executable file, for which you should also define a global path. For instructions on how to set the path on Linux and Mac, go to [this webpage](https://stackoverflow.com/questions/14637979/how-to-permanently-set-path-on-linux). For instructions on how to set the path on Windows, go to [this webpage](https://stackoverflow.com/questions/1618280/where-can-i-set-path-to-make-exe-on-windows).
33
+
To install Terraform, [download](https://www.terraform.io/downloads.html) the appropriate package for your operating system into a separate install directory. The download contains a single executable file, for which you should also define a global path. For instructions on how to set the path on Linux and Mac, go to [this webpage](https://stackoverflow.com/questions/14637979/how-to-permanently-set-path-on-linux). For instructions on how to set the path on Windows, go to [this webpage](https://stackoverflow.com/questions/1618280/where-can-i-set-path-to-make-exe-on-windows).
31
34
32
-
Verify your path configuration with the `terraform` command. You should see a list of available Terraform options as output:
35
+
Verify your path configuration with the `terraform` command. A list of available Terraform options is shown, as in the following example output:
Configure [an Azure AD service principal](/cli/azure/create-an-azure-service-principal-azure-cli) to enable Terraform to provision resources into Azure. The service principal grants your Terraform scripts using credentials to provision resources in your Azure subscription.
42
-
43
-
There are several ways to create an Azure AD application and an Azure AD service principal. The easiest and fastest way today is to use Azure CLI 2.0, which [you can download and install on Windows, Linux, or a Mac](/cli/azure/install-azure-cli).
44
-
45
-
Sign in to administer your Azure subscription by issuing the following command:
46
-
47
-
`az login`
48
-
49
-
If you have multiple Azure subscriptions, their details are returned by the `az login` command. Set the `SUBSCRIPTION_ID` environment variable to hold the value of the returned `id` field from the subscription you want to use.
44
+
To enable Terraform to provision resources into Azure, create an [Azure AD service principal](/cli/azure/create-an-azure-service-principal-azure-cli). The service principal grants your Terraform scripts to provision resources in your Azure subscription.
50
45
51
-
Set the subscription that you want to use for this session.
46
+
If you have multiple Azure subscriptions, first query your account with [az account show](/cli/azure/account#az-account-show)to get a list of subscription ID and tenant ID values:
52
47
53
48
```azurecli-interactive
54
-
az account set --subscription="${SUBSCRIPTION_ID}"
49
+
az account show --query "{subscriptionId:id, tenantId:tenantId}"
55
50
```
56
51
57
-
Query the account to get the subscription ID and tenant ID values.
52
+
To use a selected subscription, set the subscription for this session with [az account set](/cli/azure/account#az-account-set). Set the `SUBSCRIPTION_ID` environment variable to hold the value of the returned `id` field from the subscription you want to use:
58
53
59
54
```azurecli-interactive
60
-
az account show --query "{subscriptionId:id, tenantId:tenantId}"
55
+
az account set --subscription="${SUBSCRIPTION_ID}"
61
56
```
62
57
63
-
Next, create separate credentials for Terraform.
58
+
Now you can create a service principal for use with Terraform. Use [az ad sp create-for-rbac]/cli/azure/ad/sp#az-ad-sp-create-for-rbac), and set the *scope* to your subscription as follows:
64
59
65
60
```azurecli-interactive
66
61
az ad sp create-for-rbac --role="Contributor" --scopes="/subscriptions/${SUBSCRIPTION_ID}"
67
62
```
68
63
69
-
Your appId, password, sp_name, and tenant are returned. Make a note of the appId and password.
70
-
71
-
To test your credentials, open a new shell and run the following command, using the returned values for sp_name, password, and tenant:
72
-
73
-
```azurecli-interactive
74
-
az login --service-principal -u SP_NAME -p PASSWORD --tenant TENANT
75
-
az vm list-sizes --location westus
76
-
```
64
+
Your *appId*, *password*, *sp_name*, and *tenant* are returned. Make a note of the *appId* and *password*.
77
65
78
66
## Configure Terraform environment variables
79
67
80
-
Configure Terraform to use the tenant ID, subscription ID, client ID, and client secret from the service principal when creating Azure resources. You can also set the environment if working with an Azure cloud other than Azure public. Set the following environment variables, which are used automatically by the [Azure Terraform modules](https://registry.terraform.io/modules/Azure).
68
+
To configure Terraform to use your Azure AD service principal, set the following environment variables, which are then used by the [Azure Terraform modules](https://registry.terraform.io/modules/Azure). You can also set the environment if working with an Azure cloud other than Azure public.
81
69
82
70
- ARM_SUBSCRIPTION_ID
83
71
- ARM_CLIENT_ID
84
72
- ARM_CLIENT_SECRET
85
73
- ARM_TENANT_ID
86
74
- ARM_ENVIRONMENT
87
75
88
-
You can use this sample shell script to set those variables:
76
+
You can use the following sample shell script to set those variables:
89
77
90
78
```bash
91
79
#!/bin/sh
@@ -101,7 +89,7 @@ export ARM_ENVIRONMENT=public
101
89
102
90
## Run a sample script
103
91
104
-
Create a file `test.tf` in an empty directory and paste in the following script.
92
+
Create a file `test.tf` in an empty directory and paste in the following script.
Save the file and then run `terraform init`. This command downloads the Azure modules required to create an Azure resource group. You see the following output:
103
+
Save the file and then initialize the Terraform deployment. This step downloads the Azure modules required to create an Azure resource group.
116
104
105
+
```bash
106
+
terraform init
117
107
```
108
+
109
+
The output is similar to the following example:
110
+
111
+
```bash
118
112
* provider.azurerm: version = "~> 0.3"
119
113
120
114
Terraform has been successfully initialized!
121
115
```
122
116
123
-
Preview the script with `terraform plan`, and then create the `testResouceGroup`resource group with `terraform apply`:
117
+
You can preview the actions to be completed by the Terraform script with `terraform plan`. When ready to create the resource group, apply your Terraform plan as follows:
124
118
119
+
```bash
120
+
terraform apply
125
121
```
122
+
123
+
The output is similar to the following example:
124
+
125
+
```bash
126
126
An execution plan has been generated and is shown below.
127
127
Resource actions are indicated with the following symbols:
128
128
+ create
@@ -144,8 +144,7 @@ azurerm_resource_group.rg: Creation complete after 1s
144
144
145
145
## Next steps
146
146
147
-
You have installed Terraform and configured Azure credentials so that you can start deploying infrastructure into your Azure subscription. You then tested your installation by creating an empty Azure resource group.
147
+
In this article, you installed Terraform or used the Cloud Shell to configure Azure credentials and start creating resources in your Azure subscription. To create a more complete Terraform deployment in Azure, see the following article:
148
148
149
149
> [!div class="nextstepaction"]
150
-
> [Create an Azure VM with Terraform](terraform-create-complete-vm.md)
151
-
150
+
> [Create an Azure VM with Terraform](terraform-create-complete-vm.md)
0 commit comments