Hi, I am Niekis Aurimas, and I am a System division leader at GMO Research. I have been working for GMO for over a year.
I was born and raised in Lithuania, but I lived in Germany and the United Kingdom for a few years before moving to Japan last year before CODID-19.
Since last winter, I have been working on a new project structure at GMO, which would include all the latest technologies and practices for our new and old projects to be based on.
For this article, I decided to introduce a tool called Terraform.
Which we are using for our future project structure, to automate infrastructure development and deployment. It is really interesting and easy to use even for developers who have not much experience in infrastructure technologies and allows developers to participate in infrastructure planning and development.
What is Terraform?
Terraform is an open-source infrastructure as a code (IaC) software tool developed by HashiCorp. It allows users to define and provision the complete infrastructure using a simple and easy-to-learn declarative language.
It is an infrastructure provisioning tool with which you can define infrastructure setup as codes. There are many alternatives that provide similar functionality, such as Saltstack, Pulumi, and many more.
This is HashiCorp’s official YouTube page.
What are the benefits of using Terraform?
- Does orchestration, not just configuration management
- Supports multiples cloud providers such as AWS, Azure, GCP, DigitalOcean, and many more third party providers
- Can work directly with bare-metal hardware not only cloud providers
- Provides immutable infrastructure where configuration changes smoothly
- Uses easy to understand language HCL (HashiCorp configuration language)
- Easily portable with any other provider
- It supports Client only architecture, so no need for a dedicated server for configuration management.
Terraform Core Concepts
There are a few core concepts and terminologies used in Terraform:
- Variables: Probably an essential idea is variables which are also used as input-variables. It is a key-value pair used by Terraform modules to allow customization of blocks.
- Provider: It is a plugin to interact with the API of services, software, and hardware.
- Module: It is a folder with Terraform templates where all configurations are defined.
- State: It consists of cached information about the infrastructure managed by Terraform and the related configurations.
- Resources: It refers to a block of one or more infrastructure objects (compute instances, virtual networks, etc.) used in configuring and managing the infrastructure.
- Data Source: It is implemented by providers to return information on external objects to terraform.
- Output Values: These are return values of a terraform module that other configurations can use.
- Plan: It is one of the stages that determine what needs to be created, updated, or destroyed to move from the real/current state of the infrastructure to the desired state.
- Apply: It is one of the stages where it applies the changes actual/current state of the infrastructure in order to move to the desired state.
Terraform Lifecycle
Terraform lifecycle consists of – init, plan, apply, and destroy.
- Terraform init initializes the working directory, which consists of all the configuration files
- Terraform plan is used to create an execution plan to reach a desired state of the infrastructure. Changes in the configuration files are done in order to achieve the desired shape.
- Terraform apply then makes the infrastructure changes as defined in the plan, and the infrastructure comes to the desired state.
- Terraform destroy is used to delete all the old infrastructure resources, which are marked tainted after the apply phase.
How Terraform Works?
Terraform has two main components that sum up its architecture:
- Terraform Core
- Public Modules from Community (Providers)
Source : https://www.terraform.io
Terraform Core
Terraform core uses two input sources to do its job.
The first input source is a Terraform configuration that you, as a user, configure. Here, you define what needs to be created or provisioned. And the second input source is a state where terraform keeps the up-to-date shape of how the infrastructure’s current setup looks.
So, what Terraform core does is it takes the input, and it figures out the plan of what needs to be applied. It compares the state, what is the current state, and what the configuration is that you desire in the end result. It figures out what needs to be applied to get to that desired state in the configuration file. It figures all required processes such as creation, update, removal of what needs to be done to reach the desired state of infrastructure.
Public Modules
The second component of the architecture is public modules, aka providers for specific technologies. This could be cloud providers like AWS, Azure, GCP, or other infrastructure as a service platform. It also can be lower or higher-level components such as VMware vSphere or Kubernetes or other software or service like GitHub.
It gives you the possibility to define your infrastructure on different levels.
For example – create an AWS infrastructure, create AWS EKS instances, configure all required firewalls and routes, create services/components inside that Kubernetes cluster and define a deployment plan.
Conclusion
To wrap up, the Terraform is a universal tool that can be used from enterprise-level companies to low-level home projects. It would take many articles to explain all possible use cases, but I hope my introduction at least helped to understand the core idea of the Terraform.