Using Terraform Data Sources Effectively
Learn how to use Terraform data sources to query existing resources, look up AMIs, reference remote state, and build dynamic configurations. Complete.
Infrastructure as Code
Compare Terraform and OpenTofu side by side. Understand licensing, features, compatibility, and which IaC tool is right for your organization.
In August 2023, HashiCorp announced a significant change: Terraform would move from the Mozilla Public License (MPL 2.0) to the Business Source License (BSL 1.1). This decision sent shockwaves through the infrastructure as code community and led to the creation of OpenTofu, a community-driven fork of Terraform maintained under the Linux Foundation.
Whether you're starting a new project or considering a migration, understanding the differences between these two tools is essential. This article provides a detailed, objective comparison to help you make an informed decision.
HashiCorp's BSL license allows free use of Terraform for most purposes but restricts use in products that compete with HashiCorp's commercial offerings. Specifically:
OpenTofu maintains the original open-source license:
For most individual users and organizations using Terraform as a tool (not building products around it), the license change has no practical impact. The distinction matters primarily for companies building managed Terraform services or embedding it in their products.
OpenTofu was forked from Terraform 1.5.x, and both tools maintain a high degree of compatibility:
| Feature | Terraform | OpenTofu |
|---|---|---|
| HCL Configuration Language | ✅ | ✅ |
| State Management | ✅ | ✅ |
| Provider Ecosystem | ✅ | ✅ (same registry) |
| Module Registry | ✅ | ✅ (own registry) |
| Import Blocks | ✅ | ✅ |
| Moved Blocks | ✅ | ✅ |
| Check Blocks | ✅ | ✅ |
OpenTofu has introduced features not available in Terraform:
1. Client-Side State Encryption
# OpenTofu-only feature
terraform {
encryption {
method "aes_gcm" "default" {
keys = key_provider.aws_kms.default
}
state {
method = method.aes_gcm.default
enforced = true
}
plan {
method = method.aes_gcm.default
enforced = true
}
}
}This encrypts state and plan files before they're stored, adding an extra layer of security beyond backend encryption.
2. Early Variable/Local Evaluation
OpenTofu allows using variables and locals in backend and module source configurations:
# Works in OpenTofu, not in Terraform
variable "environment" {
default = "production"
}
terraform {
backend "s3" {
bucket = "state-${var.environment}"
key = "terraform.tfstate"
}
}3. for_each with Unknown Values
OpenTofu handles for_each with values that aren't known until apply time more gracefully than Terraform.
Terraform also continues to innovate:
1. Stacks (Preview)
Terraform Stacks provide a higher-level abstraction for managing multiple Terraform configurations:
# Terraform Stacks configuration
component "networking" {
source = "./modules/networking"
inputs = {
region = var.region
}
}
component "compute" {
source = "./modules/compute"
inputs = {
vpc_id = component.networking.vpc_id
}
}2. Terraform Cloud / HCP Terraform Integration
Terraform has deep integration with HashiCorp's cloud platform, offering:
3. removed Block
Terraform 1.7+ introduced the removed block for safely removing resources from state without destroying them:
removed {
from = aws_instance.old_server
lifecycle {
destroy = false
}
}Both Terraform and OpenTofu use the same provider protocol and can use the same providers. The vast majority of providers work identically with both tools. OpenTofu maintains its own provider registry at registry.opentofu.org, but it mirrors most providers from the HashiCorp registry.
# This works in both Terraform and OpenTofu
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}Migration is straightforward for most projects:
# 1. Install OpenTofu
brew install opentofu
# 2. Replace terraform commands with tofu
tofu init
tofu plan
tofu apply
# 3. State files are compatible — no migration neededKey considerations:
.terraform.lock.hcl files are compatibleMoving back is equally simple, though you'll lose access to OpenTofu-specific features like state encryption:
terraform init -upgrade
terraform planRegardless of which tool you choose:
Learn by doing with interactive courses on CopyPasteLearn:
Terraform and OpenTofu are more similar than different. Both are excellent infrastructure as code tools that share the same foundation, configuration language, and provider ecosystem. The primary differences lie in licensing philosophy, governance model, and a handful of unique features.
For most users, the choice comes down to organizational preferences around open-source licensing and commercial support. The good news is that migration between the tools is relatively painless, so you're not locked into either decision permanently. Focus on writing clean, modular infrastructure code, and you'll be well-served by whichever tool you choose.
Learn how to use Terraform data sources to query existing resources, look up AMIs, reference remote state, and build dynamic configurations. Complete.
Master multi-account AWS management with Terraform. Learn provider aliases, cross-account IAM roles, AWS Organizations integration, and production-ready.
Learn how to implement Terraform state locking with AWS DynamoDB to prevent concurrent modifications and state corruption. Complete setup guide with examples.
Master Terraform version constraints for Terraform core and providers. Covers operators, lock files, required_version, required_providers, and upgrade...