> For the complete documentation index, see [llms.txt](https://notes.nomanaziz.me/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://notes.nomanaziz.me/devops/infrastructure-as-a-code-iac/terraform/13.-managing-multiple-environments.md).

# 13. Managing Multiple Environments

### Approaches

***

#### 1. Workspaces

Multiple named sections within a single backend

```sh
$ terraform workspace new dev
$ terraform workspace list
$ terraform workspace select staging
```

**Pros**

* easy to get started
* convinient terraform.workspace expression
* minimizes code duplication

**Cons**

* prone to human error
* state stored with same backend
* codebase doesn't unambigously show deployment configurations

***

#### 2. File Structure

Directory layout provides seperation, modules provide reuse

```
_modules
  module_1
    main.tf
  module_2
    main.tf

dev
  main.tf
  terraform.tfvars

prod
  main.tf
  terraform.tfvars
```

**Pros**

* isolation of backends
  * improved security
  * decreased potential for human error
* codebase fully represents deployed state
* referencing resources acrodd configurations is possible using `terraform_remote_state`

**Cons**

* multiple terraform apply required to provision environments
* more code duplication, but can be minimized with modules

***
