Last updated
Last updated
An Azure subscription. If you do not have an Azure account, . This tutorial can be completed using only the services included in an Azure .
If you are using a paid subscription, you may be charged for the resources needed to complete the tutorial.
Terraform 0.14.9 or later
The Azure CLI Tool installed
You will use the Azure CLI tool to authenticate with Azure.
Follow the below link to install Azure CLI
Terraform must authenticate to Azure to create infrastructure.
In your terminal, use the Azure CLI tool to setup your account permissions locally.
Your browser will open and prompt you to enter your Azure login credentials. After successful authentication, your terminal will display your subscription information.
You have logged in. Now let us find all the subscriptions to which you have access...
Find the id
column for the subscription account you want to use.
Once you have chosen the account subscription ID, set the account with the Azure CLI.
Next, create a Service Principal. A Service Principal is an application within Azure Active Directory with the authentication tokens Terraform needs to perform actions on your behalf. Update the <SUBSCRIPTION_ID>
with the subscription ID you specified in the previous step.
HashiCorp recommends setting these values as environment variables rather than saving them in your Terraform configuration.
In your Powershell terminal, set the following environment variables. Be sure to update the variable values with the values Azure returned in the previous command.
Create a folder called terraform-azure
Initialize your terraform-azure
directory in your terminal. The terraform
commands will work with any operating system. Your output should look similar to the one below.
We recommend using consistent formatting in all of your configuration files. The terraform fmt
command automatically updates configurations in the current directory for readability and consistency.
Format your configuration. Terraform will print out the names of the files it modified, if any. In this case, your configuration file was already formatted correctly, so Terraform won't return any file names.
You can also make sure your configuration is syntactically valid and internally consistent by using the terraform validate
command.
Validate your configuration. The example configuration provided above is valid, so Terraform will return a success message.
Run the terraform apply
command to apply your configuration.
This output shows the execution plan and will prompt you for approval before proceeding. If anything in the plan seems incorrect or dangerous, it is safe to abort here with no changes made to your infrastructure. Type yes
at the confirmation prompt to proceed.
When you apply your configuration, Terraform writes data into a file called terraform.tfstate
. This file contains the IDs and properties of the resources Terraform created so that it can manage or destroy those resources going forward. Your state file contains all of the data in your configuration and could also contain sensitive values in plaintext, so do not share it or check it in to source control.
Inspect the current state using terraform show
.
To review the information in your state file, use the state
command. If you have a long state file, you can see a list of the resources you created with Terraform by using the list
subcommand.