0. ๐ ์ ํด๋ผ์ฐ๋ ์ธํ๋ผ์ โTerraform & OpenTofuโ๋ฅผ ์ฌ์ฉํ๋์?
์ฝ์ ํด๋ฆญ(ClickOps)์ผ๋ก ๋ง๋ ์ธํ๋ผ๋ ๋ณ๊ฒฝ ์ด๋ ฅ์ด ๋จ์ง ์๊ณ ํ๊ฒฝ(Dev/Staging/Prod) ๊ฐ ์ฐจ์ด๊ฐ ๋ฐ์ํฉ๋๋ค. **OpenTofu(v1.13.x, Linux Foundation ์คํ์์ค)**์ Terraform์ ๋์ผํ HCL(HashiCorp Configuration Language) ๋ฌธ๋ฒ์ผ๋ก ์ธํ๋ผ๋ฅผ ์ ์ธํ๊ณ , State ๋น๊ต๋ฅผ ํตํด ์์ ํ๊ฒ ๋ณ๊ฒฝ๋ถ์ ๋ฏธ๋ฆฌ ๊ฒํ (plan)ํ๊ณ ์ ์ฉ(apply)ํ ์ ์์ต๋๋ค.
1. ํ์ ์ฌ์ ์๊ฑด & ์์คํ ์๊ตฌ์ฌ์ (Prerequisites & System Requirements)
๐ป ์์คํ ์ค์น ๋ฐ ์คํ ์๊ตฌ์ฌ์ (System Requirements)
| ํญ๋ชฉ | ์ต์ ์ฌ์ (Minimum) | ๊ถ์ฅ ์ฌ์ (Recommended) | ์ฐธ๊ณ ๋ฐ ๋น๊ณ |
|---|---|---|---|
| ์ด์์ฒด์ (OS) | Windows 10/11, macOS 13+, Linux | Ubuntu 24.04 LTS / macOS Apple Silicon | CLI ๋ฐ์ด๋๋ฆฌ ๋จ์ผ ์คํ |
| CPU / RAM | 2 vCPU / 2 GB RAM | 4 vCPU / 4 GB+ RAM | ๋ค์ค ํ๋ก๋ฐ์ด๋ ํ๋ฌ๊ทธ์ธ ๋ค์ด๋ก๋/๊ฒ์ฆ |
| ๋ฐํ์ / ํด์ฒด์ธ | OpenTofu v1.13+ (tofu) ๋๋ Terraform CLI | aws CLI v2, tflint, trivy | ๋ก์ปฌ ์ค์ต ์ LocalStack ํ์ฉ ๊ฐ๋ฅ |
# macOS (Homebrew)๋ก OpenTofu ์ค์น
brew install opentofu
# ๋ฒ์ ํ์ธ
tofu version # OpenTofu v1.13.x
[1๊ฐ] HCL ๋ณ์ยท๋ชจ๋ ์ค๊ณ & ๊ธฐ๋ณธ ์ธํ๋ผ ์ ์ธ
# variables.tf
variable "environment" {
type = string
description = "๋ฐฐํฌ ํ๊ฒฝ (dev | staging | prod)"
validation {
condition = contains(["dev", "staging", "prod"], var.environment)
error_message = "environment๋ dev, staging, prod ์ค ํ๋์ฌ์ผ ํฉ๋๋ค."
}
}
variable "vpc_cidr" {
type = string
default = "10.20.0.0/16"
}
# main.tf
terraform {
required_version = ">= 1.10"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.80"
}
}
}
provider "aws" {
region = "ap-northeast-2"
}
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "~> 5.16"
name = "lucky-${var.environment}-vpc"
cidr = var.vpc_cidr
azs = ["ap-northeast-2a", "ap-northeast-2c"]
private_subnets = ["10.20.1.0/24", "10.20.2.0/24"]
public_subnets = ["10.20.101.0/24", "10.20.102.0/24"]
enable_nat_gateway = true
single_nat_gateway = var.environment != "prod"
}
[2๊ฐ] S3 + ๋ค์ดํฐ๋ธ Lockfile ๊ธฐ๋ฐ Remote State ๊ด๋ฆฌ
ํ ํ์
์์๋ State ํ์ผ์ ๋ก์ปฌ์ ๋์ง ์๊ณ S3 ์๊ฒฉ ๋ฐฑ์๋์ ์ํธํ ์ ์ฅํ๋ฉฐ ๋์ apply ์ถฉ๋์ ๋ง๊ธฐ ์ํด State Lock์ ํ์ฑํํฉ๋๋ค.
# backend.tf
terraform {
backend "s3" {
bucket = "lucky-terraform-state-prod"
key = "network/ap-northeast-2/terraform.tfstate"
region = "ap-northeast-2"
encrypt = true
use_lockfile = true # S3 ๋ค์ดํฐ๋ธ State Lock (DynamoDB ํ
์ด๋ธ ๋ถํ์)
}
}
# ์ด๊ธฐํ ๋ฐ ๋ณ๊ฒฝ ๊ณํ ๊ฒํ
tofu init
tofu plan -var="environment=dev" -out=tfplan
tofu apply tfplan
[3๊ฐ] Plan / Apply GitOps ์๋ํ (PR ๋ฆฌ๋ทฐ ์ํฌํ๋ก์ฐ)
# .github/workflows/iac.yml
name: OpenTofu GitOps
on:
pull_request:
paths: ["infra/**"]
jobs:
plan:
runs-on: ubuntu-latest
defaults:
run:
working-directory: infra
steps:
- uses: actions/checkout@v4
- uses: opentofu/setup-opentofu@v1
with:
tofu_version: 1.13.9
- run: tofu init
- run: tofu fmt -check -recursive
- run: tofu validate
- run: tofu plan -no-color
[4๊ฐ] Drift ๊ฐ์ง, import ๋ธ๋ก & ์์ ํ State ๋ฆฌํฉํ ๋ง
์ค๋ฌด์์ ๋๊ตฐ๊ฐ AWS ์ฝ์์์ ์๋์ผ๋ก ๋ฆฌ์์ค๋ฅผ ์์ ํ๋ค๋ฉด(Drift) tofu plan -refresh-only๋ก ์ฐจ์ด๋ฅผ ํ์ธํ๊ณ , ๊ธฐ์กด ๋ฆฌ์์ค๋ฅผ ์ฝ๋๋ก ํธ์
ํ ๋๋ ์ ์ธ์ import {} ๋ฐ moved {} ๋ธ๋ก์ ์ฌ์ฉํฉ๋๋ค.
# ๊ธฐ์กด์ ์๋ ์์ฑ๋ S3 ๋ฒํท์ IaC State๋ก ์์ ํ๊ฒ ํธ์
import {
to = aws_s3_bucket.legacy_assets
id = "my-existing-assets-bucket"
}
resource "aws_s3_bucket" "legacy_assets" {
bucket = "my-existing-assets-bucket"
}
ํ๋ก๋์ ์ฒดํฌ๋ฆฌ์คํธ
- OpenTofu v1.7+์ **State Encryption(
encryption { method "aes_gcm" ... })**์ ํ์ฑํํด State ๋ด ๋ฏผ๊ฐ ์ ๋ณด ๋ ธ์ถ์ ์์ฒ ์ฐจ๋จํ์ธ์. - ํ๋ก๋์
ํ๊ดด ๋ฐฉ์ง๋ฅผ ์ํด ํต์ฌ DB/VPC ๋ฆฌ์์ค์๋
lifecycle { prevent_destroy = true }๋ฅผ ๋ฐ๋์ ์ ์ธํ์ธ์.
์๊ฒฌ ๋ฐ ์ง๋ฌธ
0์์ง ๋ฑ๋ก๋ ์๊ฒฌ์ด ์์ต๋๋ค. ์ฒซ ๋ฒ์งธ ๋๊ธ์ ๋จ๊ฒจ๋ณด์ธ์!
๋๊ธ ์์
๋๊ธ ์ญ์