Introduction to Infrastructure as Code with Terraform

Inshiya Nalawala
Geek Culture
Published in
4 min readJun 24, 2021

--

Imagine working in some IT company, years ago, when the concepts of Virtual Machines or Cloud Computing did not exist. If your team was given the responsibility of provisioning the infrastructure required to launch your company's latest product, what do you think it would take to achieve that?

Well, it would not be easy. Your team would have to go through the entire hardware purchasing cycle and wait for days to accurately configure the server to match the company’s expectations. Also, the network configuration, storage configuration, etc., falls upon the mantle of your team.

Fortunately, today we have the power of Virtualization and Cloud computing at our disposal. We no more have to rack and stack servers or buy real estate to set up data centers.

With the rapid advancement in the IT sector, companies today are turning to the cloud for their infrastructure requirements. While this solves a lot of problems that were there in earlier times, the issue of configuration consistency became arose quickly.

And this is how Infrastructure as Code was born.

As the name suggests, Infrastructure as Code (IaC) allows developers to provision IT environments by simply writing code. The motivation behind doing so is to achieve automation, speed, and consistency. With huge teams working on large projects, manually managing infrastructure can be a task in itself and is prone to inconsistencies and discrepancies.

An IaC system allows us to treat network devices, storage devices, or computing devices as software and are provisioned by writing code in human-readable languages that make API calls to specified resource providers to fulfill the need for infrastructure.

Terraform is a good example of an IaC tool. Terraform uses declarative language to write code that is readily understood and effectively helps in provisioning and managing infrastructure’s lifecycle. It supports over 1,000 providers including cloud providers like AWS, Azure, GCP, orchestration tools like Kubernetes, and many others.

Providers are nothing but plugins that allow Terraform to provision and manage resources on that specific provider.

For example, if you were to work with AWS, and launch an EC2 instance there using Terraform, you may first want to load the provider AWS — including the information regarding access to your AWS account.

provider “aws” {region=”ap-south-1"profile = “default”}

The above few lines specify a provider and the ‘profile’ keyword tells what AWS profile to use to access the resources.

Note: If you are new to the cloud, it would prove helpful if you get a basic idea about cloud computing before reading further

The terraform registry helps you to easily and quickly define resources for various providers and it’s best to start learning and implementing use-cases using this documentation.

However, we shall talk more about provisioning resources and writing code in later blogs, below is the general syntax that is used to provision resources.

resource “<type_of_resource>” “<name_of_resouce>” {<resource_parameters>}

Terraform uses declarative language, so consistency is always maintained. Moreover, when a particular configuration file is executed using terraform, it compares the current state of the said environment with the requirements specified in the code file. A file with an extension .tfstate stores all the information about the current state of the infrastructure. If no change is detected, terraform automatically perceives that the infrastructure is up to date. This is how terraform displays idempotent nature and consistency.

Basic commands to get started:

terraform init — used to load and download the providers used in the terraform configuration files

terraform plan — used to check to see the changes that will occur on executing the configuration files.

terraform apply — used to run the code files.

Note: Every code file in terraform that we write will have a .tf extension.

Browse this URL to install Terraform in your system

To get started with some real examples, create an AWS account. Create a user using the IAM service, and keep note of the access_key & the secret_key.

If you haven’t already done that you can follow the below steps to do that, once you have installed terraform.

Configure AWS CLI

aws configure

You will be prompted to enter the region and the IAM credentials that you noted earlier. Enter these things and leave the last option to default. This will create a ‘default’ aws profile that we can use to authenticate with our account while communicating with AWS using Terraform.

To list the created profile,

aws configure list-profiles

Then, click here to download the basic terraform code in your workspace and run the following commands:

terraform init 
terraform plan

You will see an output similar to this.

terraform apply

Type ‘yes’ when prompted. This basic example launches an EC2 instance of type t2.micro in your AWS account. Check the AWS console to confirm this.

You will receive an output similar to this if everything goes well.

To undo the changes and delete the instance, run the following command

terraform destroy

I will be back again with another demonstration, soon.

Thank you.

--

--