Terraform Infrastructure as Code: A Practical Guide for 2026
How teams run Terraform in production: state, repository layout, testing, CI/CD and policy, and what changed with Terraform 1.17, HCP Terraform pricing and OpenTofu.
Published November 25, 2026 · HostingX Solutions LLC
Short answer
With Terraform you describe infrastructure in code, plan shows what will change, apply makes the change, and state records which real objects belong to which resource in your code.
For a team, the essentials are remote state with locking, small root modules per environment and component, pinned versions, and a pipeline that runs
planon every pull request andapplyonly after merge.The current release is Terraform 1.17.2 (November 11, 2026). Terraform has been under the Business Source License since 1.6.0. OpenTofu, its MPL-licensed fork under the Linux Foundation, is at 1.13.
What Is Infrastructure as Code with Terraform?
Infrastructure as code means your networks, clusters, databases and permissions are defined in files that live in version control, get reviewed like application code, and can be recreated from scratch. Terraform does this with HashiCorp Configuration Language (HCL) and providers, plugins that translate your code into API calls to AWS, Google Cloud, Azure, Kubernetes, GitHub and many other services.
HashiCorp describes the core workflow in three steps:
Write: author infrastructure as code.
Plan: preview changes before applying.
Apply: provision reproducible infrastructure.
The plan is what makes Terraform safe to use in a team. Every change shows up as a diff before anything is touched, and that diff is what reviewers approve.
How Does Terraform State Work?
In HashiCorp's words, the primary purpose of state is “to store bindings between objects in a remote system and resource instances declared in your configuration.” Without it, Terraform can't tell which real security group corresponds to aws_security_group.web in your code.
By default, state is a local file named terraform.tfstate. That stops working as soon as two people or a pipeline run Terraform. HashiCorp warns that state kept in storage without locking or access control “can result in data loss or exposure of secrets stored in the state file.” Remote state fixes three things at once: everyone works from the same file, locking prevents two runs against the same state, and other configurations can read its outputs.
Remote state on AWS without DynamoDB
The S3 backend can now lock state on its own. Native S3 locking appeared in Terraform 1.10 and became generally available in 1.11, and the S3 backend docs now say “DynamoDB-based locking is deprecated and will be removed in a future minor version.” A minimal setup:
terraform {
required_version = ">= 1.11"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0"
}
}
backend "s3" {
bucket = "example-terraform-state"
key = "prod/network/terraform.tfstate"
region = "us-east-1"
encrypt = true
use_lockfile = true
}
}Bucket name and key are placeholders. Turn on versioning for the state bucket and restrict who can read it: state can contain secrets.
For workspaces, state migrations and multi-account setups, see our Terraform state management guide.
How Should You Structure a Terraform Repository?
Modules: HashiCorp's standard structure
For reusable modules, HashiCorp recommends a standard layout: main.tf as the primary entry point, variables.tf and outputs.tf for declarations, a README, nested modules under modules/ and usage examples under examples/.
Root modules: one per environment and component
How you split root modules is a design choice, not a HashiCorp rule. We keep one root module, and therefore one state file, per environment and component, such as production networking or staging services. A bad apply then reaches one component in one environment, plans stay fast, and teams can own their own folders:
infrastructure/
modules/
network/
main.tf
variables.tf
outputs.tf
README.md
service/
live/
staging/
network/
terraform.tf
backend.tf
providers.tf
main.tf
variables.tf
prod/
network/Style rules worth enforcing
Use the file names from HashiCorp's style guide:
terraform.tffor the singleterraformblock,providers.tf,backend.tf,main.tf,variables.tf,outputs.tfandlocals.tf.Name resources with a descriptive noun and underscores, and don't repeat the resource type:
aws_instance.web, notaws_instance.web_instance.Run
terraform fmtandterraform validatebefore committing.Pin Terraform, provider and module versions, and commit the dependency lock file.
Keep
terraform.tfstateout of Git. The style guide is blunt about why: “The state file can contain secrets.”
Which Terraform Features Should You Be Using in 2026?
Many Terraform codebases were written before these features existed. Each one replaces a manual workaround:
| Feature | Since | What it replaces or adds |
|---|---|---|
import | 1.5.0 (June 2023) | Adopt existing resources by declaring them in code, then review the import in a normal plan |
terraform test | 1.6.0 (October 2023) | Native tests in .tftest.hcl files made of run blocks |
removed | 1.7.0 (January 2024) | Record that a resource left the configuration, optionally without destroying it. Test mocks for providers and modules arrived in the same release |
| Ephemeral resources | 1.10.0 (November 2024) | Values read fresh in each phase and never stored in state or plan files |
| Write-only arguments, S3 native locking GA | 1.11.0 (February 2025) | Pass secrets to resources without persisting them in state; lock S3 state without DynamoDB |
| Variables in provider requirements, Terraform Policy GA | 1.17.0 (October 2026) | Use variables and locals in provider requirements; native HCL policy in HCP Terraform |
Versions and dates from the Terraform changelogs on GitHub.
import and removed blocks matter most day to day, because they turn risky one-off commands into reviewed code:
import {
to = aws_s3_bucket.logs
id = "example-logs-bucket"
}
removed {
from = aws_instance.legacy
lifecycle {
destroy = false
}
}Ephemeral resources and write-only arguments close an old gap: database passwords and API tokens no longer have to end up in state. Write-only arguments need Terraform 1.11 or later and a provider resource that supports them.
How Do You Test Terraform Code?
Testing happens in layers, from cheapest to most thorough:
Format and validate.
terraform fmt -checkandterraform validatecatch syntax and type errors in seconds.Static analysis. Scanners such as Checkov and Trivy flag insecure settings before anything is planned. Our Terraform DevSecOps guide shows how to wire them in.
Native tests.
terraform testruns.tftest.hclfiles made ofrunblocks, either against a plan or against real infrastructure it creates and destroys. Since 1.7, providers can be mocked, so module tests don't need cloud credentials.
# tests/bucket.tftest.hcl
variables {
bucket_name = "example-test-bucket"
}
run "bucket_name_is_set" {
command = plan
assert {
condition = aws_s3_bucket.this.bucket == var.bucket_name
error_message = "Bucket name does not match the input variable."
}
}How Do You Run Terraform in CI/CD?
This pattern works across GitHub Actions, GitLab CI and similar tools:
# On every pull request
terraform fmt -check -recursive
terraform init -input=false
terraform validate
terraform test
terraform plan -input=false -out=tfplan
# After merge, in the pipeline, from the reviewed plan
terraform apply -input=false tfplanApplies come from the pipeline, not from laptops. Give the pipeline short-lived cloud credentials through OIDC instead of stored access keys.
Apply the plan that was reviewed. Saving the plan to a file and applying that file means nothing changes between review and apply.
Post the plan on the pull request. Reviewers should see the diff without running anything.
Check for drift on a schedule. A nightly
terraform planthat reports any difference shows changes made outside Terraform.Show cost in the pull request. A cost estimate next to the plan catches expensive changes before merge. See Terraform for FinOps.
How Do You Enforce Policy on Terraform?
HCP Terraform supports three policy frameworks: Terraform policy (native HCL, which needs Terraform 1.17 or later), Sentinel and Open Policy Agent. A policy set can contain policies from only one framework. Sentinel has advisory, soft-mandatory and hard-mandatory enforcement; OPA has advisory and mandatory. The free edition allows one policy set of up to five policies.
Without HCP Terraform, run the same checks in CI: OPA with Conftest against the JSON plan, or Checkov policies, as a required status check on the pull request. Tag policies belong here too. Untagged resources are the most common reason cloud costs can't be allocated to a team.
HCP Terraform or Your Own Pipeline?
HCP Terraform runs plans and applies remotely and adds state storage, policy, private registry, and Stacks. Its plans as of November 2026:
| Plan | Price | Notes |
|---|---|---|
| Free | $0 | Up to 500 managed resources, one concurrent run |
| Essentials | From $0.10 per resource per month | Billed per managed resource per hour |
| Standard | From $0.47 per resource per month | Billed per managed resource per hour |
| Premium | From $0.99 per resource per month | Billed per managed resource per hour |
HashiCorp ended the legacy free plan on March 31, 2026 and moved remaining organizations to the current Free tier. Terraform Stacks, which deploy many related configurations together, became generally available on September 25, 2025 for HCP Terraform plans billed by resources under management, and are also in Terraform Enterprise 2.0 or later.
Running Terraform from your own CI with an S3 or GCS backend costs nothing for the tool itself, but you build and maintain the guardrails: locking, credentials, policy checks and drift detection. Billing per managed resource is worth estimating before you choose. Count how many resources your state files hold today.
Terraform or OpenTofu?
In August 2023, HashiCorp moved future releases from MPL 2.0 to the Business Source License 1.1. The license applies to Terraform 1.6.0 and later. Each version converts to MPL 2.0 four years after it is published. Its additional use grant allows production use unless you offer Terraform to third parties in a way that competes with IBM's paid versions. IBM completed its acquisition of HashiCorp on February 27, 2025.
OpenTofu is the fork that stayed on MPL 2.0. It is a Linux Foundation project, was accepted into the CNCF as a Sandbox project in April 2025, and released 1.13.10 on November 23, 2026. Features it added on its own path include client-side state encryption (1.7), variables in module sources and backend configuration (1.8), provider for_each (1.9) and module and provider distribution through OCI registries (1.10).
Stay on Terraform if you use or plan to use HCP Terraform, Stacks or Terraform Enterprise, or want vendor support from IBM.
Consider OpenTofu if an open-source license matters to your company, or you need state encryption that doesn't depend on the backend.
Either way, pin the binary version in CI. Terraform and OpenTofu have diverged since 2023, so newer configurations aren't always portable between them.
One more change if you use CDK for Terraform: HashiCorp deprecated CDKTF on December 10, 2025 and no longer maintains it. Plan a move to HCL, or to OpenTofu if you depend on CDKTF-style tooling from the community.
What Are the Most Common Terraform Mistakes?
One giant root module. Plans take minutes, every change risks everything, and nobody can own a piece of it.
State in Git or on a laptop. Secrets leak, and two people applying at once corrupt the state.
Unpinned providers. A new major provider version changes behavior on a Monday morning with no code change on your side.
Applies from workstations. No audit trail, and no guarantee that what was reviewed is what was applied.
Ignoring drift. Manual console fixes pile up until the next apply reverts them during an incident.
Secrets in state. Use ephemeral resources and write-only arguments where providers support them.
Frequently Asked Questions
Is Terraform still free to use?
Yes, for managing your own infrastructure. The Business Source License allows production use; its restriction covers offering Terraform to others in a way that competes with IBM's paid versions. HCP Terraform has a free tier for up to 500 managed resources.
What is the latest version of Terraform?
Terraform 1.17.2, released November 11, 2026. The 1.17 line added variables and locals in provider requirements, a -minimal-refresh planning option, and general availability of Terraform Policy.
Should you use workspaces or separate directories for environments?
We use separate root modules per environment. Each environment gets its own backend key, permissions and pipeline, and differences between environments are visible in code instead of hidden in workspace variables.
Is DynamoDB still needed for Terraform state locking on AWS?
No. Since Terraform 1.11, the S3 backend locks state natively with use_lockfile = true, and DynamoDB-based locking is deprecated.
Is CDK for Terraform still supported?
No. HashiCorp deprecated CDKTF on December 10, 2025 and no longer maintains it.
Need Terraform your team can trust in production?
We set up Terraform and OpenTofu codebases, remote state, CI/CD pipelines and policy checks for US businesses on AWS and Google Cloud. The first 30-minute consultation is free.
Sources
Related Articles
Terraform State Management Best Practices
Backends, locking, workspaces and state migrations.
Terraform DevSecOps with Checkov and Trivy
Security scanning for Terraform in the pull request.
AWS Landing Zone with Terraform
Multi-account AWS foundations built in code.
CloudFormation to Terraform Migration
Moving existing stacks without downtime.
See also: DevOps & cloud services · Enterprise Terraform state management
Services
Subscribe to our newsletter
Get monthly email updates about improvements.
© 2026 HostingX Solutions LLC. All Rights Reserved.
LLC No. 0008072296 | Est. 2026 | New Mexico, USA
Terms of Service
Privacy Policy
Acceptable Use Policy