Terraform Overview
- Terraform is one of the most popular tools for Infrastructure as Code or IaC. It was developed by HashiCorp, the topmost company in the IaC space. It makes it possible for users to write out their cloud infrastructure in code that will be easy to manage with version control, distribution, and use again.
- Terraform aims at building cloud infrastructure by code instead of a GUI. In this way, teams can collaborate and track provisioned cloud resources. Consequently, users of Terraform write down their infrastructures as code that can be reviewed, tested, and validated before its deployment.
-
It uses a
declarative approach
, meaning that users tell Terraform what they want, not how to do it. This allows Terraform to abstract away the underlying complexities of cloud infrastructure to focus only on the desired outcome.
- One of the most significant benefits of Terraform is that it can function with any cloud service provider. Indeed, Terraform has the capability to work seamlessly across multiple cloud platforms like Amazon Web Services (AWS), Microsoft Azure, Google Cloud Platform (GCP), and many more.
Why must we use Terraform?
There are several advantages that Terraform offers, but companies use it for the following three major reasons:
- Version control
- Reusability
- State of the Cloud
Version control
Problem: Traditionally, our team would have to create dozens of resources manually in the cloud platform based on some specified needs. We soon discovered that this practice was not only a time waste but also brought about errors and unorganized resource creation. When and where they were no longer needed, we had to delete those, often proving to be a headache in terms of wasted team time.
Solution: We used Terraform; an amazing and very popular IaC tool to use the concept of code management to control infrastructures. An infrastructures that is controlled using Terraform with Git version control and a hosting server, for example, GitHub and Bitbucket, makes easy to have version control of infrastructures. This enables
- Track changes: We could track changes to our infrastructure code over time so it is easily seen who made changes and when.
- Revert to previous versions: We could revert to previous commits and apply those codes to the cloud, resulting in automatic deletion of resources by running just one command.
- Automate resource deletion: Version control allows the auto-deletion of resources which are no longer being used, thus eliminating the need for manual removal of the resource.
- Collaborate effectively: Version control makes it easier to collaborate since team members can collaborate on the infrastructure code and work together, sharing knowledge much more easily.
Reusability
Problem: Previously, creating the same cloud resource multiple times would require one to manually repeat the process on the respective cloud platform. This was an extremely time-consuming and redundant task, especially in corporate settings where we often work with the same cloud resource provisioning. For instance, suppose we wanted to create many identical virtual machines in AWS. We would have to do so one by one through doing nothing but typing on the keyboard.
Solution: Terraform also has a feature called Module that allows us to achieve reusability. We can write repeated code inside a module and then use that module whenever we need to apply that code again. You can think of modules like functions in a programming language. Just as you define a function once and call it multiple times in a program, you define a module once and use it multiple times in your Terraform configuration.
State of the Cloud
Problem: One of the biggest issues when working in a team in the cloud is that we are not aware of what has been provisioned in our infrastructure or what has been currently in use. It occurs when someone creates a resource without notice, thereby confusing people and leading to inefficiencies. For example, if a team member decides to deploy a new virtual machine in AWS without bringing it to others' notice, then this can result in
- Resource duplication: Multiple team members may proceed to create the same resource, thus resulting in wasted resources and costs.
- Resource conflicts: Multiple team members might try to use the same resource. As a result of this, conflicts and errors would be encountered.
- Security risks: Unmanaged resources are the major cause of the security risks, such as unpatched vulnerabilities or open ports.
Solution: This can easily be solved by having the team create cloud resources with the help of Terraform. Terraform provides a Statefile that every member of the team can see which resources are running currently in the cloud infrastructure. The State file is simply a JSON file that contains information regarding all the resources created by Terraform.
How does Terraform work?
Each cloud provider, like AWS, GCP, or Microsoft Azure, has its own APIs. The functionality to
create
, delete
, and update
resources within the environment of that cloud provider is exposed through these APIs. Therefore, in essence, it will use the APIs of the respective cloud provider to do the necessary operations regarding the management of the infrastructure.
One of the great advantages of Terraform is that it supports working with lots of different cloud services. To make this a reality, Terraform has offered plugins for each of the supported cloud providers, referred to as providers. These provider plugins contain any necessary code and logic to interact with the respective cloud APIs.
When you run Terraform to provision or update your infrastructure, appropriate provider plugins act as agents for you with the involved cloud APIs. In reality, the translation of your high-level configuration set up in Terraform to the low-level API calls that create, delete, or update some of the corresponding resources in the cloud is carried out by these provider plugins.
Related Pages
Feedback
Was this page helpful?