Podcast about DevSecOps

#43 - Terraform 1.0 to 1.3.0. One year in review

Module upgrades got easier, but state files still exposed database passwords.

18 min read

We are discussing what has happened in Terraform world since the 1.0 release last year and if there are new features worth mentioning, trends in Terraform development, etc. As well as doing a recap of the road to 1.0 and how long it took us to get there.

Discuss the episode or ask us anything on LinkedIn


Terraform upgrades used to be a project

Imagine a repository somewhere in your organisation that still runs Terraform 0.11, where every engineer who opens it decides today isn’t the day. That picture is hypothetical, but the version spread behind it isn’t: we found a 0.11 configuration sitting in a company’s repos not that long ago. Across what we manage there should be no 0.11 left, and there could still be configurations on 0.12 through 0.15. That isn’t neglect. It’s what the 0.x upgrades cost: a language rewrite in 0.12, a new provider namespace in 0.13, a dependency lock file in 0.14, each one landing on infrastructure that was already running and already load bearing. For a stack that existed before you started, the migration was sometimes more work than deleting the thing and building it again.

Terraform 1.0 arrived on 8 June 2021 with a promise attached: upgrades inside the 1.x line leave your workflow alone, and every 1.x release gets at least 18 months of maintenance. A year later that promise has held through two minor releases, and the upgrade is a package manager command rather than a sprint.

So the question isn’t whether to move to 1.x. It’s what the year actually bought you, and what it left untouched. The short answer on both: nearly all of the new language features are aimed at the people who write modules rather than the people who call them. Nothing in the year addressed the state file, which records managed resource attributes in plain text, database passwords included, for anyone who can read the bucket.

Nearly seven years to 1.0

The 1.x releases look quiet next to what came before them, so start with the road behind.

Why did Terraform take nearly seven years to reach 1.0?

Because the configuration language and the plugin model were both replaced underneath everyone who was already using them, and each replacement needed an operator to drive it through infrastructure that was already running.

The first tag is dated 28 July 2014 and supported AWS and DigitalOcean. v0.7.0 came in August 2016, and v0.11.0 on 16 November 2017. That one held for eighteen months, after a run of much faster releases before it, which is why it reads as the first version that was genuinely stable rather than a step on the way. Then v0.12.0 on 22 May 2019 brought HCL2: first-class expressions, for expressions that build a list or map out of another one, and dynamic blocks that generate repeated nested blocks inside a resource. Iterating over the resource itself came later in the same line, with v0.12.6 on 31 July 2019, whose changelog line is one sentence: “Resources can now use a for_each meta-argument”. Iterating over a module waited for v0.13.0 in August 2020, which added count and for_each to module blocks. v0.14.0 in December 2020 added the dependency lock file, and 1.0 followed six months after that.

Credit where it’s due: for a migration of that size, HashiCorp handled it better than we expected. The v0.12.0 release notes point at an upgrade guide with a tool “to automatically upgrade many improved language constructs” that also indicates “situations where human intuition is required to complete the upgrade”. The tool did the mechanical rewriting; a person still had to run it per configuration, settle what it flagged, and land the result on infrastructure already serving traffic. It wasn’t painless, but we have come through worse. The other thing that changed along the way is harder to measure. On 0.11 we would hit “I wish I could express this and I can’t”. We can’t remember the last time we said it. HCL isn’t Turing complete and isn’t trying to be, and for describing infrastructure declaratively that ceiling now sits well above what most configurations need.

Pinning providers is the part of 1.0 that still pays

The change that affects your week most isn’t in the 1.0 release notes at all. It’s the plumbing that went in just before.

Why do you need a lock file when you can already pin a version?

Because most constraints are ranges rather than exact pins, and because the lock file also records the checksum of the build you installed.

Terraform core doesn’t know how to talk to AWS. It stopped knowing in v0.10.0, August 2017, when providers left the main distribution and became plugins that terraform init downloads. Every provider now moves on its own release cycle, which is why required_providers gives you package manager style constraints: this AWS provider but no higher than that, this Kubernetes provider between these two versions.

A range leaves the version to install day. With no lock file entry, terraform init takes “the newest available version that matches the given version constraint”, so ~> 4.0 can land a different build next week. An exact pin, = 4.0.0, settles the version and says nothing about the package you got. Since 0.14, terraform init writes .terraform.lock.hcl into the configuration directory, with the selected versions and their checksums. Later runs re-select the same versions until you ask for new ones with terraform init -upgrade. Check that file in. It’s the same bargain as an npm lock file, with the supply chain benefit that a provider package whose contents changed fails verification instead of quietly running. Terraform 1.1 extended the same checksum scheme to terraform apply with a saved plan file, so the plugins that produced the plan are verified before they’re used to apply it.

State is what lets Terraform delete things

The lock file settles which code runs. State settles what that code is allowed to do.

Why does Terraform keep a state file at all?

Because it has to know what it created last time, so a plan can compare three things: what you wrote, what’s in the cloud, and what Terraform did before.

The configuration management generation didn’t keep that kind of record. Chef, Puppet, SaltStack and Ansible re-assert a desired state on every run and assume each step is idempotent, so a run that dies in the middle leaves a host half configured. They do record the run: Ansible writes to a log on the control node when you set log_path, and Tower keeps job history per host, project and inventory. What none of them keeps is a mapping from a line of configuration to the object it created, so the next run inspects the host and works the state out again. We sat through an OpenShift demo at KubeCon where the Ansible install ran beautifully, with every preflight check switched off for the occasion. Running it with the checks on was a different afternoon entirely.

None of those tools are gone. They’re where the hardware is. The future isn’t evenly distributed, and on physical servers in your own racks they still do the job. What you rarely see now is a new team on a cloud account reaching for them first.

A resource mapping changes what deletion means. Delete a resource block and the object actually goes away, because the state remembers that Terraform made it. Ansible will happily destroy things too: its ec2 module is documented as “create, terminate, start or stop an instance in ec2”, and state: absent terminates. The difference is what has to be written down. There you delete by writing a task that says delete; in Terraform you delete by removing the block, and the state is what connects that absence to an object still running in your account.

Import exists because discovery can’t be solved in general

The same file explains the first thing that irritates everyone who arrives with infrastructure already running.

Why can’t Terraform just adopt the database that’s already there?

Because matching a block of configuration to a cloud object needs a unique identifier, and for most resource types nothing in your configuration is one.

A database instance has a name you chose, so a match is at least plausible. A security group has sg-0a1b2c3d, assigned by AWS after creation, and nothing you wrote predicts it. Even when every attribute lines up, you can’t be certain you’ve found the object you meant rather than its twin. So you run terraform import, which the CLI documentation is blunt about: it “can only import resources into the state” and “does not generate configuration”, so you write the resource block yourself first. Solving the general case would be a real step forward for infrastructure as code, and we wouldn’t bet on it arriving soon.

The flip side of a state file is that it’s a JSON document, and terraform show -json prints state or a saved plan in a documented format. Once your infrastructure has a machine readable description in one place, you can answer “how many instances are we running” without walking five cloud APIs, and a whole generation of scanners reads that JSON to find misconfiguration. The trust boundary is worth naming: the file describes what Terraform believes, which matches the cloud only as recently as the last run.

What 1.1 and 1.2 shipped, and who they shipped it for

With the foundation set, the releases since 1.0 have been small, deliberate and pointed at a particular audience.

What did Terraform 1.1 and 1.2 actually add?

moved blocks, precondition and postcondition checks, replace_triggered_by, a native cloud block, and plan output that explains itself better. Most of it lands with module authors.

Renaming a resource inside a module used to be a destructive act. Terraform saw the old address disappear and a new one appear, proposed a destroy and a create, and every consumer had to run terraform state mv by hand first. Since 1.1, 8 December 2021, the module author writes a moved block and Terraform migrates the existing object to its new address during planning. The same release made plan and apply explain why they propose a deletion, so a destroy caused by lowering a count now says so.

Terraform 1.2, 18 May 2022, carried on in the same direction. precondition and postcondition blocks let a module state its assumptions about the world around it, that CloudTrail is already enabled, say. When you hear about a failure depends on the data, not on which block you used. The documentation says Terraform “evaluates custom conditions as early as possible, but must defer conditions that depend on unknown values until the apply phase”. So a postcondition whose inputs are all known is checked during planning, before anything is created, even though the docs describe postconditions as running “after planning and applying changes”. Only when the condition reads a value that isn’t known until apply does the check move, and then “a failed postcondition will halt processing after Terraform has already implemented these actions”, which it does not undo. A precondition is checked before the object it belongs to, and during apply a failed one “will prevent Terraform from implementing planned actions for the associated resource”.

replace_triggered_by joined create_before_destroy, prevent_destroy and ignore_changes in the lifecycle block, replacing an object when an upstream dependency changes. depends_on is a separate meta-argument, not a lifecycle argument.

One thing to watch while you upgrade. The v1.1.0 release notes open by telling you to leave 1.1.0 and 1.1.1 behind, because a failure to build the apply-time graph there “can cause Terraform to incorrectly report success and save an empty state”. An empty state is Terraform forgetting your infrastructure. Later patches fix it, and the compatibility promise covers the language rather than the bugs.

Your state file holds plaintext secrets

That’s the good news from a year of releases. Here’s what a year of releases didn’t touch.

What’s actually inside your state file?

Every attribute of every resource Terraform manages, including the ones you marked sensitive, in plain text. For a database that includes the password you passed in.

HashiCorp’s own documentation says it without flinching. With local state, it is “stored in plain-text JSON files”, and “for resources such as databases, this may contain initial passwords”. Marking a variable sensitive doesn’t change that: it suppresses the value in CLI output, and “Terraform will still record sensitive values in the state”.

A remote backend improves the transport and the storage, not the audience. The S3 backend encrypts at rest when you enable the encrypt option. Terraform Cloud always encrypts state at rest, and holds it in memory only while a run needs it. Both decrypt it for anyone the backend lets in. Who that is on S3 takes several policies to answer. Inside a single account, an identity policy and the bucket policy are a union, so either grants s3:GetObject on its own. The other policy types AWS checks can only subtract. An explicit deny in either policy stops the read. So can a service control policy on the account, a permissions boundary on the principal, or a session policy passed when the role was assumed.

Read the bucket policy’s Principal before you count those last two as restrictions. Where it names a role session ARN, the grant goes to the session itself. AWS says such a grant is “not limited by an implicit deny” in a permissions boundary or a session policy. An explicit deny in either still stops the read. SSE-KMS adds a separate check, kms:Decrypt on the key, which the key policy controls. A key policy can refuse a read S3 was willing to serve. A pair of allows gives you a shortlist, not effective access.

Since everyone who runs terraform apply needs the state, the lever you have is the size of that list. Move applies into something that runs them for you, Terraform Cloud, Atlantis or the Jenkins you still have, and the humans with bucket access become the humans who can change the pipeline. Put access logging on the bucket so an unexpected read is visible. It’s a smaller list rather than a solved problem, and worth checking who is on yours today.

Switching to Pulumi, a CDK or GitOps

Which raises the obvious question, since other tools made different choices here.

Should you move off Terraform to fix this?

Only if the replacement is roughly five times better, because the switch costs you everything that currently works.

Pulumi earns the comparison, and on this specific problem it’s ahead. Secret values are encrypted before they reach the stack state. Its documentation says “encryption ensures that these values never appear as plain text in your state file”. The keys are per stack, from Pulumi’s own service or one you control in AWS KMS, Azure Key Vault, Google Cloud KMS or Vault. Pulumi walked through the AWS KMS version in May 2020, where an exported stack shows ciphertext where the secret would be. What comes with it is a different proposition: infrastructure in a general purpose language. Some teams want exactly that. Others look at HCL, a constrained declarative language whose iteration stops at for_each and expressions, and see the lower barrier for everyone who has to read the code at 3am. The CDKs sit in much the same place, with the same trade and little else on top.

That’s the whole argument for Terraform being a safe bet right now, and it’s a weaker argument than it sounds. It isn’t that nothing better is possible. It’s that nothing on the table clears the bar for ripping out something that works, and incremental improvements on top of Terraform’s own model, Pulumi included, inherit most of what you’d be trying to leave. Maybe GitOps eats the category next. We’ll believe it when the migration is cheaper than the gain.

Coming in 1.3, and five backends already on notice

Two more changes point where the rest of the year pointed: one feature for module authors that is still in alpha, one piece of housekeeping that has already shipped.

The 1.3 line is already in alpha, and its notes carry the feature we’re waiting for: an optional() modifier on object type constraints, with a default value.

variable "with_optional_attribute" {
  type = object({
    a = string                # a required attribute
    b = optional(string)      # optional, null when omitted
    c = optional(number, 127) # optional, 127 when omitted
  })
}

Pass an object into a module today and you supply every attribute of it, opinion or not. The modifier does exist already, as an experiment you opt into with experiments = [module_variable_optional_attrs], without the default value, and the docs warn it can break even in a minor release.

The housekeeping is out already: 1.2.3, on 15 June 2022, marked the artifactory, etcd, etcdv3, manta and swift backends deprecated and planned for removal in a later release. We have never seen any of them in use, Artifactory aside, which is how a deprecation like this gets missed. Move the state while you’re still on a version that can read it.

What this means for teams

The 1.x line has made Terraform boring to run, which is the highest praise infrastructure tooling gets. The work left is yours, not HashiCorp’s.

This week:

  • List every Terraform version in your repositories, from required_version and your CI images. Anything on 0.x is upgrade debt with a date on it. From 0.14 or 0.15 you can go straight to the latest 1.0; below that, take the minors in order.
  • Check that .terraform.lock.hcl is committed in each configuration, and that nobody’s CI runs terraform init -upgrade on every build, which quietly hands your provider selection back to whatever is newest.
  • Grep your backend blocks for artifactory, etcd, etcdv3, manta and swift, and plan the move while you’re still on a version that supports them.
  • Work out who can really read your state objects. That takes every policy in the request, not two of them. Read the identity policies and the bucket policy together, then look for an explicit deny in either. Check the account’s service control policies, the principal’s permissions boundary and any session policy your roles pass. Read the bucket policy’s Principal before you treat those last two as limits. A grant to a role session ARN escapes them unless the deny is explicit. If the bucket uses SSE-KMS, kms:Decrypt on the key is a separate question. Whoever is still standing after all of that can read your database passwords. Decide whether that list should be shorter.

Common questions, answered

How do I keep a database password out of Terraform state?

You can’t keep it out, so shrink the set of principals who can read the state. Terraform records every attribute it manages, database passwords included, and sensitive = true only hides the value in CLI output. Enable encrypt on the S3 backend, then work out who can really read that object. An identity policy or the bucket policy can each grant s3:GetObject alone. An explicit deny, a service control policy, a permissions boundary or a session policy can each take it away. Read the bucket policy’s Principal first: a grant to a role session ARN escapes a permissions boundary or a session policy, though an explicit deny still applies. Move applies into a pipeline so engineers don’t need the bucket.

How do I upgrade from Terraform 0.11 to 1.x?

Work up one minor at a time to 0.14, then go straight to the latest 1.0. HashiCorp’s v1.0 upgrade guide says you can “upgrade directly from Terraform v0.14 if you wish”, provided you read the v0.15 guide too. Below 0.14 the order isn’t optional: the 0.14 release notes say the terraform 0.13upgrade command is gone and the v0.13 upgrade must be done first. Budget real time for 0.12, the HCL2 rewrite, and 0.13, which changes provider naming. For a small stack nobody changes, a rebuild sometimes beats the migration.

Is it safe to upgrade within Terraform 1.x?

Yes, that’s what the 1.0 compatibility promises commit to: configurations and workflows that work on one 1.x release keep working on the next, with at least 18 months of maintenance each. Take the latest patch of a minor line, not the .0. Terraform 1.1.0 and 1.1.1 could save an empty state after a failure, which a language promise doesn’t cover. Read the upgrade notes anyway: the promises allow narrow exceptions.

Resources

Since we recorded

Terraform 1.3 shipped on 21 September 2022 with the optional() defaults described above, and it removed the five deprecated backends rather than only warning about them. In August 2023 HashiCorp moved Terraform to the Business Source License, the community forked it, and we covered the fallout in HashiCorp’s BSL move and OpenTF and the practical migration in From Terraform to OpenTofu. The state secret problem has since been answered twice, in different ways: OpenTofu 1.7, April 2024, added end-to-end encryption of the state file itself. Terraform went the other way in 1.11, 27 February 2025, pairing ephemeral values with write-only arguments, whose values “are never persisted in Terraform’s plan or state files” for the resource attributes that support them.