Skip main navigation

Infrastructure As Code with Terraform

Terraform is an open-source Infrastructure as Code tool. It can be used to define a data centre by building, changing, and versioning infrastructures.

In the last step, we learned about environment deployment and in this step, we will investigate Infrastructure as Code with Terraform. Terraform is an open-source Infrastructure as Code tool. You can use it to define a data centre by building, changing and versioning infrastructures.

Terraform has four key features:

  • Infrastructure as Code
  • Execution Plans
  • Resource Graph
  • Change Automation

The tool uses a high-level configuration from which it generates an execution plan describing what it will do to reach the desired state and then executes it to build the described infrastructure.

If this configuration changes, Terraform is able to verify what changed and it creates incremental execution plans that can be applied.

Terraform builds a graph of your resources, and parallelises the creation and modification of any non-dependent resources. It supports custom in-house infrastructures and popular cloud providers such as Microsoft Azure. Terraform can manage Azure low-level components such as Storage and Compute, and also high-level components such as Security Groups or Resource Groups. Let’s take the example of creating an Azure Resource Group. To do that, you should start by creating a new Terraform template (main.tf):

resource "azurerm_resource_group" "my-terraform-group" {
name = "my-resource-group-name"
location = "West US"
}

This should be followed by initialising a working directory containing Terraform configuration files.

terraform init

Preview the resources to be created by the Terraform template with:

terraform plan

Finally, provision the Azure resources with:

terraform apply

After applying this latest change to your Azure infrastructure, the actual state of the new infrastructure is stored in a terraform.tfstate file that was created after the first run of Terraform. By default, Terraform stores the state locally in a file named terraform.tfstate.

The fact that this file is stored locally makes collaboration on a shared infrastructure complex, given the merge conflicts that could result. To let a team collaborate on a shared infrastructure, one state should be used, and that is why Terraform allows the remote state feature. This is done by configuring Terraform to use Azure as a back end and Azure Blob Storage as a storage tool.

Have you shared an experience or engaged with a peer? Drop a comment or a question in the comment box below and then click on Mark as complete. In the next step, we will learn about environment configuration.

This article is from the free online

Microsoft Future Ready: Fundamentals of DevOps and Azure Pipeline

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now