Preventing Terraform state conflicts when deploying multiple VMs

Published on 04 May 2026 by Adam Lloyd-Jones

If you use the same state file for every pipeline run, yes, Terraform will attempt to destroy or modify the first VM to make it match the second one.

This happens because Terraform is a declarative tool; the state file acts as its “memory” of what it is responsible for. If your code defines one VM and your state file records one VM, running the pipeline with a new name tells Terraform: “The existing VM should now have this new name,” which often triggers a replacement (destroy then create).

To manage multiple VMs independently using the same code, you must ensure each VM has its own isolated state. Your proposed approach of using a dynamic state file per VM is correct and is a standard industry practice often achieved through Partial Configuration or Workspaces.

1. The Right Approach: Partial Backend Configuration

Your idea of using vm-$(vm_name).tfstate is highly effective for CI/CD pipelines. This is known as partial configuration.

In this setup, you leave the backend configuration “empty” in your .tf files and provide the specific details at runtime during the terraform init phase.

Why this works: This forces Terraform to look at a unique state file for every VM name. When the pipeline runs for “VM-B,” Terraform will see an empty state file (since vm-B.tfstate doesn’t exist yet) and create it from scratch without ever knowing “VM-A” exists in a different state file.

2. Alternative: Terraform Workspaces

Terraform has a built-in feature called Workspaces designed specifically for this “one codebase, many deployments” scenario.

3. Managing “Sync” Fear (Idempotency)

You mentioned concerns about Terraform trying to “sync” the state. In Terraform, this is actually a feature called idempotency.

Summary and Best Practices

Partial backend configuration is a specialized technique in Terraform that allows you to omit some or all configuration parameters from a backend block within your .tf files, providing them instead at runtime during the initialization phase. This method is essential for creating flexible, secure, and reusable infrastructure modules, as Terraform’s backend block does not natively support the use of variables or references.

Core Purpose and Benefits

The primary reasons for utilizing partial backend configuration include:


Implementation Methods

To implement this, you first define a terraform block with a backend block that is either entirely empty or only contains non-sensitive, static parameters.

# main.tf
terraform {
  backend "s3" {
    # Leave dynamic or sensitive settings out of this block
  }
}

You then provide the missing configuration values during the terraform init phase using the -backend-config flag. There are two primary ways to pass these values:

1. Using External Configuration Files

You can create a separate file (e.g., backend.hcl, prod.tfbackend, or backend.tfvars) containing the specific key-value pairs required by your backend.

2. Using Command-Line Flags

Parameters can also be passed directly as individual key-value pairs on the command line.


Important Considerations

In the context of Infrastructure as Code (IaC), workspaces serve as isolated environments that allow teams to manage multiple instances of the same infrastructure codebase. While the term is most prominently associated with Terraform, other tools like Pulumi also utilize the workspace concept to facilitate collaboration and environment separation.

Terraform CLI Workspaces

Terraform CLI workspaces allow a developer to use the same configuration files to build multiple, isolated environments, each with its own state file. By default, every Terraform project starts with a single workspace named default.

Mechanics and State Isolation

Common CLI Commands

Workspaces are managed through several subcommands:

Pros and Cons for Production


2. HCP Terraform (Cloud) Workspaces

Workspaces in HCP Terraform (TFC) are fundamentally different from CLI workspaces. While CLI workspaces share code but have separate states, TFC workspaces are centralized logical containers that manage code, variables, state, and permissions for a specific component.


3. Pulumi Workspaces

Pulumi utilizes the workspace concept to foster team collaboration by creating isolated units within a project.


4. Other Tools: SaltStack Environments

While not called “workspaces,” SaltStack achieves similar goals through Environments defined in the file_roots parameter.

Related Posts

Adam Lloyd-Jones

Adam Lloyd-Jones

Adam is a privacy-first SaaS builder, technical educator, and automation strategist. He leads modular infrastructure projects across AWS, Azure, and GCP, blending deep cloud expertise with ethical marketing and content strategy.

comments powered by Disqus

Copyright 2026. All rights reserved.