Skip to main content

Command Palette

Search for a command to run...

๐Ÿš€ Getting Started with Terraform ๐Ÿš€

Updated
โ€ข2 min read
๐Ÿš€ Getting Started with Terraform ๐Ÿš€

Terraform is an amazing Infrastructure as Code (IAC) tool that lets you define and provision your infrastructure using code.

  1. ๐Ÿ› ๏ธ Installation ๐Ÿ› ๏ธ

    For window installation: Install Terraform from the official link download

    For Linux installation: go to the official link to download or run this command

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
  1. ๐Ÿ—๏ธ Creating Your First Terraform Configuration ๐Ÿ—๏ธ

    Create a directory for your Terraform project. Let's call it my-terraform-project.

    Inside this directory, create a new file called main.tf. This is where you'll define your infrastructure.

๐ŸŒ Defining Resources ๐ŸŒ

   provider "aws" {
     region = "us-west-2"  # Set your desired AWS region
   }

   resource "aws_instance" "example" {
     ami           = "ami-03f65b8614a860c29"  # Specify an appropriate AMI ID
     instance_type = "t2.micro"
     subnet_id = "subnet-0c5ca548286b6c0d0"
     tags = {
       Name = "Demo_ec2"
     }
   }
  1. Provider: A provider is a plugin for Terraform that defines and manages resources for a specific cloud or infrastructure platform. Examples of providers include AWS, Azure, Google Cloud, and many others. You configure providers in your Terraform code to interact with the desired infrastructure platform.

  2. Resource: A resource is a specific infrastructure component that you want to create and manage using Terraform. Resources can include virtual machines, databases, storage buckets, network components, and more. Each resource has a type and configuration parameters that you define in your Terraform code.

Setup Terraform for AWS

Go to your terminal and type

aws configure

After that, you have to provide AWS_ACCESS_KEY_ID , AWS_SECRET_ACCESS_KEY, Default region name, Default output format

Initialize Terraform

terraform init

This command initializes the Terraform working directory, downloading any necessary provider plugins.

Plan the Configuration

terraform plan

This command is used for dry runs and will show you what is going to be created.

Apply the Configuration

terraform apply

Terraform will display a plan of the changes it's going to make. Review the plan and type "yes" when prompted to apply it.

Destroy Resources

terraform destroy

This command will remove the resources created by Terraform.

More from this blog

DevOps

9 posts