Terraform configuration

The files that are used to build the infrastructure are simply known as the Terraform configuration. The configuration declares the desired state and it is up to Terraform and the provider (AWS, Azure, Google…) to determine how to create and configure the infrastructure to match the desired state. Some of the common pieces of the Terraform configuration are:

Provider
Terraform uses API calls to build the infrastructure on the required platform (AWS, Azure, Google…) A provider connects the resources that have been declared in the configuration to API calls that are needed to create, read, update, delete the resource in the appropriate platform.

The provider plugin is outside of the Terraform code base and is downloaded when Terraform init is run. The version of the provider plugin downloaded can be controlled with the version = as shown in the below azure provider block

provider "azurerm" {
      version = "~> 1.35"
 }

Resources
The resource block declares the desired state for a resource of the infrastructure. A resource can be a physical item such as a network card and storage or it could be a logical resource like an application.

Below is an example of a resource block that provisions an Azure resource group

resource "azurerm_resource_group" "rsg" {
     name     = "ResourceGroup"
     location = "eastus"
 }

A resource block must have two string parameters, the resource Type (which is always first) and the resource Name (second). The combination of the two must be unique in the configuration. In the above example “azurerm_resource_group” is the resource type and rsg is the resource name. The format of referring to an object created in Terraform is type.name so the above-created resource group is referred to as azurerm_resource_group.rsg.

The section of the block between the {} is where the statements for the resource are defined. The options that can be specified depend on the resource that will be created and may have both required and optional statements. In the above example, Name and Location are both required when creating a resource group in Azure.

Leave a Reply

Your email address will not be published. Required fields are marked *