Getting started with Terraform

Picture credit: https://learn.hashicorp.com/tutorials/terraform/infrastructure-as-code

Lab Environment

Terraform will create resources in AWS. We will try to remain inside the Free-tier throughout the course, but some of the resources do not lie on the AWS Free tier so it might cost you. Do not forget to destroy the resources immediately after you are done. I will be creating an EC2 instance and run terraform from there. You can do the same as well or install it on your machine as well.

Prerequisite

  • Terraform uses the CLI client and that requires some familiarity with one of the command line tools.
  • Terraform creates resources on AWS itself, hence an AWS account is required to follow through the tutorial.
  • This tutorial requires enough experience of building resources on AWS manually.
  • In the later part of the series, we are going to build VPCs and use Subnets. So, you are expected to have a good understanding of how networking is done inside an AWS environment.
  • Using remote repositories is one of the most fascinating and crucial parts of IaC. So, you should have a Github account and are expected to know how git works and basic git commands.

Installing Terraform

Now that we have all the prerequisites explained, let’s start the tutorial by installing Terraform.

Install Terraform on Ubuntu

I am going to install Terraform on my own machine. You can create an EC2 instance and install Terraform there as well.

To verify the installation

Create infrastructure using Terraform

To create resources with Terraform, the least requirement is to specify the resource and cloud provider. Let’s separate them into two different files; provider.tf and main.tf.

terraform apply -var-file=”<file-path>”

Initialize Terraform

For Terraform to understand the configuration, it must be first initialized inside the directory that contains our code. Once terraform is initialized it can look into our configuration and create plan to build our resources accordingly. The steps to create any infrastructure with Terraform are; terraform init, terraform plan and terraform apply in that order.

Plan Terraform

terraform plan does a dry run with the provided instructions and shows the overview of the resources that are going to be built with the provided configuration. If there are errors while in the code somewhere, it shows errors and doesn’t complete the plan.

Apply terraform changes

If the output of the dry run is what we need, we can now go ahead and build our resources using terraform apply. If you log into the AWS console, you should be able to see the EC2 instance we just created.

Terraform state files

If you look inside your directory Terraform, you should be able to see a new file being created, named terraform.tfstate. This file is the terraform state file, which is in the json format that contains the information about all the resources that terraform created.

Destroying the resource

Now we have understood how the state files work in terraform, we can now go ahead to understand how “destroying” terraform works. When “terraform destroy” is hit, terraform looks into its state file and destroys all the resources that exist in the terraform state file.

Pushing to code to the Github repository

As discussed earlier as well, one of the most fascinating features of IaC is that it can be treated as code. So, let’s put that into practice and create a remote repository to store our code. We are using Github but any other remote repository is just as fine.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Suman Dhakal

Suman Dhakal

AWS certified Solutions Architect — Associate