Setting Up Terraform Workspaces for Multi-Account AWS Deployment

106 views Asked by At

I am currently using Terraform Cloud, and my project has the following structure:

.
├── backend.tf
├── dev.tfvars
├── outputs.tf
├── providers.tf
├── prod.tfvars
├── variables.tf
└── vpc.tf
terraform {
  cloud {
    organization = "theorganization"
    workspaces {
      name = "dev"
    }
  }
}

I'm seeking guidance on how to maintain two separate states (or workspaces) without modifying the existing structure. Specifically, I want to execute tf apply -var-file=dev.tfvars to apply changes to one AWS account and tf apply -var-file=prod.tfvars to apply changes to a different AWS account, ensuring that each has its own distinct state. Any insights on achieving this would be greatly appreciated.

Using different directories is not an option. It's likely that a modification needs to be made in backend.tf, but I'm struggling to grasp the underlying logic

1

There are 1 answers

0
Math On BEST ANSWER

I found an answer for my question in the documentation:

https://developer.hashicorp.com/terraform/cli/workspaces

Interactions with Terraform Cloud Workspaces

Terraform Cloud organizes infrastructure using workspaces, but its workspaces act more like completely separate working directories. Each Terraform Cloud workspace has its own Terraform configuration, set of variable values, state data, run history, and settings.

When you integrate Terraform CLI with Terraform Cloud, you can associate the current CLI working directory with one or more remote Terraform Cloud workspaces. Then, use the terraform workspace commands to select the remote workspace you want to use for each run.