Terraform Basics #1 - Getting Started

Last Edited: 7/1/2025

This blog post introduces the fundamental concepts needed to get started with Terraform.

DevOps

In the previous article in this DevOps series, we discussed how to utilize kubeadm and other tools to set up a production-ready multi-node cluster with high scalability, maintainability, and availability. While we can technically set up a cluster on our local servers, we often leverage more flexible remote servers and infrastructures provided by cloud providers like AWS and Azure via APIs. This eliminates the need for time-consuming and costly hardware investments and maintenance and allows us to fully take advantage of the scalability and availability of containerized applications. Therefore, in this short subseries, we will discuss Terraform, a tool for easily provisioning the required specifications of remote servers and networks, or infrastructure, across various cloud providers.

Why Terraform?

Cloud providers typically offer GUIs and CLIs for interacting with their APIs, but these are often complex and frustrating to navigate. They often don't allow for the quick setup of multiple resources at once, easy sharing of resource configurations, or guarantee that resources are configured as desired. Most importantly, they are cloud-specific and involve significant switching costs. Terraform addresses these problems as an Infrastructure as Code (IaC) tool by providing a cloud-agnostic programming language, HashiCorp Configuration Language (HCL), for interacting with Terraform and with the APIs of many cloud providers. This allows us to flexibly apply, organize, and share resource provisioning configurations with proper version control and to switch between cloud providers relatively easily.

Terraform Architecture

Terraform's HCL is also declarative, like Kubernetes, which is itself an example of an IaC tool for orchestration. This means users only need to define the desired state, and Terraform figures out the steps or API calls required to achieve that state, making those API calls behind the scenes. This almost gurantees that all resources are provisioned and configured as desired and ensures consistent resource configurations across teams. Terraform achieves this with Terraform Core, which processes user code and configurations to determine the necessary API calls, and with Terraform providers, which are the interfaces for Terraform Core to make API calls to the different cloud providers' APIs. Terraform was open-source until 2023, and the community developed many Terraform providers for various cloud providers, increasing its coverage and contributing to its popularity. Although it is no longer open-sourced, it and its open-source fork, OpenTofu, maintain their popularity as IaC tools for infrastructure provisioning.

Terraform Deployment Workflow

Terraform allows us to standardize the deployment workflow, which generally involves initialization, planning, and application. We first write code in HCL to declare the desired state and initialize Terraform to resolve dependencies. Then, we move to the planning phase, where Terraform Core plans (or analyzes) the steps to reach the desired state from the current state (the current state can be stored in a state file and stored locally or remotely on Terraform Cloud or other remote storage services like an S3 bucket) and checks the formatting and validity. Finally, we apply the code via Terraform providers to provision and potentially configure the specified resources. The following is a simple visualization of a standard deployment workflow with Terraform.

Terraform Workflow

To clean up development and testing environments or production environments that we want to terminate to prevent unnecessary payments on unused resources, we can simply use the destroy command to smoothly and easily terminate the infrastructure. Terraform workflows can be utilized with version control tools like Git/GitHub and integrated naturally with CI/CD workflows like GitHub Actions for effective management, collaboration, and automation. Terraform can apply some configurations to resources by running bash scripts on the servers, but other IaC tools are better suited for configurations like Ansible and cluster configuration and container orchestration like Kubernetes. Therefore, Terraform is often used in conjunction with other IaC tools like Ansible and Kubernetes for provisioning and configuration of various infrastructures.

Install Terraform

To get started with Terraform, you can download Terraform by following the instructions on the official tutorial accessible from here. After installation, verify that it is successfully installed with terraform -help. Depending on the cloud providers you are using, you will need accounts with appropriate privileges and all necessary dependencies installed. For example, if you are using AWS, you need to set up an AWS user with full access to the AWS resources you want to provision with Terraform, install the AWS CLI, and use aws configure in the repository where you manage Terraform code to configure the default AWS user and its credentials.

In this short Terraform series, we are primarily provisioning infrastructure with AWS resources like EC2, S3, DynamoDB, RDS, and so on, assuming some level of familiarity with them. Therefore, I recommend learning the basics if you are not familiar with them and setting up an AWS account and AWS CLI to follow along, although the articles mainly focus on Terraform itself, and they are not strict requirements. If you are interested in using other providers like Azure, Google Cloud, or even managed Kubernetes services like EKS and GKE, you can check out their official websites and their corresponding Terraform providers in the Terraform Registry accessible from here. (You can even build your own Terraform providers with the Terraform Plugin SDK, though that is outside the scope of this article series.)

Conclusion

In this article, we covered the basics of what Terraform is, why we might want to use Terraform, and how Terraform works. We also briefly discussed the installation requirements of Terraform and how that differ depending on the cloud providers of choice. From the next article, we will begin discussing how to write Terraform code and provision basic infrastructure with AWS, details of some important and useful Terraform features, and their best practices. (Note: The contents and structure of this Terraform series are largely inspired by the tutorial course by DevOps Directive (2022). I recommend checking it out from the link cited below if you are interested.)

Resources