Table of Contents
Introduction
The type of resources that you can create with your Terraform scripts are defined by the providers. A resource however is an infrastructure component that you plan to deploy using your Terraform Script. A few examples of resources could be Virtual Networks, Virtual Machines, Load Balancers, Public IPs and many more.
Terraform Resources
Resources are usually defined within a construct called Resource Block. A resource block defines what type of resource we are going to create and the assigns it a unique name. A combination of resource type and resource name assigned can be later used to reference this resource somewhere else in the Terraform script.
The parameters defined for the resource creation within a resource block are known as Resource Arguments. Every Terraform provider has its own documentation, describing its resource types and their arguments. In order to call a resource attribute somewhere else I the code you need to use the following syntax:
<RESOURCE TYPE>.<NAME>.<ATTRIBUTE>
When you do a ‘Terraform apply’ you create the infrastructure/resources defined within your resource block while maintaining a local state file to identify what all resources are created. This state file is updated every time you create new or deleted/modify the existing resources.
While you define the resources in your Terraform scripts, there could be some resource that might need to be created before the other resource can be created. To achieve this, you could use a construct of dependencies in your Terraform code. Most of the dependencies are handled automatically by Terraform by analyzing the expression within a resource block and these are known as implicit dependencies. However, you can create an explicit dependency in your resource block using depends_on meta-argument.
How to Create a Resource Block
Let us look at a simple example of creating an Azure Resource Group:
#Create Resource Group
provider “azurerm” {
version = “2.2.0”
features {}
}
resource “azurerm_resource_group” “web_rg” {
name = “Test-Resources”
location = “west-us”
}
In the above script we have following:
- Resource Type: Azure Resource Group (azurerm_resource_group where azurerm is the Azure provider for this script)
- Resource Name: Azure Resource Group Name local to terraform script.
- Name: Actual name of the resource group created on Azure.
- Location: Azure Region in which the resource group will be created on Azure.
How to Deploy a VNET on Azure within the Resource-group
Now consider you need to deploy a VNET on Azure within the resource-group you have created above you will need to add block as below:
resource “azurerm_virtual_network” “web-vnet” {
name = “Test-VNET”
#Referring to the Resource Group created and assigning VNET in same region as Resource Group.
location = azurerm_resource_group.web_rg.location
# Assigning the VNET in the already created Resource-group (Test-Resources)
resource_group_name = azurerm_resource_group.web_rg.name
# Assigning Address Space to VNET
address_space = [“10.0.0.0/16”]
}
Useful Meta-Arguments
Finally, there are few meta-arguments of use that you can define within your resource block as below:
- Depends_on: This is used to create an explicit dependency of a resource on another resource. The support for this is only available from Terraform version 0.13 onwards. It only accepts string inputs.
- Count: It accepts input as whole numbers and is used to create an equivalent number of resources as defined in the whole number argument.
- For_each: It accepts map or string as input variables. This is like a For loop for Terraform scripts and can be used like count to create multiple copies of a resource from a single resource block. Each crated resource is separately created, updated and can be destroyed.
FAQs Related to Terraform
Name some major market competitors of Terraform.
- Some of the major competitors of Terraform are: Kubernetes, Ansible, Packer and Cloud Foundry.
Why is Terraform used for DevOps?
- This is because, Infrastructure as code is the foundation for DevOps and Terraform manages Infrastructure as code.
Can we use Terraform for an on-prem infrastructure?
- Yes, we can use Terraform for an on-prem infrastructure.
Name some of the built-in provisioners available in Terraform.
- Some of the built-in provisioners available in Terraform are: Chef Provisioner, Salt-masterless Provisioner, File Provisioner, Remote-exec Provisioner, Habitat Provisioner, Puppet Provisioner & Local-exec Provisioner
What is Terraform D?
- It is a plugin that is used on most in-service systems and Windows.
Continue Reading:
ABOUT THE AUTHOR
I am here to share my knowledge and experience in the field of networking with the goal being – “The more you share, the more you learn.”
I am a biotechnologist by qualification and a Network Enthusiast by interest. I developed interest in networking being in the company of a passionate Network Professional, my husband.
I am a strong believer of the fact that “learning is a constant process of discovering yourself.”
– Rashmi Bhardwaj (Author/Editor)