DEV Community

Vishal Patil
Vishal Patil

Posted on

Terraform for Beginners: Infrastructure as Code on AWS Explained Simply

description: "A beginner-friendly introduction to Terraform: what it is, why it matters, the 4-command workflow, and your first AWS S3 bucket."
tags: terraform, aws, devops, beginners
cover_image:

canonical_url: https://builder.aws.com/content/3MuxB8LDXVowqLvRISwqJjqBOyl/introduction-to-terraform-beginner-friendly


What is Terraform?

Terraform is an open-source Infrastructure as Code (IaC) tool made by HashiCorp. Instead of clicking through the AWS Console to create servers, networks and databases, you write them as code in files, and Terraform builds them for you.

Analogy: Think of an architect's blueprint. You draw the plan once, and the builder (Terraform) constructs exactly that, every time.

Why Do We Need It?

Manual (Console) Terraform (Code)
Click-by-click, slow One command, fast
Easy to make mistakes Same result every time
Hard to repeat or share Reusable and shareable via Git
No history of changes Full version history
Hard to delete everything terraform destroy removes it all

Where Terraform Fits in the AWS + DevOps Map

If AWS and DevOps feel mixed up in your head, place Terraform on your map first:

  • AWS side: it works with almost every service (EC2, S3, VPC, IAM and more).
  • DevOps lifecycle: Build and Deploy, because it provisions infrastructure.
  • Works with: Git (stores the code), Jenkins (automates it) and CloudWatch (monitors what it builds).

Key Terms

  1. Provider: the plugin that talks to a platform (AWS, Azure, GCP).
  2. Resource: one thing you create, such as an EC2 instance or an S3 bucket.
  3. State file (terraform.tfstate): Terraform's record of what it has built.
  4. Variables: inputs that make your code reusable.
  5. Module: a reusable package of resources.

The Basic Workflow (4 Commands)

terraform init      # download the provider plugins
terraform plan      # preview what will change
terraform apply     # create the infrastructure
terraform destroy   # delete everything it created
Enter fullscreen mode Exit fullscreen mode

Your First Example: Create an S3 Bucket

Create a file named main.tf:

provider "aws" {
  region = "ap-south-1"
}

resource "aws_s3_bucket" "my_bucket" {
  bucket = "your-unique-bucket-name-12345"
}
Enter fullscreen mode Exit fullscreen mode

Then run:

terraform init
terraform plan
terraform apply
Enter fullscreen mode Exit fullscreen mode

Type yes when asked, and your bucket now exists. Bucket names must be globally unique, so change the name before you run it. When you are done practicing, run terraform destroy to clean up.

Tips for Beginners

  • Never put AWS access keys inside your .tf files, and never push them to GitHub. Use aws configure or IAM roles.
  • Always run plan before apply so you see what will change.
  • Add .terraform/ and *.tfstate to your .gitignore. State files can contain sensitive data.
  • Destroy lab resources after practice to avoid surprise bills. Setting up AWS Budgets and billing alarms first is a good habit.

Key Takeaways

  1. Terraform = infrastructure written as code.
  2. The workflow is init, plan, apply, destroy.
  3. It makes infrastructure repeatable, shareable and version-controlled.
  4. Start with one small resource, then grow into full projects like a 3-tier app.

What would you like to build first with Terraform: an EC2 instance, a VPC or something else? Tell me in the comments.

vishalcloudjourney


Connect with me

Top comments (0)