Many people wonder why we use Terraform when there are already so many Infrastructure as Code (IaC) tools out there. So, before learning Terraform, let’s understand why it was created.
Terraform was made to solve some common problems with existing IaC tools. Some tools, like AWS CloudFormation, only work with one cloud provider. Others, like Ansible or Puppet, are more focused on setting up software rather than managing cloud infrastructure. Because of this, DevOps teams often had to use several different tools to get the job done, which made things complicated and harder to manage. Terraform, developed by HashiCorp, introduced a better solution. It uses a simple and readable language called HashiCorp Configuration Language (HCL), which lets you define infrastructure in a clear, declarative way. I
nstead of writing scripts that tell the system how to build something, you just describe what you want, and Terraform makes it happen. It also manages infrastructure state, so changes are predictable and safe. One of its biggest advantages is that it works across all major cloud providers, AWS, Azure, GCP, and helps DevOps teams automate and standardize infrastructure with reusable, version-controlled code. This made managing even complex cloud environments much faster and more reliable.
In this article, you will learn the fundamentals of Terraform. Whether you are a beginner, intermediate, or expert, you will be able to understand Terraform from a basic level.
Terraform is an open-source infrastructure as code (IaC) software tool that can be used to provision the infrastructure of a cloud platform. The HCL scripts used to provision infrastructure can be human-readable configuration files that can be versioned, reused, and shared. You can provision a wide range of resources in the cloud by using Terraform, like compute, storage, networking, and application services, across various cloud providers and on-premises environments.
Suppose you are working on any project in which you want to create a web server on AWS. Without Terraform, you would have to log in to the AWS website, click many buttons, and set up everything manually. This takes time and can lead to mistakes. With Terraform, you just write a short code like this:
resource "aws_instance" "web" {
ami = "ami-12345678"
instance_type = "t2.micro"
}
Then just by running terraform apply command, Terraform will automatically create the server for you. In this way you can also create multiple server by just using same script. It saves your time and reduce manual work.
Infrastructure as a Code (IaC)
Infrastructure as Code (IaC) is a method of managing and provisioning IT infrastructure(like servers,networks,databases) using code, rather than manual work. So instead of clicking buttons in a cloud dashboard, you write instructions in a file and tools like Terraform, Ansible, or CloudFormation will read that file and build everything for you. It works just like software development you can edit, share, and reuse the code, and you can track changes over time using version control systems like Git. This is particularly useful in the DevOps environment, where teams are constantly updating and deploying software.
Following are the some of the use cases of terraform.
1. Provisioning Cloud Infrastructure
Different types of cloud resources can be provisioned by using terraform like AWS,GCP and others. The resources can be managed are compute, storage, networking, and application services.
Example: A company wants to deploy 10 virtual machines and a load balancer in AWS. With Terraform, they just write the configuration once and deploy it in minutes with a single command.
2. Multi-Cloud Deployment
You can manage the infrastructure of different cloud platform at a time which will helps you to maintain the multi-cloud or hybrid cloud environments.
Example: Your application uses AWS for computing and Google Cloud for AI services. Terraform can manage both at the same time in one script.
3. Infrastructure Versioning and Collaboration
You can store the scripts which have been written to provision the infrastructure in the version control system like git form where other teams can collaborate on infrastructure changes, track revisions, and roll back to previous states if needed.
Example: A team keeps their Terraform scripts in Git. They can roll back to old infrastructure setups if something breaks just like restoring old code.
4. Saving Time by Automating Work
You don’t have to do the same setup over and over again. Just write it once and reuse it.
Example: Every time a new developer joins, Terraform can set up their cloud environment in seconds no clicks needed.
5. Managing Kubernetes Easily
Terraform can also create and manage things inside your Kubernetes cluster like pods, services, and volumes.
Example: Instead of typing long kubectl commands, you define everything in a Terraform file and apply it in one go.
6. Sharing Pre-Made Setups Across Teams
You can build Terraform modules (like templates) that others can reuse.
Example: Your team creates a ready-to-use setup for a secure database. Other teams can use the same setup without writing it from scratch.
With Terraform, users can define infrastructure resources using a simple, declarative configuration language. These resources can include virtual machines, networking components, storage resources, and more. Once the configuration is defined, Terraform can be used to create, modify, and destroy these resources in a repeatable and predictable way.To know more about terraform work flow.
One of the key benefits of Terraform is its ability to support multiple cloud providers, as well as on-premises and open-source tools. This means that users can define infrastructure resources using a single configuration and use Terraform to manage resources across different environments.
Overall, Terraform is a powerful and flexible tool that enables users to define and manage infrastructure resources in a reusable and automated way. It is widely used in a variety of industries and scenarios, including cloud infrastructure, data centers, and hybrid environments.
These files contain the definition of the infrastructure resources that Terraform will manage, as well as any input and output variables and modules. The configuration files are written in the HashiCorp Configuration Language (HCL), which is a domain-specific language designed specifically for Terraform.
This file stores the current state of the infrastructure resources managed by Terraform statefile. The state file is used to track the resources that have been created, modified, or destroyed, and it is used to ensure that the infrastructure resources match the desired state defined in the configuration files.
3. Infrastructure as Code
Terraform allows you to use code to define and manage your infrastructure, rather than manually configuring resources through a user interface. This makes it easier to version, review, and collaborate on infrastructure changes.
4. Cloud APIs or other Infrastructure Providers
These are the APIs or other interfaces that Terraform uses to create, modify, or destroy infrastructure resources. Terraform supports multiple cloud providers, as well as on-premises and open-source tools.
5. Providers
Terraform integrates with a wide range of cloud and infrastructure providers, including AWS, Azure, GCP, and more. These providers allow Terraform to create and manage resources on those platforms.
Overall, the architecture of a Terraform deployment consists of configuration files, a state file, and a CLI that interacts with cloud APIs or other infrastructure providers to create, modify, or destroy resources. This architecture enables users to define and manage infrastructure resources in a declarative and reusable way.
A private module registry is a repository for Terraform modules that is only accessible to a specific group of users, rather than being publicly available. Private module registries are useful for organizations that want to manage and distribute their own infrastructure code internally, rather than using publicly available modules from the Terraform Registry.
To use a private module registry, users need to configure their Terraform CLI to authenticate with the registry and access the modules. This typically involves setting up an access token or other authentication method and specifying the registry URL in the Terraform configuration.
Once configured, users can use the 'module' block in their Terraform configuration to reference the modules in the private registry, just like they would with publicly available modules. Private module registries can be hosted on a variety of platforms, including cloud providers, on-premises servers, and open-source tools.
Overall, private module registries are a useful tool for organizations that want to manage and distribute their own Terraform modules internally, enabling them to better control and reuse their infrastructure code.
Terraform init command initializes a Terraform working directory by downloading and installing any required plugins and dependencies. It should be run before any other Terraform commands.
$ terraform init

The validate command performs precisely what its name implies. It ensures that the code is internally coherent and examines it for syntax mistakes. Only the configuration files (*.tf) in the active working directory are examined. You must provide the -a recursive flag if you want to validate files inside of folders (for example, if you have a module/ directory).
$ terraform validate
Terraform apply command applies the changes defined in the configuration to your infrastructure. It creates or updates the resources according to the configuration, and it also prompts you to confirm the changes before applying them.
$ terraform apply

Terraform destroy command will destroy all the resources created by Terraform in the current working directory. It is a useful command for tearing down your infrastructure when you no longer need it.
$ terraform destroy
Imports an existing resource into the Terraform state, allowing it to be managed by Terraform.
$ terraform import
Opens an interactive console for evaluating expressions in the Terraform configuration.
$ terraform console
This command updates the state of your infrastructure to reflect the actual state of your resources. It is useful when you want to ensure that your Terraform state is in sync with the actual state of your infrastructure.
$ terraform refresh
Terraform is an open-source tool that is packaged into a single executable binary, which you can download and run directly from the command line. This tool helps you automate the creation and management of infrastructure. To see a list of available commands in Terraform, you can run:
terraform --help
This command will display all the available commands, with the most commonly used ones listed first. The primary Terraform commands include:
- init: Prepares your directory to run other Terraform commands.
- validate: Checks if the configuration is valid.
- plan: Shows what changes will be made to your infrastructure.
- apply: Executes the changes to create or modify your infrastructure.
- destroy: Deletes the infrastructure that was previously created.
In addition to these, there are other commands for various tasks like formatting code (fmt
), managing state (state
), and more.
Terraform uses HashiCorp Configuration Language (HCL) to define infrastructure. HCL is designed to be both easy to read by humans and understandable by machines, making it a great fit for DevOps tools.
Infrastructure elements managed by Terraform are called resources. These can include virtual machines, S3 buckets, VPCs, and databases. Each resource is defined in a block, like this example for creating an AWS VPC:
resource "aws_vpc" "default_vpc" {
cidr_block = "172.31.0.0/16"
tags = {
Name = "example_vpc"
}
}
A software element known as a Terraform provider enables Terraform to communicate with a particular infrastructure platform. The resource kinds and data sources that Terraform can handle for that platform must be implemented by providers.Cloud platforms, data centres, network devices, databases, and other resources inside the target infrastructure or service can all be defined, configured, and managed by Terraform providers.
In Terraform, a module is a container for a set of related resources that are used together to perform a specific task. Modules allow users to organize and reuse their infrastructure code, making it easier to manage complex infrastructure deployments.
Modules are defined using the ' module ' block in Terraform configuration. A module block takes the following arguments:
- source: The source location of the module. This can be a local path or a URL.
- name: The name of the module. This is used to reference the module in other parts of the configuration.
- version: The version of the module to use. This is optional and can be used to specify a specific version of the module.
Inside a module block, users can define the resources that make up the module, as well as any input and output variables that the module exposes. Input variables allow users to pass values into the module when it is called, and output variables allow the module to return values to the calling configuration. Modules can be nested, allowing users to create complex infrastructure architectures using a hierarchical structure. Modules can also be published and shared on the Terraform Registry, enabling users to reuse and extend the infrastructure code of others.
Provisioners are special tools in Terraform that let you execute commands on your infrastructure after it’s been created. For example, you can use provisioners to copy files to a virtual machine or run scripts for further configuration.
However, provisioners should be used with caution because they can complicate your setup and may require higher-level permissions. It’s best to only use them when no other Terraform constructs (like resources or modules) can achieve the same result.
Terraform keeps track of your infrastructure and its current state in a file called terraform.tfstate
. This file contains information about your infrastructure resources, which helps Terraform determine what changes to make during future operations.
The state can be stored locally on your machine, but in collaborative settings, it's usually better to store it remotely to ensure everyone on the team is working with the same state information.
The following are the advantages of using terraform:
- Declarative Configuration: Terraform uses a declarative configuration language, which means that users define the desired state of their infrastructure resources, rather than the specific steps required to achieve that state. This makes it easier to understand and manage complex infrastructure deployments.
- Support for Multiple Cloud Providers: Terraform supports multiple cloud providers, as well as on-premises and open-source tools, which means that users can define and manage their infrastructure resources using a single configuration.
- Reusable Infrastructure Code: Terraform allows users to define their infrastructure resources in a reusable and modular way, using features such as modules and variables. This makes it easier to manage and maintain complex infrastructure deployments.
- Collaboration and Version Control: Terraform configuration files can be stored in version control systems such as Git, which makes it easier for teams to collaborate and track changes to their infrastructure.
- Efficient Resource Management: Terraform has features such as resource dependencies and provisioners that enable users to manage their infrastructure resources efficiently, minimizing duplication and ensuring that resources are created and destroyed in the correct order.
As we have already discussed about the advantages of terraform let's discuss some of it's disadvantages:
- Complexity: Terraform can be complex to learn and use, especially for users who are new to infrastructure automation. It has a large number of features and can be difficult to understand the full scope of its capabilities.
- State Management: Terraform uses a state file to track the resources it manages, which can cause issues if the state file becomes out of sync with the actual infrastructure. This can happen if the infrastructure is modified outside of Terraform or if the state file is lost or corrupted.
- Performance: Terraform can be slower than some other IaC tools, especially when managing large infrastructure deployments. This can be due to the need to communicate with multiple APIs and the overhead of managing the state file.
- Limited Error Handling: Terraform does not have robust error handling, and it can be difficult to diagnose and fix issues when they arise. This can make it difficult to troubleshoot problems with infrastructure deployments.
- Limited Rollback Capabilities: Terraform does not have a built-in rollback feature, so it can be difficult to undo changes to infrastructure if something goes wrong. Users can use the ' terraform destroy ' command to destroy all resources defined in the configuration, but this can be time-consuming and may not be feasible in all situations.
Infrastructure as Code (IaC) tools are essential for automating and managing infrastructure. Terraform is a popular choice, but there are several other tools that serve similar purposes. Here’s a straightforward comparison to help you understand the differences.
The following is the comparison table between Terraform and Cloudformation:
Feature | Terraform | AWS CloudFormation |
---|
Provider Support | Works with multiple cloud providers (AWS, Azure, GCP, etc.) and on-prem services. | Limited to AWS with minimal third-party support. |
---|
Language | Uses HashiCorp Configuration Language (HCL), which is provider-neutral and declarative. | Uses YAML or JSON, tailored for AWS services. |
---|
State Management | Requires a state file to track resources (stored locally or remotely). | Manages state internally within AWS. |
---|
Flexibility | Highly flexible, with plugins for diverse services. | Focused on AWS-specific use cases. |
---|
Ease of Use | Slightly more complex due to its multi-cloud capabilities. | Easier for AWS-exclusive users. |
---|
The following is the comparison table between Terraform and Ansible:
Feature | Terraform | Ansible |
---|
Primary Use | Focuses on setting up and managing infrastructure. | Primarily for configuring systems and deploying applications. |
---|
Language | Uses HCL for infrastructure definitions. | Uses YAML for defining tasks. |
---|
Idempotency | Automatically ensures resources are created only if necessary. | Requires careful task definition to avoid duplication. |
---|
Execution | Manages infrastructure changes using plans and state. | Executes tasks immediately without state tracking. |
---|
Cloud Support | Excellent multi-cloud capabilities. | Useful for multi-cloud configurations but limited to system-level tasks. |
---|
The following is the comparison table between Terraform and Chef :
Feature | Terraform | Chef |
---|
Primary Use | Infrastructure creation and updates. | Automating system configurations. |
---|
Language | Uses HCL, which is easy to learn. | Uses Ruby-based DSL, which is more complex. |
---|
Execution Model | Plans and applies changes with tracked states. | Relies on agents for configuration changes. |
---|
State Management | Maintains a state file for consistency. | Doesn’t require state files. |
---|
Ease of Use | Simpler for managing infrastructure. | Higher learning curve, especially for beginners. |
---|
Similar Reads
DevOps Tutorial
DevOps is a combination of two words: "Development" and "Operations." Itâs a modern approach where software developers and software operations teams work together throughout the entire software life cycle, from planning and coding to testing, deploying, and monitoring.The main idea of DevOps is to i
9 min read
Introduction
What is DevOps ?
As technology keeps advancing, businesses need to deliver software faster and more reliably. DevOps helps achieve this by bringing together development and operations teams, ensuring they work as one instead of in separate silos.DevOps is more than just a set of toolsâitâs a way of working that prom
11 min read
DevOps Lifecycle
DevOps is a practice that enables a single team to handle the whole application lifecycle, including development, testing, release, deployment, operation, display, and planning. It is a mix of the terms "Dev" (for development) and "Ops" (for operations). We can speed up the delivery of applications
6 min read
The Evolution of DevOps - 3 Major Trends for Future
DevOps is a software engineering culture and practice that aims to unify software development and operations. It is an approach to software development that emphasizes collaboration, communication, and integration between software developers and IT operations. DevOps has come a long way since its in
7 min read
Continuous Integration (CI) & Continuous Deployment (CD)
Infrastructure as Code (IaC)
Microsoft Teams vs Slack
Both Microsoft Teams and Slack are the communication channels used by organizations to communicate with their employees. Microsoft Teams was developed in 2017 whereas Slack was created in 2013. Microsoft Teams is mainly used in large organizations and is integrated with Office 365 enhancing the feat
4 min read