Infrastructure as Code with Terraform — Terraform 101

Infrastructure as code with terraform for beginners by using the Azure.

Yohan Malshika
7 min readMay 31, 2021

Hello All, Hope you all are doing well! Finally, I could manage the time to write the new blog article after few months. Meanwhile, I could learn few new technology concepts with my university academics works and as well as my studies. So, I got the chance to learn about Terraform and then tried to terraform with Microsoft azure. So, I’ll plan to tell you about terraform and also show you a simple demo for terraform with azure.

Infrastructure as Code with Terraform — Terraform 101

1. What is infrastructure as code(IaC)?

Basically, Infrastructure as Code (IaC) refers to the increasingly common practice of using the code to provision and manage IT infrastructure. Its means, manage your IT infrastructure using configuration files. For example, You’re treating your servers, databases, networks, and other infrastructure as if they were software. This code can also assist you in rapidly and consistently configuring and deploying various infrastructure components.

IaC enables you to automate infrastructure deployment in a continuous and consistent manner, which offers several advantages.

2. Benefits of IaC

Everything Codified

The main advantage of IaC is that it allows for explicit coding of configuration files in use. You may share codes with the team, test them for correctness, maintain consistency, and integrate the infrastructure into the same IaC flow.

Speed

IaC enhances a company’s IT architecture and workflow since it leverages automation to significantly speed up infrastructure development, testing, and production.

Cost

Without any doubt, one of the most significant advantages of IaC is the reduction of infrastructure management costs. Engineers save time and money by having everything automated and organized and this time and money may be better spent on other manual duties and higher-value occupations.

Version Controlling

We could check-in the infrastructure configurations into version control systems like GitHub and begin versioning them now that they are documented.

Now we have an idea about IaC. Let’s move to Terraform!

3. So, What is this Terraform?

Terraform is an open-source tool that aids in the automation of cloud infrastructure. It lets us to create the infrastructure as code, using a high-level configuration language called HCL.

4. Workflow of Terraform

The basic workflow of Terraform

Terraform Init

After writing the code, we need to initialize the code. This terraform Init command is using to initialize a working directory that containing the terraform configuration files. Before Terraform can use the provider, it must be initialized. And also, the provider’s plugin is downloaded and installed during initialization so that it may be used afterwards.

Terraform plan

This terraform plan command is using to create an execution plan. This command is a quick and handy way to see if the execution plan for a set of changes meets our expectations without affecting real resources or the state.

Terraform Apply

This terraform apply command is using to create or introduce the changes into real infrastructure. This command is scanning for configuration changes in the current working directory and makes the necessary adjustments by default. Also, this will write data to the terraform.tfstate file. Then, when the application is completed, the resources that we applied are immediately available.

Terraform Destroy

When we need to destroy the managed infrastructure, we could use terraform destroy command.

5. Simple Demo: Creating Azure SQL database

This is a simple demo that I’ll show you how to create Azure SQL database by using the terraform.

Prerequisite

  1. Install the terraform
  2. Install the Azure CLI
  3. Azure subscription
  4. Install Terraform Extension for VS code (I’ll use VS Code as IDE for this demo)

So you guys also can start with me! Let’s Start!!!

  1. First, create a folder and then create the main.tf file as the main file inside that folder. The file’s very first component includes details about the provider we’ll be using in the setup. Azure, AWS, or any other accessible provider can be used. Declare the providers with a specified version so that our setup doesn’t break when the service we’re using gets a new version.

2. When we need to create any resource in Azure, First we have to create a resource group inside the subscription. For that, I add the resource group as a resource in the existing configuration code. and we have to pass in two required variables for name and location for our resource group.

3. Then need to create Azure SQL Server for SQL database. for that add the azure SQL server resource block into the configuration file. In that case, we have to add few configurations as variables that we could see in the below code.

4. Then we have to create a storage account for creating an azure SQL database. In that case, we have to add a storage account resource block with configurations that it needs into the configuration file.

5. Then we could create an Azure SQL database. For that, we have to add a SQL database resource block with configurations that need to provide the details in the configuration file.

6. We use variables for resource name, location and many other that you could see in the above code. so our next step is to create a file called variables.tf and this file where we will define what input variables we need for our build such as resource-groupname, location, and so on to make sure we don't hardcode anything inside the main terraform file. So, You have already seen where these variables are used in the main file above.

variable "variable_name" {}

So now, you could see the above configuration code for variables. type is set a type of variables such as string, list, and map. then the default is set a default value of the variable.

7. Now we are done with the code. Now we have to validate and deploy the recourse.

For that, first, we need to log in to Azure by using the az login command. for that, we need to install the Azure CLI module as I told early.

az login

Use the terminal in the VS Code and type the following command and Login to your Azure Account.

After that, run the terraform init command to initialize the provides in our terraform code. when run this command, it will create the local directory called .terrafrom for the terraform providers.

Terraform init command

Then, The following step is to plan and see what will be deployed in Azure once the terraform code is executed. To discover what resources will be deployed in your Azure Subscription, use the command terraform plan. In this stage, Terraform is going to plan the configuration against our Azure environment and not going to deploy anything into the azure environment.

Terraform plan command

Now, the last step to perform is to apply the changes and wait for the terraform to build the infrastructure defined in our main file. Run the terraform apply command and wait for it to complete the deployment and ensure to enter “yes” once prompted to confirm, in the terminal session.

Terraform apply command

After this process, you could see the resources from the Azure portal.

Resources in the Azure Portal

When the deployment is completed, we could use the resources for our tasks. when we need to clean up the environment, we could use terraform destroy command clean up the environment.

Terraform destroy command

I hope you guys learned the basics of Terraform and how you use terraform with azure to provisioned IaC project. As a developer, I think the Infrastructure as code concept is something good move that needs to care about.

See you again soon with another article!

Happy coding!! Happy Terraforming!!👽👽

--

--