🔧 DevOps

OpenTofu

OpenTofu

An open-source fork of Terraform. Created after HashiCorp's license change.

📖 Detailed Explanation

OpenTofu is an open-source fork of HashiCorp Terraform, an alternative created by the community after HashiCorp changed the Terraform license to the BSL (Business Source License) in 2023. As a Linux Foundation project, it maintains the MPL 2.0 license and provides a truly open-source IaC (Infrastructure as Code) tool.

OpenTofu is compatible with Terraform 1.5.x, so existing Terraform code can be used with almost no modification. The .tf files, provider, and module syntax are the same, and you only need to change the terraform command to tofu. Existing state files (.tfstate) can also be migrated.

OpenTofu is evolving rapidly under community leadership and is also adding its own features. State encryption (encryption of state files) is a feature introduced first in OpenTofu, and it strengthens security by encrypting state files that contain sensitive infrastructure information.

Switching from Terraform to OpenTofu is relatively simple. Replace the terraform binary with tofu and update the commands in your CI/CD pipeline. HashiCorp providers can still be used, and you can find community providers and modules in the OpenTofu Registry.

💻 Code Example

# main.tf - OpenTofu 인프라 정의 (Terraform과 동일 문법)
terraform {
  required_version = ">= 1.6.0"

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }

  # OpenTofu 1.7+ State Encryption
  encryption {
    key_provider "aws_kms" "main" {
      kms_key_id = "alias/tofu-state-key"
      region     = "ap-northeast-2"
    }
    state {
      encrypted = true
      method    = "aws_kms"
      keys      = [key_provider.aws_kms.main]
    }
  }

  backend "s3" {
    bucket         = "my-tofu-state"
    key            = "prod/infrastructure.tfstate"
    region         = "ap-northeast-2"
    dynamodb_table = "tofu-locks"
    encrypt        = true
  }
}

provider "aws" {
  region = var.region
  default_tags {
    tags = {
      Environment = var.environment
      ManagedBy   = "OpenTofu"
    }
  }
}

# VPC 모듈 사용
module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "5.0.0"

  name = "${var.project}-vpc"
  cidr = var.vpc_cidr

  azs             = ["${var.region}a", "${var.region}c"]
  private_subnets = var.private_subnets
  public_subnets  = var.public_subnets

  enable_nat_gateway = true
  single_nat_gateway = var.environment != "production"
}
# OpenTofu CLI 사용법 (Terraform과 거의 동일)

# 설치 (macOS)
brew install opentofu

# 초기화 (provider 다운로드)
tofu init

# 변경사항 미리보기
tofu plan -out=plan.tfplan

# 인프라 적용
tofu apply plan.tfplan

# 리소스 목록 확인
tofu state list

# 특정 리소스 상태 보기
tofu state show module.vpc.aws_vpc.this

# Terraform에서 OpenTofu로 마이그레이션
# 1. .terraform.lock.hcl 삭제 (선택적)
rm .terraform.lock.hcl

# 2. tofu init으로 재초기화
tofu init -upgrade

# 3. 상태 확인
tofu plan  # 변경사항 없어야 함

# CI/CD 예시 (GitHub Actions)
# - uses: opentofu/setup-opentofu@v1
#   with:
#     tofu_version: 1.7.0
# - run: tofu init
# - run: tofu plan -no-color

🗣️ How It's Said in Practice

Infrastructure Lead: "The Terraform Enterprise license renewal cost is too high. How about switching to an OpenTofu + Atlantis combination?"

Junior: "Do we have to modify all of our existing .tf files?"

Senior: "No, the syntax is the same, so we can use them as they are. Just change the terraform command to tofu and modify the CI pipeline. Let's run both in parallel for about a month and verify."

Interviewer: "Please tell me about your experience with IaC tools and recent trends."

Candidate: "I managed AWS/GCP infrastructure with Terraform. Since OpenTofu has been getting attention after the HashiCorp license change, I tested it in a side project, and it is 99% compatible with Terraform, and useful features like state encryption are being added as well. The community is also active, so I think it is a good alternative in the long term."

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

Reviewer: "We store state in S3, so please add an encryption block as well. It's supported from OpenTofu 1.7, so encrypting the state file will strengthen security."

Author: "I'll create a KMS key and add the encryption configuration. We can migrate the existing state with tofu init -migrate-state, right?"

⚠️ Cautions

📚 Learn More