In the current IT industry, a combined skillset of ‘Automation + Cloud’ is one of the most desired by the IT companies. Having knowledge of these in present times can give you an edge in the competitive IT world across the globe.
What is Terraform?
Pretty much, everyone by now must have heard about term ‘Infrastructure-as-a-code (IAAC)’. Infrastructure as code automates the provisioning of infrastructure on Public/Private Clouds helping the organisation to deploy, manage & scale the infrastructure with great speed, lesser risks, and reduced costs.
Terraform is a power IAAC automation tool developed by Hashicorp that has gained popularity recently as it easy-to-use and can be used with almost all major cloud providers AWS/Azure/GCP/OCI etc.
Advantages of Using Terraform:
- Can do orchestration as well as configuration management.
- Uses easy to understand language, HCL (HashiCorp configuration language). Infrastructure components are described in a declarative manner in Terraform configuration file.
- Is supported across all major Cloud Service Providers, hence is Cloud Agnostic.
- Provides for the state management with the help of state files. Referring to state file you can always get an idea of the Infrastructure deployed using terraform. Terraform lets you manage state remotely, which helps prevent confusion amongst team members in case they attempt to recreate the infrastructure.
Components of Terraform
- Provider: Responsible for interfacing Terraform with Cloud Providers (Azure/AWS/GCP etc). Provider defines resources that you can create using Terraform.
- Terraform relies on plugins called “providers” to interact with remote systems.
- Each Terraform config file must declare a provider, & each resource type is implemented by a provider.
- main directory of publicly available Terraform providers: https://registry.terraform.io/browse/providers
- Resources: Resources in terraform maps to resources in Cloud. Each resource block describes one or more infrastructure objects, such as virtual networks, compute instances etc. It is made up of Attributes. Attributes define how resource will be configured in Cloud.
- Attributes: Arguments to the resources are passed as readable attributes. These arguments can be defined statically or could be defined using Variables.
- Variables: Also used as input-variables, it is key-value pair used by Terraform modules to allow customization.
- Module: It is a folder with Terraform templates where all the configurations are defined.
Important File Types in Terraform Module:
- tf – This the configuration file where you define the providers you are going to use and also define your resources to be created.
- tf – In this file you define your variables and their types.
Variable types are divided into two categories:
- Basic Types: string, number, Boolean
- Complex types: list, map
- tfvars – It is used to assign the values to the created variables.
- tfstate – This state file contains information about the provisioned infrastructure which terraform manage. Whenever we change the configuration file, it automatically determines which part of your configuration is already created and which needs to be changed with help of state file. This files automatically gets created when we do a terraform apply.
All these files put under a working directory on your system are then called as Terraform Module.
It initializes to the working directory of terraform module. Always the first command to be run and can be run multiple times.
It is used to create an execution plan for the terraform code. It will perform a refresh when re-run. Terraform plan might be run before committing a change to version control, to create confidence that it will behave as expected.
The terraform apply command is used to apply the changes required to reach the desired state of the configuration, or the pre-determined set of actions generated by a terraform plan execution plan.
It is used to destroy the Infrastructure managed by Terraform. This will ask for confirmation before destroying. If -auto-approve is set, then the destroy confirmation will not be shown.