From the course: Introduction to Terraform on Azure
Using Terraform workspaces
From the course: Introduction to Terraform on Azure
Using Terraform workspaces
- [Instructor] Imagine you've just finished setting up your Terraform configuration for your virtual machine resource. It works great with your current setup in the dev environment, but now your manager has asked you to deploy identical VM infrastructure for staging, and eventually for the product environment for live workloads. Let's work through creating a complete multi environment setup using workspaces with the configuration we've been working on. Back in your IDE, click at the terminal menu, and then select New Terminal. Now let's see what workspaces are available, and then create new ones. So in your terminal, run Terraform workspace list, and Enter. I have just the default workspace. Now let's run the following commands to create dev staging and prod workspaces. Terraform workspace new dev, terraform workspace new staging, terraform workspace new prod. Now to switch between workspaces, you run terraform workspace, select dev to select the dev workspace. You can also run terraform workspace select staging, and terraform workspace select prod. If you want to know the current workspace you're in, you run terraform workspace show. And you should see the name of your workspace. To delete a workspace, you must ensure that the workspace is empty, meaning there is no state file, and it's not the current workspace before you run terraform workspace delete, for example, staging. The key to effective workspace usage is making your configuration respond intelligently to the current workspace. So let's modify some of our configuration to work seamlessly across environments. In the main dot TF file, let's modify the resource group to use terraform dot workspace. So where we have the resource group block, I'll delete this, and type an open quote RG dash to the string interpolation, terraform dot workspace dash string interpolation again, and this time we would use the project name, so var dot project name. Awesome. Let's also update the environment to use terraform dot workspace. So where you have the tag, I'll delete dev, and type terraform dot workspace. Let's also modify the name of the virtual network resource block. I'll delete Vnet terraform demo, and use the string interpolation. Terraform dot workspace, and then let's add the project name to make this specific. Var dot project name. Let's also define environment specific sizing options using locals. Locals are named values that you can define once, and reuse throughout your configuration. Think of them as constants that help you avoid repetition, and make complex expressions more readable. Unlike variables which are imputes to your configuration, locals are computed values that are calculated from other variables, data sources, or expressions. So let's define locals at the top before the resource group block. So up here, after the provider block, you type locals, and in here, we'll type environments, underscore config, default, open coily brace, and we'll have VM underscore size. Press the Tab two times, and then open quote, standard underscore, capital B one S, close quotes. The VM underscore count, press Tab twice after the equal sign, and we'll give it the value of one. Let's create locals for the dev environment. So dev, Tab, equal, Tab, curly brace, VM underscore size, two tabs and then two tabs again, standard underscore B one S. VM count one tab, and this will be one. Let's also create the locals for prod, and we'll have VM size two tabs, double quote, standard underscore, capital D, capital S one, underscore V two and close quote. The VM count, let's give this the value of two. All right, now let's add a current config, local dot environment underscore config, And let's open bracket terraform dot workspace. Now let's create a subnet for a virtual network. So after the virtual network resource block, create a new line, and type resource Azure RM underscore subnet, and I'll name this "main". Enter. The name of the resource will be subnet dash, let's use a string interpolation, terraform dot workspace, and then dash string interpolation again, variable dot project name. The subnet would need the resource group, so let's add Azure RM resource group dot main, dot name. It also needs a virtual network name, so it's reference the virtual network dot main, dot name. It also needs an address prefixes, so it would add var dot subnet address prefixes. Now let's create a network interface card, since that is needed for a VM. So create a resource block. Let's name this Azure RM underscore, network underscore interface. I'll name this as "main", press Enter, and the count will be local dot current config. Remember the current config we defined in locals, dot VM counts. The name, open quote, NIC dash let's use a string interpolation to specify terraform dot workspace so that this is environment specific. Then dash string interpolation again, var dot project name, and let's add the dash again. This time, the string interpolation would be to use the count dot index plus one so that we don't have a clash in the name. Now let's add location, and would use the resource group's location. The (indistinct) needs a resource group name, so we'll reference the Azure RM resource group dot name. Let's define IP configuration. It needs a name, so we type the name key, and the value will be quote IP config dash, again, the string interpolation, terraform dot workspace, dash string interpolation again, var dot project name, and a string interpolation again, count dot index plus one. Press Enter, and let's create the subnet ID. This will reference the subnet dot main, dot ID. Now let's create the private IP address location. I would assign this dynamic. Let's also add tags equal merge, then bracket var dot tags, brace environment equal terraform dot workspace, and then project code if you want to add that. Awesome. We've created most of our multi environment set up with workspaces. Next, we will see how to create a resource block for the Azure virtual machine, and we will continue that in the next video.
Contents
-
-
-
-
-
(Locked)
What are workspaces?5m 43s
-
Using Terraform workspaces11m 21s
-
(Locked)
Define and provision Azure VM with workspaces10m 28s
-
(Locked)
Understanding Terraform state (local vs. remote)5m 6s
-
(Locked)
Configuring remote state using Azure Blob Storage11m 14s
-
(Locked)
Implementing state locking and versioning10m 25s
-
(Locked)
-
-
-