Skip to main content

Infrastructure as Code (IaC) is a process of managing and provisioning IT infrastructure through machine-readable definition files. It means writing code to define your infrastructure, in the same manner, you would write code to define your application software. Nowadays, all modern IT companies use IaC tools to build and manage their cloud computing infrastructure.

Traditionally, IaC was accomplished by using configuration management tools like Chef, Puppet and Ansible. Configuration management tools like these provide great customization and focus more on application delivery rather than infrastructure provisioning.

What is Terraform?

Terraform is an infrastructure provisioning tool. Being a provisioning tool means that Terraform can deploy your entire infrastructure stack, not just new versions of your application (what configuration management tools actually do). By writing configuration files, Terraform can deploy just about any cloud or combination of clouds, including private, public, hybrid, and multi-cloud.

Benefits of using Terraform

Terraform isn’t the only Infrastructure as Code technology. There are plenty of other tools that do the same thing. Moreover, Terraform was created and initially released in 2014 by a relatively small tech company called HashiCorp, and doesn’t even have a 1.0 version out yet. So, how does Terraform compete on the market? The answer is, of course, that Terraform provides a unique set of advantages. These competitive advantages stem from six key characteristics:

  • Provisioning tool: Deploy infrastructure, not just applications
  • Easy to use: For all Software Engineers, not just DevOps
  • Free and Open Source: Who doesn’t like free?
  • Declarative: Say what you want, not how to do it
  • Cloud agnostic: Deploy to any cloud
  • Extendable: You aren’t limited by the language

The very beginning

After seeing what Terraform is and the advantages of using it, let’s see how simple it is to start using it.

Terraform code is written in HCL (Harshicorp Configuration Language) with “.tf” files extension where your goal is to describe the infrastructure you want.

There are numerous providers supported by Terraform including the most popular: AWS, Google Cloud, Azure, Heroku and many more.

A typical provider configuration would look something like:

provider "aws" {
      region = "eu-central-1"

This means that you use aws as a cloud computing platform, and eu-central-1 define the region that will host your resources.

The main purpose of the Terraform language is declaring resources, which represent infrastructure objects. Each provider has different types of resources related to their real infrastructure objects. This is the most simple representation of AWS EC2 instance (a resource with required attributes only) by Terraform:

resource "aws_instance" "example" {
       ami = "ami-d74be5b8"
       instance_type = "t2.micro"

If you gather these two code blocks above in one file, you already have one small Terraform project that is ready to launch a single EC2 instance from your local machine. You only need to have the following prerequisites:

  • Terraform installed
  • An AWS account
  • An AWS CLI installed

After you provide the prerequisites, you should configure AWS locally by executing the following command:

$ aws configure

Follow the prompts to input your AWS Access Key ID and Secret Access Key, which you will find on this page.

Now you are ready to execute your first terraform command. First, navigate to your directory where you created your file and initialize the directory with:

$ terraform init

By initializing, Terraform downloads the AWS provider and installs it in a hidden subdirectory of the current working directory. The output shows which version of the plugin was installed.

After the successful initialization you can see the set of instructions that terraform prepared to execute for you:

$ terraform plan

To apply the plan and release it:

$ terraform apply

Now you have your EC2 instance up and running. You can see your instance here.

If you go inside the newly created instance, you can see that Public IPv4 addresses and Private IPv4 addresses are already created for you. These IPs will help you communicate inside and outside the cloud world. You can also see it is Red Hat instance by default and a lot of other details about the instance. You can customize all these details by adding proper arguments in your aws_instance.

At the end of your work, you are able to destroy your instance or whole infrastructure by executing one command:

$ terraform destroy


Even though I explained Terraform in a very simple way, it can become very complex by going further and try to build more complex cloud infrastructure. I recommend you go through the documentation before you try to develop anything. When you learn how to build your infrastructure on one cloud platform, it is going to be much easier to make it happen on other platforms as well.