Day 1: Introduction to Terraform and Terraform Basics

Day 1: Introduction to Terraform and Terraform Basics

#Day-1 of TerraWeek Challenge

  • Introduction of Terraform

    Terraform is an open-source, infrastructure-as-code software (IaC) tool, that maintains the infrastructure change history using VCS like Git, created by HashiCorp and written in the Go programming language.

    Terraform can manage both existing service providers and custom in-house solutions.

  • How can it help you manage infrastructure as code?

    Infrastructure as code is the process of managing infrastructure in a file or files, rather than manually configuring resources in a user interface (UI).

    Here resources are nothing but virtual machines, Elastic IP, Security Groups, and Network Interfaces... Terraform code is written in the HashiCorp Configuration Language (HCL) in files with the extension. tf

    Terraform allows users to use HashiCorp Configuration Language (HCL) to create the files containing definitions of their desired resources on almost any provider (AWS, GCP, Azure, Digital Ocean, OpenStack, etc) and automates the creation of those resources at the time of application.

  • Why do we need Terraform?

Terraform is used to create, manage, and update infrastructure resources such as physical machines, VMs, network switches, containers, and more.

Terraform can manage both existing service providers and custom in-house solutions.

With the help of Terraform, we can create our infrastructure anywhere on VM, AWS, GCP, Openstack Digital Ocean etc.,

Let's understand why we need to terraform with comparison.

Difference between Terraform and Cloud Formation

Terraform

Cloud Formation

Terraform is developed by HashiCorp

Cloud Formation is Developed by AWS

It will work for many Cloud providers like AWS, Azure, GCP, Digital Ocean…

Cloud Formation will support only AWS

Terraform uses HashiCorp Configuration Language (HCL), a language built by HashiCorp. It is fully compatible with JSON.

AWS Cloud Formation utilizes either JSON or YAML.

Difference between Terraform and Ansible

Terraform

Ansible

Terraform is an Open-Source Tool which is provided by HashiCorp.

Ansible is also an Open-Source Tool.

Terraform is an Infrastructure as a Code, which means they are designed to provision the servers themselves.

Ansible is a Configuration Management Tool. Which means they are designed to install and manage software on existing servers.

Terraform is ideal for creating, managing, and improving infrastructure.

Ansible is ideal for software provisioning, application deployment, and configuration management.

  • How does it simplify infrastructure provisioning?

    The workflows of Terraform are built on top of five key steps: Write, Init, Plan, Apply, and Destroy. Nevertheless, their details and actions vary between workflows.

    Write – this is where you create changes to the code.

    Init – this is where you initialize your code to download the requirements mentioned in your code

    • Terraform init :

      This command is used to initialize the working directory containing Terraform configuration files.

      The init command for:

      1. Plugin Installation

      2. Child Module Installation

      3. Backend Initialization

    • Terraform plan :

      The terraform plan command creates an execution plan. By default, creating a plan consists of:

      1. Reading the current state of any already-existing remote objects to make sure that the Terraform state is up-to-date.

      2. Comparing the current configuration to the prior state and noting any differences.

      3. Proposing a set of change actions that should, if applied, make the remote objects match the configuration.

    • Terraform Apply :

      The Terraform apply command executes the actions proposed in a Terraform plan.

  • How can you install Terraform and set up the environment for AWS, Azure, or GCP?

    We will install Terraform on AWS Linux.

    Log in to the AWS console Select ubuntu os and launch the instance.

    Once the instance is launched check the state of an instance whether it is in running or not. Once it is running connect with it Mobaxterm or Putty software or you can directly connect it from the AWS console.

Once you log in to the console first of all log in as a root user and update the system.

type the below commands to update the system :

sudo su -
apt-get update -y

Now refer below link to install Terraform on your machine.

https://developer.hashicorp.com/terraform/downloads

Run the below commands one by one.

wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform

Now check whether the terraform is installed or not by typing terraform -v command.

-v means version of terraform.

Finally terraform is installed on our machine. Congratulations!!!!!

Now we will move towards configuration.

Create a directory for terraform project. mkdir terraformproj

open the directory in visual studio core and start writing files for the project with the .tf extension.

output  hello {
value ="Welcome to Terraform"
}

run the command terraform plan in the console.

  • Explain the important terminologies of Terraform with the example of at least (5 crucial terminologies).

    Let's discuss terminologies in terraform:

    1. Provider :

      The provider block configures the specified provider. A provider is a plugin that Terraform uses to create and manage resources.

    2. Resource :

      The resource block is used to define components of your infrastructure. A resource might be a physical or virtual component.

    3. Variables:

      It stores the declaration for the variables which are referenced in the main. tf

      For our variable, we just have a really simple configuration in a file called variables. tf.

      variable "availability_zone" {

      type = string

      default = "us-east-1a"

      }

    4. Terraform state :

      The state is useful to keep a record of why and how infrastructure was created in the first place.

    5. Module :

      A module is a container for multiple resources that are used together. Every Terraform configuration has at least one module, known as its root module, which consists of the resources defined in the .tf files in the main working directory. To call a module means to include the contents of that module into the configuration with specific values for its input variables. Modules are called from within other modules using module blocks.

      module "servers" {

      source = "./app-cluster"

      }

Thank you for reading this blog.Happy Learning!!!!!

You can follow me on LinkedIn for my daily updates:- linkedin.com/in/parimal-pradhan-b62021168

Great initiative by the #TerraWeek #trainwithshubham community. Thank you Shubham Londhe

Did you find this article valuable?

Support Parimal Pradhan by becoming a sponsor. Any amount is appreciated!