Long gone are the days where there was a clear line of separation between operations or sysadmin roles and developers, where infrastructure was deployed manually.
Nowadays thanks to the cloud, applications, services and the infrastructure that supports them are dynamic and ever changing, sometimes it’s hard to keep up the phase.
Nevertheless, we have a great tool to keep up the pace and be able to manage and deploy cloud resources in a programmatic way: Terraform.
Terraform is an open-source infrastructure as code software tool created by HashiCorp. Users define and provision data center infrastructure using a declarative configuration language known as HashiCorp Configuration Language (HCL), or optionally JSON.
HCL seems like a lighter version of JSON (let’s just pretend yaml doesn’t exist) where we can define resources in a programmatic way and deploy them to Azure. So without further ado, let’s get down to business:
Requirements
- https://learn.hashicorp.com/tutorials/terraform/install-cli
- https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest
- Execute the commands in a command line (git bash for example):
- az login (login into azure with your credentials)
- az account set –subscription <subscription_id> (select the subscription you want to use)
- Create a file called main.tf
- Use any editor of your choice, I highly recommend visual studio code.
Definition of the resources using terraform
Create a file called main.tf with the following content:
terraform {
required_version = ">= 0.12.6"
backend "local" {
path = "./terraform.tfstate"
}
}
provider azurerm {
version = "~> 2.2.0"
features {}
}
Terraform uses an “state”, which basically is a json file that tracks the state of all the resources you have deployed, there are several ways to store this state, in this case we will use a local state, but you could use terraform cloud or and storage account (more details here: https://www.terraform.io/docs/state/index.html).
Also in this file we define the required version of terraform to be used and the minimal version of the azure resource manager provider for terraform (https://www.terraform.io/docs/providers/azurerm/index.html)
Let’s start with the most basic building block in azure, a resource group that can be defined in the following way:
resource "azurerm_resource_group" "rg" {
name = "rg-my-rg"
location = "west europe"
}
We use a resource group as a container to hold and organise all the other resources.
Then we deploy an storage account inside the resource group:
resource "azurerm_storage_account" "storage" {
name = "stmyuniquestorageacc01"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
account_kind = "StorageV2"
account_tier = "Standard"
account_replication_type = "ZRS"
access_tier = "Hot"
enable_https_traffic_only = true
}
The syntax is quite simple and easy to understand, it is important to know that in order to understand each resource we should always refer to the official documentation to see examples and all the possible arguments:
https://www.terraform.io/docs/providers/azurerm/r/storage_account.html
the name of the storage account should be unique.
Deploying of the resources using terraform
In order to deploy our resources we use 3 basic terraform commands, to be executed in the same folder as main.tf
terraform init -reconfigure
This will initialise all the modules and configure our terraform state backend, in this case we are using a localstate.
terraform plan
This command will analyse the terraform files and show us a deployment plan where we can review what actions are going to be performed.
terraform apply
This command will show us again the plan and asks us our confirmation before deployment, if we say yes the resources will be deployed.
And voila! Your first deployment using terraform. Next time we will deploy more advanced resources such as web apps and virtual machines. Stay tuned!