OVERVIEW OF TERRAFORM WORKSPACES:

Terraform Workspaces is a feature in Terraform that allows you to create multiple environments or instances of your infrastructure within a single Terraform configuration. It enables you to manage different sets of resources or configurations for different stages of your development workflow, such as development, staging, and production. Each workspace maintains its own state, which allows you to isolate and manage the resources independently.

BENEFITS OF TERRAFORM WORKSPACES:

  • Isolation: Workspaces provide isolation between different environments, ensuring that changes in one workspace do not affect others. This separation is particularly useful for managing different stages of your infrastructure lifecycle.
  • Collaboration: Workspaces enable multiple team members to work on different environments simultaneously without interfering with each other. Each member can have their own workspace for development, testing, or experimentation.
  • Configuration Reusability: You can reuse Terraform modules across different workspaces. This allows you to define common infrastructure components once and use them in multiple environments, promoting consistency and reducing duplication.
  • State Management: Each workspace has its own state file, which allows you to track and manage the state of resources separately. This prevents accidental changes or conflicts between different environments.

EXAMPLE USAGE:

Let’s assume you have a Terraform configuration for managing Azure infrastructure that consists of a virtual network, subnet, and virtual machine. Here’s how you can use workspaces:

Creating a Workspace:

$ terraform workspace new dev
 

This command creates a new workspace named “dev.” By default, Terraform starts with a default workspace called “default.” You can switch between workspaces using the terraform workspace select <workspace-name> command.

Defining Resources:

In your Terraform configuration, you define the resources you want to create within the appropriate blocks. For example, you might have a main.tf file that includes the following code.

This configuration will create a resource group, virtual network, subnet, nic, and VM when you apply it:

variable “prefix” {
    default = “iactest”
}
resource “azurerm_resource_group” “example” {
    name = rg-${var.prefix}-example
    location = Central US
}
resource “azurerm_virtual_network” “main” {
    name = ${var.prefix}-network
    address_space = [10.0.0.0/16]
    location = azurerm_resource_group.example.location
    resource_group_name = azurerm_resource_group.example.name
}
resource “azurerm_subnet” “internal” {
    name = internal
    resource_group_name = azurerm_resource_group.example.name
    virtual_network_name = azurerm_virtual_network.main.name
    address_prefixes = [10.0.2.0/24]
}
resource “azurerm_network_interface” “main” {
    name = nic-${var.prefix}-vm01
    location = azurerm_resource_group.example.location
    resource_group_name = azurerm_resource_group.example.name
    ip_configuration {
        name = vm01-nic-ip
        subnet_id = azurerm_subnet.internal.id
        private_ip_address_allocation = Dynamic
    }
}
resource “azurerm_virtual_machine” “main” {
    name = vm-${var.prefix}-vm01
    location = azurerm_resource_group.example.location
    resource_group_name = azurerm_resource_group.example.name
    network_interface_ids = [azurerm_network_interface.main.id]
    vm_size = Standard_DS1_v2
    storage_image_reference {
        publisher = Canonical
        offer = UbuntuServer
        sku = 16.04-LTS
        version = latest
    }
    storage_os_disk {
        name = myosdisk1
        caching = ReadWrite
        create_option = FromImage
        managed_disk_type = Standard_LRS
    }
    os_profile {
        computer_name = hostname
        admin_username = exampleadmin
        admin_password = Password123@!
    }
    os_profile_linux_config {
        disable_password_authentication = false
    }
}

 

Applying Changes:

When you’re ready to apply changes to a specific workspace, use the terraform apply command along with the -var-file flag to provide any necessary variables.

This command applies the changes defined in the Terraform configuration to the “dev” workspace, using variables from the “dev.tfvars” file:

$ terraform apply var-file=dev.tfvars

 

Switching Workspaces:

You can switch between workspaces using the terraform workspace select <workspace-name> command. For example, to switch to the “staging” workspace:

$ terraform workspace select staging

 

FINAL THOUGHTS:

By utilizing Terraform workspaces, you can manage and deploy infrastructure changes separately for each environment, ensuring better organization, collaboration, and resource isolation.

The Terraform workspaces feature is a great first step into creating more efficient infrastructure as code. If you’re just getting started on your journey into infrastructure as code, follow this link to get in contact with one of our experts.

We’ve helped a variety of organizations get started the right way on their journey into infrastructure as code.

For more technical how-to’s click here.