Introduction to Terraform and Terraform Basics

Introduction to Terraform and Terraform Basics

#TerraWeek Challenge by TWS Community Builders

What is Terraform?

Terraform can be defined as a tool for versioning, changing, and building infrastructure efficiently and safely. It can manage popular and existing service providers and custom in-house solutions also.

Terraform can provide support with multi-cloud having a single workflow for every cloud. Various manages of terraform infrastructure could be hosted over Google Cloud Platform, Microsoft Azure, and Amazon Web Services, or on-prem within the private clouds like CloudStack, OpenStack, or VMWare vSphere.

Terraform considers IaC (Infrastructure as Code). So, we need not be worried about our infrastructure drifting away through the desired configuration.

How can Terraform help you manage infrastructure as code?

Terraform is HashiCorp's infrastructure as a code tool. It lets you define resources and infrastructure in human-readable, declarative configuration files and manages your infrastructure's lifecycle. Using Terraform has several advantages over manually managing your infrastructure:

  • Terraform can manage infrastructure on multiple cloud platforms.

  • The human-readable configuration language helps you write infrastructure code quickly.

  • Terraform's state allows you to track resource changes throughout your deployments.

  • You can commit your configurations to version control to safely collaborate on infrastructure.

    Manage any infrastructure:

    Terraform plugins called providers let Terraform interact with cloud platforms and other services via their application programming interfaces (APIs). HashiCorp and the Terraform community have written over 1,000 providers to manage resources on Amazon Web Services (AWS), Azure, Google Cloud Platform (GCP), Kubernetes, Helm, GitHub, Splunk, and DataDog, just to name a few. Find providers for many of the platforms and services you already use in the Terraform registry. If you don't find the provider you're looking for, you can write your own.

IaC(Infrastucture as Code):

Reproducible environments: The similar environment can be built again and again by applying code for generating infrastructure.

Convergence and Idempotence:

Easing collaboration: If we have code within the version control system such as Git, it will permit teams for collaborating on infrastructure.

Self-service infrastructure: Various developers can plan any infrastructure they require when they require it

UseCases of Terraform:

What is Terraform

  • How can you install Terraform:

    To start with Terraform, take the following steps:

  • Downloading and Setting up Terraform The necessary binaries for your operating system can be downloaded by visiting the Download Page. Extract the binary to one of the PATH directories on your system.

  • To verify Terraform installation: To make sure Terraform is installed properly, launch a terminal or command prompt and type Terraform -v. The version number should be visible.

  • To start your Terraform project, create a new directory: To store your Terraform configuration files, create a new directory. Enter this directory's path at the command prompt or terminal.

  • Create a Terraform configuration file: In your project directory, create a new file called main. tf. This file will contain your Terraform configuration.

  • In main.tf file add the below HCL commands to create a file with automation.

To begin using Terraform, initiate your project by running the command "terraform init" in your terminal or command prompt. This action will trigger the initialization process, during which Terraform will automatically fetch the required provider plugins and configure the backend for storing your project's state file.

To deploy your infrastructure, execute the command "terraform apply" to apply your Terraform configuration.

After Terraform has completed the process of applying your configuration, you can verify the successful deployment of your infrastructure by checking the console of your cloud provider.

Important Terminologies in Terraform:

Providers:

A provider is a plugin that lets Terraform manage an external API. When we run terraform init, plugins required for the provider are automatically downloaded and saved locally to a .terraform directory. Terraform supports multiple providers. Depending on what type of infrastructure we want to launch, we have to use appropriate providers accordingly.

Resource :

Resources are the most important element in the Terraform language. Each resource block describes one or more infrastructure objects, such as virtual networks, compute instances, or higher-level components such as DNS records.

Resource syntax:

resource "aws_instance" "web" {

ami = "ami-a1b2c3d4"

instance_type = "t2.micro"

}

Variables :

Variables in Terraform can be assigned values in multiple ways. Some of these include: i)Environment variables ii)Command Line Flags iii)From a File iv)Variable Defaults.

Thanks for Reading!!!!!