# 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

***


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://notes.nomanaziz.me/devops/infrastructure-as-a-code-iac/terraform/13.-managing-multiple-environments.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
