Terraform
HashiCorp Terraform
An IaC tool. Defines infrastructure in HCL. Multi-cloud support. Manages resources declaratively.
HashiCorp Terraform
An IaC tool. Defines infrastructure in HCL. Multi-cloud support. Manages resources declaratively.
Terraform is an Infrastructure as Code (IaC) tool released by HashiCorp in 2014. It uses HCL (HashiCorp Configuration Language), a declarative configuration language, to define and manage cloud infrastructure. It was the de facto standard in the IaC field until the OpenTofu fork in 2024.
Its core concepts are Provider (a platform plugin for AWS, Azure, GCP, etc.), Resource (the infrastructure being managed), State (stores the current infrastructure state), and Module (a reusable unit of configuration). You preview changes with terraform plan and actually apply them with terraform apply.
Terraform's strength is its multi-cloud support. With more than 3000 Providers, it manages not only AWS, Azure, and GCP but also a wide range of services such as Kubernetes, Datadog, and GitHub with a single tool. It tracks the current state of the infrastructure through the state file (tfstate) and accurately predicts the change plan.
In practice, teams use Terraform Cloud/Enterprise for team collaboration and state file management, or configure the backend with S3+DynamoDB. They use Modules to reuse complex configurations such as VPCs and EKS clusters, and apply the DRY (Don't Repeat Yourself) principle with Terragrunt.
# main.tf - AWS EKS ํด๋ฌ์คํฐ ๊ตฌ์ฑ ์์
terraform {
required_version = ">= 1.5.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
# ์ํ ํ์ผ ์๊ฒฉ ์ ์ฅ
backend "s3" {
bucket = "my-terraform-state"
key = "prod/eks/terraform.tfstate"
region = "ap-northeast-2"
dynamodb_table = "terraform-locks"
encrypt = true
}
}
provider "aws" {
region = var.aws_region
default_tags {
tags = {
Environment = var.environment
ManagedBy = "Terraform"
}
}
}
# variables.tf
variable "aws_region" {
description = "AWS ๋ฆฌ์ "
type = string
default = "ap-northeast-2"
}
variable "environment" {
description = "ํ๊ฒฝ (dev, staging, prod)"
type = string
}
variable "cluster_name" {
description = "EKS ํด๋ฌ์คํฐ ์ด๋ฆ"
type = string
}
# EKS ๋ชจ๋ ์ฌ์ฉ
module "eks" {
source = "terraform-aws-modules/eks/aws"
version = "~> 19.0"
cluster_name = var.cluster_name
cluster_version = "1.28"
vpc_id = module.vpc.vpc_id
subnet_ids = module.vpc.private_subnets
eks_managed_node_groups = {
general = {
desired_size = 2
min_size = 1
max_size = 5
instance_types = ["t3.medium"]
capacity_type = "ON_DEMAND"
}
spot = {
desired_size = 2
min_size = 0
max_size = 10
instance_types = ["t3.large", "t3.xlarge"]
capacity_type = "SPOT"
}
}
}
# outputs.tf
output "cluster_endpoint" {
description = "EKS ํด๋ฌ์คํฐ ์๋ํฌ์ธํธ"
value = module.eks.cluster_endpoint
}
output "cluster_name" {
description = "EKS ํด๋ฌ์คํฐ ์ด๋ฆ"
value = module.eks.cluster_name
}
# ์คํ ๋ช
๋ น์ด
# terraform init # ์ด๊ธฐํ
# terraform plan # ๋ณ๊ฒฝ ๊ณํ ํ์ธ
# terraform apply # ์ ์ฉ
# terraform destroy # ์ญ์
Senior: "Setting up a new environment used to take 3 days every time, but since modularizing it with Terraform, a single terraform apply finishes it within 2 hours."
Junior: "Where is the state file stored? If we keep it locally, it seems like collaboration won't work."
Senior: "We use an S3 backend and lock with DynamoDB. We've set terraform plan to run automatically in PRs through GitHub Actions."
Interviewer: "How did you manage Terraform state files?"
Candidate: "I stored it in a bucket with S3 versioning enabled and implemented state locking with DynamoDB. I also tried Remote State in Terraform Cloud, but when the team was small, the S3 approach was more advantageous in terms of cost. S3 versioning is essential for recovery if the state file gets corrupted."
Take a mock interview on this topic โ WIA SOOM AI Interview ยท Start for free
Reviewer: "If you only reference a resource without depends_on, the order is guaranteed through implicit dependencies. Adding it explicitly actually makes maintenance harder."
Developer: "Oh, so if I reference module.eks.cluster_endpoint, EKS is automatically created first. I'll remove depends_on."