£199.99 £139.99 for one year of Unlimited learning. Offer ends on 14 November 2022 at 23:59 (UTC). T&Cs apply

Find out more
Infrastructure As Code with Terraform
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

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education