🚀 Day 23 of 30 Days DevOps Interview Preparation Challenge – Provision EC2 with Terraform

Series: 30 Days DevOps Interview Preparation
Author: Tathagat Gaikwad

Welcome back to Day 23 of my 30 Days DevOps Interview Preparation Challenge!
Today’s focus is on provisioning EC2 instances with Terraform — one of the most common hands-on tasks you’ll face both in real-world DevOps work and in technical interviews.


🔹 Why Terraform for EC2?

Traditionally, engineers would launch EC2 instances manually using the AWS Console or AWS CLI. While this works for small-scale projects, it doesn’t scale well when you need to manage multiple servers, environments, and configurations.

That’s where Terraform comes in:

  • It is an Infrastructure as Code (IaC) tool.

  • It enables you to define infrastructure declaratively in .tf files.

  • Infrastructure can be version-controlled just like application code.

  • You can reproduce environments consistently without manual intervention.

  • Works not only with AWS but also with Azure, GCP, Kubernetes, and many others.

For DevOps interviews, you must understand how Terraform automates infrastructure and why it is preferred over manual approaches.


🔹 Terraform Workflow

  1. Write configuration files (.tf) describing the infrastructure.

  2. Initialize Terraform → terraform init (downloads provider plugins).

  3. Preview execution plan → terraform plan.

  4. Apply configuration → terraform apply (creates resources).

  5. Destroy resources when not needed → terraform destroy.

This workflow ensures predictable, versioned, and automated infrastructure.


🔹 Practical: Provisioning EC2 with Terraform

Here’s a simple example to create an EC2 instance on AWS.

Step 1: Create a Terraform file main.tf

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

resource "aws_instance" "my_ec2" {
  ami           = "ami-08c40ec9ead489470" # Amazon Linux 2 AMI
  instance_type = "t2.micro"

  tags = {
    Name = "Terraform-EC2"
  }
}

Step 2: Initialize Terraform

terraform init

Step 3: Preview Execution Plan

terraform plan

Step 4: Apply Configuration

terraform apply -auto-approve

This will create an EC2 instance in AWS.

Step 5: Destroy Resources (Optional)

terraform destroy -auto-approve

🔹 Advanced EC2 Provisioning with User Data

You can also use user data scripts to install software when the instance boots:

resource "aws_instance" "my_ec2" {
  ami           = "ami-08c40ec9ead489470"
  instance_type = "t2.micro"

  user_data = <<-EOF
              #!/bin/bash
              yum update -y
              yum install -y httpd
              systemctl start httpd
              systemctl enable httpd
              echo "Hello from Terraform EC2" > /var/www/html/index.html
              EOF

  tags = {
    Name = "Terraform-EC2-WebServer"
  }
}

This will launch an EC2 instance with Apache HTTPD installed and a simple webpage hosted automatically.


🔹 Interview Questions & Answers

✅ Q1. What is Terraform and how is it different from AWS CloudFormation?

Answer:

  • Terraform is a multi-cloud IaC tool (works with AWS, Azure, GCP, Kubernetes, etc.).

  • CloudFormation is AWS-only.

  • Terraform uses a declarative language (HCL), while CloudFormation uses JSON/YAML.

  • Terraform has a concept of state files to track resources.


✅ Q2. What are providers in Terraform?

Answer:
Providers are plugins that allow Terraform to interact with cloud platforms or services.
Example: aws, azurerm, google, kubernetes.


✅ Q3. Difference between terraform plan and terraform apply?

Answer:

  • terraform plan → Shows the execution plan without making changes.

  • terraform apply → Executes the plan and creates/updates resources.


✅ Q4. How does Terraform maintain the state of resources?

Answer:

  • Terraform uses a state file (terraform.tfstate) to record resource details.

  • It helps Terraform know what’s already deployed and what needs to be changed.

  • Can be stored locally or remotely (e.g., in S3 with DynamoDB for locking).


✅ Q5. How do you manage multiple environments (dev, staging, prod) in Terraform?

Answer:

  • Use workspaces (terraform workspace new dev).

  • Or maintain separate directories/modules for each environment.

  • Use variables and tfvars files for environment-specific configurations.


🔹 Pro Tips for Interviews

  1. Don’t stop at just launching EC2 — talk about attaching security groups, IAM roles, and EBS volumes using Terraform.

  2. Emphasize idempotency → Terraform ensures the same code always results in the same infrastructure.

  3. Highlight remote state management for team collaboration.

  4. Explain Terraform modules → best practice for reusable, production-grade code.


🔹 Conclusion

Provisioning EC2 with Terraform is one of the first steps in Infrastructure as Code (IaC).
It not only shows your ability to automate but also demonstrates real DevOps thinking — consistency, scalability, and automation.

Tomorrow (Day 24), we’ll continue with another exciting DevOps concept. 🚀


💬 Are you using Terraform for EC2 provisioning in your projects? Share your experience in the comments!

#DevOps #AWS #Terraform #Cloud #IaC #InterviewPreparation #30DaysOfDevOps

0
Subscribe to my newsletter

Read articles from Tathagat Gaikwad directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tathagat Gaikwad
Tathagat Gaikwad