"Terraform for Beginners: A Comprehensive Guide to Infrastructure as Code"

Lalita KashyapLalita Kashyap
2 min read

Terraform is an open-source infrastructure as code (IaC) tool developed by HashiCorp. It allows you to define and provision data center infrastructure using a high-level configuration language. Terraform is used to manage cloud services, such as AWS, Azure, Google Cloud Platform, and more.

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

Syntax of Terraform:

<block>< parameters>{

arguments

}

Follow below steps to Install terraform and perform task:

Step 1: Launch an EC2 Instance

https://lalitakashyapblog.hashnode.dev/step-by-step-guide-to-setting-up-an-ec2-instance

Step 2: Ensure that your system is up to date and you have installed the gnupg, software-properties-common, and curl packages installed. You will use these packages to verify HashiCorp's GPG signature and install HashiCorp's Debian package repository.

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

Step 3: Install the HashiCorp GPG key

wget -O- https://apt.releases.hashicorp.com/gpg | \
gpg --dearmor | \
sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg > /dev/null

Step 4: Verify the key's fingerprint

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

Step 5: Add the official HashiCorp repository to your system. The lsb_release -cs command finds the distribution release codename for your current system, such as buster, groovy, or sid.

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

Step 6: Download the package information from HashiCorp.

sudo apt update

Step 7: Install Terraform from the new repository.

sudo apt-get install terraform

Step 8: Check terraform version

terraform --version

Step 9: Create project directory and move into it

mkdir terraform
cd terraform/

Step 10: Create file "vim main.tf"

#local_file = resource type
#my_file = resource name
#filename & content are arguments
resource "local_file" "my_file"{
        filename = "tws.txt"
        content = "This file is created by terraform"
}

Step 11: Initialize terraform where your created file saved

terraform init

Step 12: Now terraform plan

terraform plan

Step 13: Terraform apply

terraform apply

Congratulations! You've taken your first steps into the world of Terraform and Infrastructure as Code (IaC). By now, you should have a good understanding of the basic concepts of Terraform, how to set up your environment, and how to create and manage infrastructure using simple configuration files.

Thank you ๐Ÿ™

Keep Learning..

0
Subscribe to my newsletter

Read articles from Lalita Kashyap directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Lalita Kashyap
Lalita Kashyap