Terraform is an IAC (Infrastructure as a Code) tool that uses declarative approach to provision Infrastructure in an automated way.
Terraform is a state-based tool. It is a tool where you define your Desired State (in configuration files) and terraform ensures that ‘Current State’ of Infrastructure matches with the defined ‘Desired State’.
How it all works?
- Define the desired infrastructure in the form of code in configuration files. ‘.tf’ is the file extension of Configuration files.
- Terraform maintains the state of current state of Infra in ‘.tfstate’ file
- As soon as you would want to apply your desired state, Terraform would refresh the ‘.tfstate’ file i.e. it would check the real world resources (Current State) and would update the information in ‘.tfstate’ file. Then it would compare the ‘.tf’ file (desired state code) with the ‘.tfstate’ file (current state) and if there would be any differences, those would be implemented in the real world. This would also ensures that ‘Desired State’ would match the ‘Current State’
Terraform Advantages
- Terraform supports multiple cloud providers like AWS, Azure, GCP etc.
- Portability – We can switch to any provider of our choice at any time. Unlike Cloud Formation which can only provision resources in AWS.
- Immutable Infrastructure – If any change is done manually in the resource managed by Terraform, terraform would destroy the whole resource and would recreate that resource again.
- HCL (HashiCorp configuration language) used in Terraform is easy to read.
Terraform Stages
Terraform commands– Init, Plan, Apply, Destroy
- Terraform Init – Terraform downloads the plugins for the providers (aws, azure etc), being used, to be able to connect to the APIs and provision infrastructure. To download these plugins, we need to run command terraform init.
- Terraform Plan – This command is to do dry run. Running this command would compare the current infra (probably in cloud) and update the .tfstate file. Then it would compare ‘desired state’ code in configuration files with the ‘.tfstate’ file (current state) and would show what all changes that would be done. This command would not change anything. It would only show us the execution plan
- Terraform Apply – This command would implement the execution plan that is shown while we run terraform plan command. This would mean that it would implement the desired state and ensures that our current state matches with desired state.
- Terraform Destroy – This command would destroy all the resources managed by Terraform in a particular environment.
Providers and File Types
Understanding Providers
Provider is an executable plugin that acts as a mediator and help terraform connect to environment where the resources are supposed to be provisioned. As an example, if we want to provision our resources in AWS, we need to declare provider in configuration file. As soon as we run terraform init, it would download the required plugin, which would help in connecting with AWS and provision the desired resources.
Files Types
- Configuration Files (.tf ) – We declare resources and providers in these files (*.tf)
- Terraform.tfstate – It is state file that maintains the state of our current infra in a json format. This file is created when we run terraform apply for the first time.
- Variable.tf – We declare variables in this file. We can also define the type of variables and their default values.
- Terraform.tfvars – In this file, we assign values to variables defined in variable.tf file.