Introduction to Terraform and Terraform Basics

Introduction to Terraform and Terraform Basics

Here we are with the new challenge "Terraweek 7 days challenge" to learn Terraform which helps in provisioning the infrastructure.

Over the next 7 days, we'll go on a journey together to learn about Terraform from basics to advanced. Get set for an adventure into Terraform! In our upcoming blog series, we're starting a deep dive into this amazing tool that helps manage computer stuff.

💥Introduction

In the ever-evolving landscape of IT infrastructure management, Infrastructure as Code (IaC) has emerged as a game-changing approach.

It allows organizations to manage and provision infrastructure resources using machine-readable configuration files or scripts, eliminating the need for manual processes and configurations.

This blog will delve into the core concepts of IaC, and more specifically, we will explore why Terraform is the preferred choice for infrastructure management.

Follow along, and you'll learn not only the 'what' and 'why' but also the 'how' of setting up Terraform and creating your first configuration.

📚Comprehending Infrastructure as Code (IaC)

Infrastructure as Code (IaC) represents a profound transformation in our approach to infrastructure management.

Instead of the conventional manual setup of servers, networks, and related infrastructure components, IaC empowers us to articulate our desired infrastructure state using code.

This code can subsequently be executed to automate the provisioning and management of our infrastructure.

IaC serves as a fundamental cornerstone within the realms of both DevOps and cloud computing, with a primary focus on efficiency, reliability, and scalability.

🤔Why Terraform?

  1. Multi-Cloud and Multi-Provider Support: Terraform supports a wide range of cloud providers, including AWS, Azure, GCP, and more. Additionally, it extends its support to various infrastructure platforms like VMware, Docker, and Kubernetes. This versatility means you can manage resources across different providers and platforms using a single tool.

  2. Declarative Syntax: Terraform employs HashiCorp Configuration Language (HCL) or JSON, offering a human-readable format to define your infrastructure. You describe the desired state of your resources, and Terraform handles the provisioning and management details.

  3. Community and Ecosystem: Terraform boasts a thriving community that provides a wealth of modules, plugins, and best practices. This community support accelerates development, assists in issue resolution, and encourages knowledge sharing.

  4. Collaboration and Version Control: Infrastructure code written in Terraform integrates seamlessly with version control systems. This facilitates collaboration, enables robust code reviews, and provides the ability to roll back changes if necessary.

Why do we need Terraform and how does it simplify infrastructure provisioning?

Earlier managing the on-premise infrastructure was a very complicated task. The time taken to launch the instance was too long (slow deployment) and it was a very expensive operation because for managing every task we had to have different groups(like Field Engineers, System/NW administrators etc..) inside Infrastructure Team as you can see in below ScreenShot. we had limited automation and there was a chance of human error too, because of that we started using Terraform (Infrastructure as a code) and now just one DevOps engineer can automate the entire task of infrastructure provisioning.

  • 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.

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

following are commands to install Terraform for Linux.

sudo apt-get update && sudo apt-get install -y gnupg software-properties-common

wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg

gpg --no-default-keyring \ --keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg \ --fingerprint

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

Create Day 01 Simple Project

So on Day 01 , let's start with a simple project

  1. To start your Terraform project, create a new directory

  2. To store your Terraform configuration files, create a new directory.

  3. Enter this directory's path at the command prompt or terminal.

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

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

 resource "local_file" "devops_terraform" {
         filename = "/home/ubuntu/first_terraform_project/config"
         content = "It's Day-1 TerraWeek challenge"
 }

  1. The code snippet creates a resource of type "local_file" with the name "devops_terraform". Here's the breakdown of the code:

    • resource: This keyword is used to define a resource in Terraform.

    • "local_file": It specifies the resource type, which in this case is a local file resource.

    • "devops_terraform": It provides a name or identifier for the resource instance, allowing it to be referenced elsewhere in the configuration.

    • filename: It specifies the path and name of the file to be created. In this case, it is set to "/root/terraform/terraform-local/automate-file.txt".

    • content: It defines the content that will be written to the file. Here, it is set to "It's Day-1 Terraweek challenge".

  2. Initialization: Start your Terraform project with terraform init. This command sets up your environment by fetching necessary provider plugins and configuring the backend for state storage. It ensures your Terraform environment is ready for infrastructure management

terraform init

Deployment: Deploy your infrastructure using terraform apply. Terraform will ask for confirmation; type "yes" and hit Enter to proceed with deployment.

 terraform apply

  1. Verification: After Terraform finishes applying your configuration, verify the successful deployment in your cloud provider's console.

  2. To Check for your newly provisioned infrastructure components to confirm everything is as expected, check whether the file has created or not

So, we have completed our day 01 of the terraWeek Challenge by creating simple project.

In the next blog post, we will be learning about HCL language, syntax, and writing Terraform configurations using HCL syntax.

Thanks for reading😊...!

Happy Learning😄