Terraform — Some Introduction!

Terraform — Some Introduction!

·

4 min read

There are many tools available for the configuration management in the software industry. Ansible, chef, puppet, salt to name a few popular ones. But when it comes to infrastructure management there are a few tools like Terraform, cloud formation [from AWS], ansible.

Why Terraform ?

Infrastructure provisioning is the first step in any organization’s application delivery process, because without the infrastructure we will not be able to deploy the applications. Due to the adoption of the DevOps and agile development methodologies, automation is the de facto in every stage of the software development life cycle. Having said that, writing infrastructure as code has become the standard for storing the infrastructure configuration and versioning the same. Terraform has become very popular tool in this space due to its simplicity and the support it has for variety of cloud providers.

What is Terraform?

As quoted in the official website https://www.terraform.io/intro/index.html

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions.

Terraform is a tool for capturing the blueprint of the required infrastructure in code, building this infrastructure and versioning the same. So this really helps in storing your infrastructure configuration in code, store it in version control system and work on it like you do for your application code. Terraform can be used to spin-up a single instance to create entire data center .

Benefits :

  1. Infrastructure as code : Your complex infrastructure can be captured in code and store this configuration scripts in version control system.

  2. No Human error : Since infrastructure is created using code, there is no manual creation and hence no human error.

  3. Backup of configuration : Even if there is a disaster and you loose the infrastructure, you always have a copy of the infrastructure configuration stored as code and you can bring up the entire infrastructure in minutes.

  4. Multi-cloud support : Terraform supports the multi cloud strategy with the generic workflow applies to entire infrastructure with different components from different cloud providers.

How Terraform works ?

Lets see the architecture of the terraform first:

Architecture :

High level architectureHigh level architecture

Terraform core module is responsible for reading the configuration from the terraform config files, create dependency graph from that and store the state of the infrastructure. Also it executes the following methods:-

diff() — Gets the difference between desired state and current state. Apply() — Applies the configuration to bring the current state to desired state. Refresh() — Refreshes the current state.

Depending on the state of the infrastructure it calls the provider by invoking different methods like create (),read(),update(),delete() etc. Provider connects to its upstream APIs to do the needful. So the responsibility of the core module is simple and it treats the provider as one black box. It calls the provider and provider take care of creation/updation/deletion of the resources. So terraform core is responsible for state transitions of the infrastructure resources and provider actually works on creating/updating the resources.

Terraform creates an execution plan for your configuration and builds the same. Terraform config files are written using the Hashicorp Configuration Language(HCL).

Basically there are two steps involved in creating an infrastructure.

Terraform plan : -

This step creates an execution plan and show exactly what it is going to do when you do ‘terraform apply’. This step is very important because you will get to know what is going to be created is what you intend to. If there is any discrepancy in the same, you can change the configuration and do terraform plan again.

Terraform apply : -

This step applies the configuration as per the execution plan created in the previous step. Creates all the resources mentioned in the terraform configuration files. Once this step is completed you will have the infrastructure resources ready and can start deploying the applications. Terraform creates the resources very efficiently. It has the built-in intelligence where it creates the resources which are dependent of each other in the sequential order and the resources which doesn’t have any dependency will be created parallelly.

Hope this gave some idea on Terraform and we will see one practical example in the next blog!!

Hope you learnt something!!

Follow me on twitter for interesting articles..

Thanks,