Using workspaces to manage environments
Note
The code for this section is under the chap05/workspaces
directory in the GitHub repo of this book.
Workspaces allow us to have multiple independent state files for a single configuration. Thus, we can have one state file for our development project and a second one for production. By itself, that might appear to be very limited. However, combined with the effective use of variable definitions files (.tfvars
) and Google projects, this can be an effective method to manage multiple environments.
Let’s see how we can accomplish this. Using the sample code at chap05/workspaces, run terraform init and terraform apply
like normal. Then, have a look at the state file using terraform
state list
:
$ terraform init $ terraform apply $ terraform state list
We can see the three servers and their static IP addresses in the state file.
Next, create a new workspace named prod
using the terraform workspace new prod
command. The terraform...