For system, network and cloud administrators

What is Terraform?

Let’s talk about infrastructure. Basically, anything that relates to how we’re setting up and configuring (policies, users, VPCs, storage buckets, etc.) what we want for our technology stack is what it’s called infrastructure. And code written in Terraform is focused on infrastructure. Instead of creating virtual machines, networks and subnets, manually, the developers use Terraform to automatically manage and provision their infrastructure for their application, thus Terraform is frequently used for Infrastructure as a Code (IaaC) on multiple cloud providers (GCP, Azure, AWS, Digital Ocean, etc.) these days.

Writing Terraform code

The Terraform code looks a lot like JSON but it’s not. And once you notice how easy are the patterns Terraform is using behind, everything we’ll look a lot clearer.

Any project in Terraform usually starts by creating a file and it can look like this:

provider "aws" {
  region = "us-east-1"

In the above example, what is really happening is that the code tells Terraform which plugin to use and for which cloud platform/provider but also in what region across the world we’re creating our first resources.

Initializing your first Terraform project

Once the first code Terraform has been written, starting in, the project needs to be initialized and that’s why the terraform init command (which is usually given at the root of the project, in the same folder as file) is one of the first required steps of a successful project. This command basically assess that it’s safe to start creating infrastructure in that folder.

Simulating that the code can actually create the infrastructure of the project

Once terraform init has been successful, Terraform can simulate and provision for you the actual infrastructure declared in the .tf files by using the terraform plan command. And the keyword here (though a bit inaccurate) is simulate – because it will do everything with those resources declared in the Terraform code but it won’t actually create anything, no resources will be created. It’s just a good way to see if the code written is good before actually being executed.

The command is checking the state of the terraform.tfstate file which is a pretty important file because that file keeps information about what’s already created and what needs to be created.

Executing the Terraform code and creating the actual infrastructure of the project

If everything goes well after running terraform plan then you can start creating your infrastructure by actually applying your code by using the terraform apply command. This is the final step of creating infrastructure for your project; once executed, all your resources will be created.

Deleting the newly created infrastructure in Terraform

Now, let’s say you just finished creating your infrastructure. You ran the terraform apply command and everything was creating. But what if we need to delete all this infrastructure? That’s when the terraform destroy command comes in place. And what this will do, it will first simulate (just like terraform plan does when creating our infrastructure) the deletion/destruction of your infrastructure. Only if you’re satisfied with the output of the simulated infrastructure destruction, it will wait for your “yes” input and then move on actually deleting/destroying your infrastructure.

Leave a Reply

Your email address will not be published. Required fields are marked *