Terraform

Terraform

Here we are to learn terraform, before goin in detail about terraform first let us know how terraform is started.

what is terraform?

Terraform is an open-source infrastructure-as-code software tool created by HashiCorp. Users define and provide data center infrastructure using a declarative configuration language known as HashiCorp Configuration Language (HCL).

Terraform can work with 200 plus providers and with all major cloud providers :AWS, Microsoft Azure, Google cloud, oracle cloud.

Now let's start to create infrastructure in Azure using terraform, to use terraform first we have to download. you can download in all operating systems Linux, windows and macOS, After installing you have to set environment variables if you are downloading in your local system. Terraform is a third party tool and it is not a part of any cloud's so we have to first connect terraform to our respective cloud as shown in below image.

Here click on Browse providers

Here you can see multiple cloud displayed here that you can work on

In my case im working on Azure, so ill use the Azure cloud provider to connect to terraform.

you can see here latest version is 3.43.0 which is updated 3 days ago here HAshicorp develops the code according to the requirement to the Microsoft Azure, we have to use provider block to connect to Azure.

Now click on "USE PROVIDER" as shown in the above image

terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "3.43.0"
    }
  }
}

This is like a plugin that we use to connect to Azure-cloud, for every cloud-provider has their own respective provider-block.

Now open any of your local terminal like git-bash or visual studio code, im using Git-bash

create one directory as learn-terraform ( any name)

create a new file as extension .tf, we should use .tf as an extension for terraform

Here in this file, you can see first is the plugin i.e provider block which used to connect to Azure cloud and next one is the Resource block using this resource block we can create any resources in azure, in this I'm creating resource group and v-net using the above code.

now save file

we have basic terraform commands those are:

  1. Terraform init:

The terraform init command initializes a working directory containing Terraform configuration files. This is the first command that should be run after writing a new Terraform configuration or cloning an existing one from version control. It is safe to run this command multiple times.

  1. Terraform plan:

The terraform plan command is used to create an execution plan. Terraform performs a refresh, unless explicitly disabled, and then determines what actions are necessary to achieve the desired state specified in the configuration files.

  1. Terraform apply:

Terraform apply command is used to create or introduce changes to real infrastructure. By default, apply scans the current working directory for the configuration and applies the changes appropriately. However, you’ll optionally give the path to a saved plan file that was previously created with terraform plan.

First, you have to initialize a working directory containing terraform configuration files

now #terraform plan, after applying this command it will show you the plan what are all going to execute .

here in this plan, we can see it displayed what are the services which are going to create

Now # terraform apply

now it will ask for the confirmation to create resources as you can see * Do you want to perform these actions :

Enter a value : yes

Now resource group and v-net is created by terraform

if you want to delete all the resources which is created the command is # terraform destroy

Now we are going to refere terraform official Documentation to create resource in Azure-cloud https://registry.terraform.io/

as we seen in last excamples of creating resources using terraform, terraform is a third party tool which is not part of any cloud, Hashicorp is providing all infrastructure as a code services for almost 2000 + providers

so if you want to connect terraform to any cloud provisers you nedd to authenticate using provider block, first you have to set provider block as shown in below search for providers in documentation

Herewe different types of Authentication for different use cases, but im refering for Azure-cli

Now open visual studio code to configure terraform to Azure cloud or you can use Git-Bash also.

After this click on extensions, there you have to download terraform and azure cli to authenticate

search for terraform

after installing terraform, same way install azure-cli also

Now all set to write a terraform code

First create a file with the name terraform-main.tf ( you can give any name but the extension should be .tf)

Now go to the official documentation ad take and refer the terraform code to create resources.

open the new terminal in visual studio code

I'm referring official documentation to authenticate to azure-cloud.

first you need to login as shown in doc # az login

now authenticate by using provider block

You should provide a subscription id and tenant id to authenticate to Azure-cloud, but it's not a good practice by providing a subscription-id & tenant-id because it already previously we discussed terraform is third-party tool. for this we have concept in terraform called Data-source.

Now i want to create Resource-group, virtual network, subnet, network interface and virtual machine, for this im referring virtual machine in Documentation.

https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/linux_virtual_machine

To create Resource-group and virtual-network & subnet

To create network-interface

To create virtual-machine

ubuntu-image

Now save this file, click on file and save

Go to terminal and apply this command # terraform init

Now # terraform plan

Now # terraform apply

creating all the resources

now all the resources are created, now go to azure portal and check

Now destroy all the resources which are created using # terraform destroy

all the resource are destroyed