Terraform Defined
According to HashiCorp (developers of Terraform),
Terraform is an open-source infrastructure as code software tool that provides a consistent CLI workflow to manage hundreds of cloud services. Terraform codifies cloud APIs into declarative configuration files.
Before we proceed, if you do not have an understanding of Infrastructure-as-Code, do read up this article. It is also important to have knowledge about cloud computing as Terraform is a tool used in cloud processes. A proper understanding of cloud computing would help you learn about Terraform easily. Refer to the links for more insights.
Understanding Terraform
Terraform is a tool for infrastructure provisioning (a case where the underlying infrastructure on which your application will run, can be automatically provisioned). It helps in reducing the average time to provision, service complexity and improves the overall efficiency with decreased costs. It allows you to automate and manage your infrastructure, platform and services that run on that infrastructure.
Terraform is a stateful application. What that means is that it keeps track of everything it builds in your cloud environments, so that if you need to change something or delete something later, Terraform will know what it built, and it can go back and make those changes for you.
Terraform also uses a declarative language - no need for definition of every step of how automation and management is done, just declare what you want/final result and terraform will figure out how to execute it. Terraform can be run on different platforms including Azure, AWS, GCP etc. Based on these 3 cloud providers mentioned, they each have their own native ways of handling infrastructure as code. For AWS - Cloudformation, for Azure - Azure Resource Manager, for GCP- Google Cloud Deployment Manager.
All infrastructure resources can be defined within configuration files that has a ".tf" file extention.
The declarative language Terraform uses is known as HCL( HashiCorp Configuration Language).
HCL SYNTAX
It consists of blocks and arguments in key value pair format representing the configuration data. It is used to define resources and variables.
Block :
It acts as a container for other contents. Contains information about the infrastructure platform and a set of resources within that platform that we want to create. It has curly braces and the arguments to be used can be found in it.
Argument :
An argument assigns a value to a particular name. It is found within the block written in key = value pair format. They are specific to type of resource being created. Each key would be a string while the value could be any of Terraform's different data types. We can also have nested blocks inside of the main block. They usually start with the name of the nested block and curly braces. What is contained inside it is more of key value pairs. Within an argument we have:
Resource Type
A fixed value which depends on the provider where we want to create the resource.
- Provider: They are the plugins that Terraform uses to manage resources.
- Resource: They define the data types and API interactions required to create, update, and destroy infrastructure with a cloud vendor.
Resource Name:
Logical name used to identify the resource and it can be named anything.
Looking at this example:
resource "local_file" "pet" {
filename = "/root/pets.txt"
content = "We love pets!"
}
Block name here is = resource
Arguments = filename and content
Resource type:
Provider = local
Resource = file
Resource name = pet
A practical example when making use of AWS in Terraform.
resource "aws_instance" "web_server" {
name = "web-server"
ebs_volume {
size = 40
}
}
ebs_volume
here is the nested block.
Terraform Installation
An official documentation to help you install terraform on your computers has been put together by HashiCorp
Terraform Architecture
Terraform has two main components that make up its architecture:
1. Terraform Core:
Terraform Core makes use of 2 input sources:
Terraform Configuration; that the user writes and where you define what needs to be created or provisioned.
Terraform State; where Terraform keeps the up-to-date state of how the current set up of the infrastructure looks like.
The work of Terraform core is to take the inputs, figure out the plan of what needs to be done that is to say terraform core compares the current state versus the end result/desired state (configuration file). It creates a file called the tf state or the terraform state. Terraform maintains the state of the infrastructure through this file.
2. Terraform Providers:
After a terraform configuration file has been written, we initialize the directory with the "terraform init" command which downloads and installs plugins for the providers used within the configuration. Terraform core interacts with cloud providers like AWS or Azure for infrastructure level tasks (IAAS). It also has providers for high level components like Kubernetes (PAAS), Fastly (SAAS). It does this through plugins called Terraform Providers. The providers can interact with different cloud platforms to execute the corresponding infrastructure provisioning being specified in the ".tf" files. The providers could be APIs or libraries which can communicate with the infrastructures.
Terraform Workflow
Write:
You define resources, which may be across multiple cloud providers and services. For example, you might create a configuration to deploy an application on virtual machines in a Virtual Private Cloud (VPC) network with security groups and a load balancer.
Init:
The first thing that you do after writing your code in Terraform is initializing the code using the command "terraform init". Terraform init would look for configuration files inside the working directory and examine them to know if they need provider plugins. If they do, it downloads the plugins from the public Terraform registry or any specified location. You can use the init command for: Plugin Installation, Child Module Installation and Backend Initialization
Plan:
Terraform creates an execution plan describing the infrastructure it will create, update, or destroy based on the existing infrastructure and your configuration. It is used to check whether the execution plan matches your expectations without making any changes to real resources or to the state.
If Terraform discovers no changes to resources, then the "terraform plan" indicates that no changes are required to the real infrastructure.
Apply:
On approval, Terraform performs the proposed operations in the correct order, respecting any resource dependencies. For example, if you update the properties of a VPC and change the number of virtual machines in that VPC, Terraform will recreate the VPC before scaling the virtual machines. Terraform apply command is used to create or introduce changes to real infrastructure. By default, apply scans the current working directory for the configuration and applies the changes appropriately. However, you’ll optionally give the path to a saved plan file that was previously created with terraform plan.
Destroy:
The terraform destroy is used to destroy infrastructure governed by terraform. To check the behavior of terraform destroy command at any time we can use terraform destroy command.
Use Cases of Terraform
Multi-Cloud Deployment
Terraform's native ability to support multiple cloud services helps increase fault tolerance. One of the main appeals of Terraform is how it works across all the cloud providers at the same time, unlike many of Terraform’s direct competitors which work primarily with a single cloud provider only. Multi-cloud deployments add complexity because each provider has its own interfaces, tools, and workflows. Terraform lets you use the same workflow to manage multiple providers and handle cross-cloud dependencies. This simplifies management and orchestration for large-scale, multi-cloud infrastructures.
Self-service clusters
The registries make it easy for users to find prepackaged configurations that can be used as it is or modified to meet a particular need in a case of repetitive infrastructure requests as it allows product teams manage their own infrastructure independently.
External resource management
Terraform supports public and private cloud infrastructure, as well as network appliances and software as a service (SaaS) deployments.
Disposable Environments
Using Terraform, the production environment can be codified and then shared with staging, QA or dev. These configurations can be used to rapidly spin up new environments to test in, and then be easily disposed of. Terraform can help tame the difficulty of maintaining parallel environments, and makes it practical to elastically create and destroy them.
Credits to geeklabs, k21, kodekloud, TechWorld with Nana and other platforms which facilitated my understanding on Terraform.
Ps. This is an introductory article, I would be releasing a practical article showing how to use Terraform with Azure and AWS soon.
Reach out to me of you have any question, thanks for reading!