DEV Community

Cover image for 🚀 Crash Course: Terraform Basics for Freshers
DevOps Descent
DevOps Descent

Posted on

🚀 Crash Course: Terraform Basics for Freshers

Image description

Terraform is a powerful tool for managing infrastructure as code (IaC). If you’re new to cloud or DevOps, don’t worry—this guide will help you grasp Terraform's basics with easy language and an example to kickstart your journey.

Note: Terraform's updated license (BSL 1.1) doesn't stop organizations from using it directly; it restricts using Terraform to create competing commercial services or products. For most organizations using Terraform internally to manage infrastructure, the license allows free use. However, it prevents creating and selling a competing tool or platform that embeds Terraform.
If you're not building a product that competes with HashiCorp's offerings, the license change shouldn't impact your organization.


What is Terraform?

Image description

Terraform is an open-source tool by HashiCorp that allows you to define, manage, and provision your cloud infrastructure using code. Think of it as a way to create servers, networks, and databases automatically, instead of clicking through cloud provider dashboards.

Why Terraform?

  • Multi-cloud Support: Works with AWS, Azure, GCP, and more.
  • Repeatability: Easily recreate the same infrastructure.
  • Automation: Avoid manual errors and save time.
  • Version Control: Track changes using Git, just like code.

Key Terraform Concepts

Image description

  1. Providers

    Providers are plugins that let Terraform talk to different platforms (e.g., AWS, Azure).

    Example: Use the AWS provider to manage AWS resources.

  2. Resources

    Resources are the building blocks of infrastructure (e.g., EC2 instance, S3 bucket).

  3. Variables

    Variables allow you to define values (like instance type) that you can reuse.

  4. State

    Terraform keeps track of your infrastructure using a state file, ensuring consistency.


How to Install Terraform

Follow these steps to get started:

  1. Download Terraform

  2. Install

    • Extract the downloaded file and add it to your system's PATH.
    • Verify the installation:
     terraform --version
    

Your First Terraform Project

Let’s create a simple AWS EC2 instance using Terraform.

Step 1: Set Up Your AWS Account

  • Create an AWS account if you don’t have one.
  • Generate an Access Key and Secret Key (for programmatic access).

Step 2: Create a Terraform File

  1. Create a folder for your project:
   mkdir terraform-demo && cd terraform-demo
Enter fullscreen mode Exit fullscreen mode
  1. Create a file named main.tf:
provider "aws" {
  region = "us-east-1"
  access_key = "<your-access-key>"
  secret_key = "<your-secret-key>"
}
resource "aws_instance" "example" {
  ami           = "ami-0c02fb55956c7d316" # Amazon Linux 2
  instance_type = "t2.micro"
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Initialize Terraform

Run this command to download the AWS provider:

terraform init
Enter fullscreen mode Exit fullscreen mode

Image description

Step 4: Plan the Changes

Preview what Terraform will create:

terraform plan

Enter fullscreen mode Exit fullscreen mode

Image description

Step 5: Apply the Changes

Create the EC2 instance:

terraform apply
Enter fullscreen mode Exit fullscreen mode

Image description

Type yes when prompted.

Step 6: Verify Your Instance
Go to the AWS Console → EC2 → Instances to see your new instance. 🎉

Clean Up Resources
To avoid unnecessary charges, destroy the resources you created:

terraform destroy
Enter fullscreen mode Exit fullscreen mode

Image description

Conclusion
Terraform makes managing cloud infrastructure simple and repeatable. With this crash course, you’ve created your first EC2 instance and taken a big step toward becoming a DevOps or Cloud Engineer. Keep experimenting, and you’ll master Terraform in no time!

Happy coding! 🌟

Support if you found this helpful😉

No Money 🙅🏻‍♀️ just Subscribe to me YouTube channel.

Linktree Profile: https://linktr.ee/DevOps_Descent
GitHub: https://github.com/devopsdescent

Top comments (0)