Using Terraform WorkSpaces

      No Comments on Using Terraform WorkSpaces

Terraform is a great tool. I am always finding ways that the application can help the flow of work.In this instance I found myself wondering was it possible to run the same code onto more than one environment without having multiple copies of the code. Reasoning behind this is we all have the issue of multiple environments such as Dev, Test and of course Production to deal with, having to copy the code three times usually, this introduces the chance of the wrong version of code being used and all the head aches that come with that. What if you could run all this from one folder, this would guarantee the code was the same when it was ran. Workspaces are the answer to this, easy to set up and easy to move between. Allowing the flow of work to move along quite nicely.

Now I know more than one of the sys admins reading this is thinking “Now hang on a minute if its easy to run new unproven code in Prod thats a big risk”. I totally agree, I think new code could be easily ran. So maybe production is not the ideal example but what workspaces do is give you freedom to change variables to use with the same set of code and to easily move between the created environments. There is never only one development environment let alone all the different areas of test. Having the ability to spin up an exact copy of an environment without too much work is a definite plus for all developers and testers. Performance testers will especially cash in one this, they could ask for different size servers and/or the number of servers, all easily done by changing the value of variables, the code would have to be set up for this but it is easily done. This way they could quickly see whether it was resources or rather lack of them that was causing issues or if its whatever is being tested. This is something not easily done without the input of technology like terraform and the cloud.

So to hammer this though home and to work through how workspaces are created and utilised, I created a little project of my own. It’s not very big but does prove the point on how workspaces can be quite useful

The project can be found on my git hub here . An explanation is below

Terraform Workspaces
Workspaces in terraform are a great way to use the same code but with different values for your variables, this is a simple example to show they work. Two workspaces are created in this example, creating the workspace is straight forward, in the folder where your terraform code is at the cli, the commands to type are

terraform workspace new “name of the workspace”
eg terraform workspace new prod – creates the workspace prod

In this example we are using the two workspaces “dev” and “prod”

Once created the workspaces can be viewed using the following command
terraform workspace list

a list from my local project

The workspace that is currently being used will have an asterisk against it. To move between the workspaces a select command must be used

terraform workspace select “workspace to be selected”
eg terraform workspace select dev – moves to the workspace dev

The example creates a Resource Group, Virtual Network, Subnet, Appropriate Number of Interfaces and the Appropriate Number of Virtual Machines

Variables
To pass in variables for this example a locals block was created and objects created that contain multiple values

A tip here is that you don’t need to initialise more than once, the .terraform folder will be available for all workspaces.
To run the code to create the two different environments

  1. terraform init # initialise the project
  2. terraform workspace new dev # create the dev workspace
  3. terraform workspace new prod # create the prod workspace
  4. terraform workspace select dev # move into the dev workspace
  5. terraform plan # run plan
  6. terraform apply # apply the code
  7. terraform workspace select prod # move into the prod workspace
  8. terraform plan # run plan
  9. terraform apply # apply the code
the prod environment being created

Two resource groups will be created and the appropriate number of machines built into the RG, in the different locations
terraform.tfstate is kept for both workspaces in a sub folder of the project called terraform.tfstate.d, in this folder is a folder for each workspace with the state and backup files in.

Destroying the resources.
Exactly like actioning the build, you will have to enter each workspace in turn and run the destroy command.Document

Leave a Reply

Your email address will not be published. Required fields are marked *