DEV Community

Cover image for βœ… Task 1 β€” Initialize a Terraform project with backend and provider blocks
Latchu@DevOps
Latchu@DevOps

Posted on

βœ… Task 1 β€” Initialize a Terraform project with backend and provider blocks

🎯 Real-World Scenario

You just joined a company as a DevOps Engineer.

Your manager asks you:

β€œSet up a new Terraform project that uses AWS as the provider, and configure the backend so our state is stored safely in S3. Also, set up state locking using DynamoDB.”

This is the first thing Terraform engineers do in any company.


🧩 What you will learn (Interview Value)

  • What a provider is in Terraform
  • Why Terraform uses a backend
  • What state, remote backend, and state locking mean
  • Why companies NEVER allow local state
  • How to write backend and provider blocks
  • How terraform init works internally

These are asked in 90% of Terraform interviews.


πŸ—οΈ Folder Structure for Practice

Create a folder:

terraform-lab-01/
  β”œβ”€β”€ main.tf
  β”œβ”€β”€ backend.tf
  β”œβ”€β”€ provider.tf
Enter fullscreen mode Exit fullscreen mode

1


πŸ§ͺ Step 1 – Create the S3 bucket and DynamoDB table (manually)

Before Terraform can use backend, the backend must exist already.

Create S3 bucket:

tf-backend-lab-123
Enter fullscreen mode Exit fullscreen mode

2

Create DynamoDB table:

Name: tf-state-lock
Partition key: LockID (String)
Enter fullscreen mode Exit fullscreen mode

3


πŸ§ͺ Step 2 – Create backend.tf

terraform {
  backend "s3" {
    bucket         = "tf-backend-lab-123"
    key            = "env/dev/terraform.tfstate"
    region         = "us-east-1"
    dynamodb_table = "tf-state-lock"
    encrypt        = true
  }
}
Enter fullscreen mode Exit fullscreen mode

πŸ’¬ Interview Tip:

If they ask β€œWhy do we store state in S3?”:

βœ” Remote state enables team collaboration
βœ” Prevents β€œstate corruption”
βœ” S3 gives versioning, encryption, backups
βœ” DynamoDB prevents multiple people applying at the same time
βœ” Required for CI/CD automation


πŸ§ͺ Step 3 – Create provider.tf

provider "aws" {
  region = var.region
}
Enter fullscreen mode Exit fullscreen mode

Create variables.tf (optional but clean):

variable "region" {
  default = "us-east-1"
}
Enter fullscreen mode Exit fullscreen mode

πŸ§ͺ Step 4 – Add a simple resource to test the setup

In main.tf:

resource "aws_s3_bucket" "demo" {
  bucket = "latchudevops-bucket-12345"
}
Enter fullscreen mode Exit fullscreen mode

πŸ§ͺ Step 5 – Initialize the project

Run:

terraform init
Enter fullscreen mode Exit fullscreen mode

You should see:

Initializing the backend...
Terraform has been successfully initialized!
Enter fullscreen mode Exit fullscreen mode

This means:

  • Backend is connected
  • Terraform downloaded the AWS provider plugin
  • State file will be written to S3 bucket

πŸ§ͺ Step 6 – Run plan & apply

terraform plan
terraform apply
Enter fullscreen mode Exit fullscreen mode

The S3 bucket should be created.


πŸ§ͺ Step 7 – Verify remote state

Check:

4

  • S3 β†’ your state file env/dev/terraform.tfstate
  • DynamoDB β†’ new lock entry during apply

5


🎀 Interview-Level Explanation (Use this Answer!)

Q: What is a provider in Terraform?

Terraform does not create resources directly. Providers act as plugins to interact with cloud platforms like AWS, Azure, GCP, GitHub, Kubernetes, etc.
The AWS provider allows Terraform to call AWS APIs and manage resources.


Q: What is Terraform state? Why do we need it?

Terraform keeps a map of your real AWS resources in a state file.
Without state, Terraform can't understand what it created previously.
State helps Terraform:

  • Detect drift
  • Track changes
  • Plan updates
  • Destroy resources safely

Q: Why use a remote backend like S3?

Because local state cannot be used by teams. Remote backend (S3 + DynamoDB) gives:

βœ” Shared state for team collaboration
βœ” State locking β†’ prevents corruption
βœ” Encryption and versioning
βœ” Required for CI/CD pipelines


Q: Why do we need DynamoDB for state locking?

To prevent two engineers (or CI/CD pipeline) from applying at the same time.
It ensures only 1 apply runs at a time β†’ avoids broken state.


Q: What happens during terraform init?

  • Downloads providers
  • Initializes backend
  • Prepares terraform working directory
  • Checks and migrates state if backend changes

β˜‘οΈ Expected Output

You end Day 1 with:

βœ” Terraform project initialized
βœ” AWS provider configured
βœ” Remote backend working (S3 + DynamoDB)
βœ” First resource deployed
βœ” You understand providers, backends, state & locking
βœ” You can answer real Terraform interview questions


🌟 Thanks for reading! If this post added value, a like ❀️, follow, or share would encourage me to keep creating more content.


β€” Latchu | Senior DevOps & Cloud Engineer

☁️ AWS | GCP | ☸️ Kubernetes | πŸ” Security | ⚑ Automation
πŸ“Œ Sharing hands-on guides, best practices & real-world cloud solutions

Top comments (0)