OpenTofu is an open-source infrastructure as code (IaC) tool that enables you to define and provision data center infrastructure using a declarative configuration language.
Check if OpenTofu is installed:
tofu versionExpected output format:
OpenTofu v1.x.x
If OpenTofu is not installed, you can install it using the official installation script:
# Download and install OpenTofu
curl --proto '=https' --tlsv1.2 -fsSL https://get.opentofu.org/install-opentofu.sh -o install-opentofu.sh
chmod +x install-opentofu.sh
./install-opentofu.sh --install-method deb
# Verify installation
tofu versionAlternative installation methods:
# Add the OpenTofu repository
curl -fsSL https://packages.opentofu.org/opentofu/tofu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/opentofu.gpg
echo "deb [signed-by=/usr/share/keyrings/opentofu.gpg] https://packages.opentofu.org/opentofu/tofu/deb/ any main" | sudo tee /etc/apt/sources.list.d/opentofu.list
# Install OpenTofu
sudo apt update
sudo apt install tofubrew install opentofuDownload the appropriate binary from the OpenTofu releases page and add it to your PATH.
# Initialize a new OpenTofu project
tofu init# Review planned changes (dry run)
tofu plan
# Apply changes to infrastructure
tofu apply
# Apply without interactive confirmation
tofu apply -auto-approve# Show current state
tofu show
# List resources in state
tofu state list
# Get specific resource information
tofu state show <resource_name>
# View outputs
tofu output# Destroy all managed infrastructure
tofu destroy
# Destroy without interactive confirmation
tofu destroy -auto-approve# Validate configuration syntax
tofu validate
# Format configuration files
tofu fmt
# Format and show differences
tofu fmt -diff├── main.tf # Main configuration file
├── variables.tf # Input variables
├── outputs.tf # Output definitions
├── versions.tf # Provider version constraints
└── terraform.tfvars # Variable values (optional)
terraform {
required_providers {
lxd = {
source = "terraform-lxd/lxd"
version = "~> 2.0"
}
}
required_version = ">= 1.0"
}resource "lxd_container" "example" {
name = "my-container"
image = "ubuntu:24.04"
config = {
"user.cloud-init.user-data" = file("${path.module}/cloud-init.yml")
}
}- Keep related resources in the same file
- Use descriptive resource names
- Separate environments with different state files
- Use modules for reusable components
- Always use remote state for team collaboration
- Use state locking to prevent concurrent modifications
- Regularly backup state files
- Never edit state files manually
- Use variables for sensitive data
- Store sensitive variables in secure locations
- Use environment variables for secrets
- Apply principle of least privilege
- Always version control your configuration files
- Use
.gitignoreto exclude state files and sensitive data - Tag releases for stable configurations
- Use meaningful commit messages
# Re-initialize to download providers
tofu init -upgrade# Force unlock (use with caution)
tofu force-unlock <lock-id># Validate configuration
tofu validate
# Check syntax with detailed output
tofu plan -detailed-exitcode# Enable debug logging
export TF_LOG=DEBUG
tofu apply
# Log to file
export TF_LOG_PATH=./tofu.log- OpenTofu Documentation
- OpenTofu GitHub Repository
- Terraform Registry (compatible providers)
- HCL Configuration Language