DEV Community

Bipin Ghimire
Bipin Ghimire

Posted on

A Comprehensive Guide to Infrastructure as Code (IaC) Using Terraform

➡️ An open-source Infrastructure as Code (IaC) program called Terraform enables declarative cloud infrastructure definition, provisioning, and management. You can follow this instructions to set up Terraform step-by-step.🚀

Image description
Step One: Install the terraform
In linux/ Mac:

curl -fsSL 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

Enter fullscreen mode Exit fullscreen mode

Confirm the installation:

terraform -v
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a Terraform Project
Create a project directory:

mkdir terraform-aws && cd terraform-aws
Enter fullscreen mode Exit fullscreen mode

Initialize a new Terraform project

terraform init
Enter fullscreen mode Exit fullscreen mode

Step 3: Write Your First Terraform Configuration

Terraform configures itself using.tf files. An example main.tf file for setting up an AWS EC2 instance may be found below:

Create main.tf
Step 4: Initial Terraform

provider "aws" {
  region = "us-east-1"
}

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0" # Change based on your region
  instance_type = "t2.micro"

  tags = {
    Name = "TerraformInstance"
  }
}

Enter fullscreen mode Exit fullscreen mode

To download provider plugins and initialize Terraform, use the following command:

terraform init
Enter fullscreen mode Exit fullscreen mode

Step 5: Plan Infrastructure

Check what changes Terraform will make:

terraform plan
Enter fullscreen mode Exit fullscreen mode

Step 6: Apply Configuration
Provision the resources:

terraform apply -auto-approve
Enter fullscreen mode Exit fullscreen mode

The AWS EC2 instance will be created as a result.

Step 7: Destroy Infrastructure

To delete resources created by Terraform:

terraform destroy -auto-approve
Enter fullscreen mode Exit fullscreen mode

Now you have successfully used Terraform to install infrastructure!

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay