Terraform Home Lab for Beginners.

Md RaselMd Rasel
3 min read

This writing is for those who wants to practice terraform but they are unable to afford any cloud environment. Many of us also wants to practice the terraform without any hassle and free of cost. so this article is based on that. If yo have a windows machine and enough core and memory to run virtual machine you can follow the instructions.

We can run our test terraform code into our home lab using windows pc with virtualbox. We can create update and manage our virtual box vm with terraform by using some provider. Let’s talk about the process.

Step 1: Virtualbox Installation on windows

Ensure that VirtualBox is installed on your Windows system. You can install virtualbox by downloading from bellow link.

Oracle VirtualBox

Step 2: Install Terraform:

Download and install Terraform for Windows from the official Terraform

website

Step 3: setting up terraform environment

To run the terraform code from any directory of your machine you need to set the system path of terraform.

Steps to add Terraform to the system PATH:

  1. Open System Properties:

    • Right-click on This PC or My Computer and click Properties.

    • Click Advanced system settings on the left side.

    • In the System Properties window, click on the Environment Variables button.

  2. Add Path to Environment Variables:

    • In the Environment Variables window, under System variables, scroll down and find the Path variable. Select it and click Edit.

    • Click New, and then enter the path to your Terraform executable: E:\My Personal\terraform_1.9.7_windows_386.

    • Click OK to close all the windows.

  3. Verify:

  • Open a new Command Prompt or PowerShell and type:

      terraform --version
    
💡
Now before running our first terraform code we need a provider for VirtualBox terraform. Here i am using terra-farm/provider for my practice.

Download the terra-farm provider and extract it on the windows machine. now we need to configure the path of terra-farm on window.

Now we are ready for our first Terraform code running on windows to create virtual machine on VirtualBox.

Create a directory, under that create a file called main.tf

terraform {
  required_providers {
    virtualbox = {
      source = "terra-farm/virtualbox"
      version = "0.2.2-alpha.1"
    }
  }
}

# There are currently no configuration options for the provider itself.

resource "virtualbox_vm" "node" {
  count     = 2
  name      = format("node-%02d", count.index + 1)
  image     = "https://app.vagrantup.com/ubuntu/boxes/bionic64/versions/20180903.0.0/providers/virtualbox.box"
  cpus      = 2
  memory    = "512 mib"
  #user_data = file("${path.module}/user_data")

  network_adapter {
    type           = "bridged"
    host_interface = "Intel(R) Wireless-AC 9560 160MHz"
  }
}

output "IPAddr" {
  value = element(virtualbox_vm.node.*.network_adapter.0.ipv4_address, 1)
}

output "IPAddr_2" {
  value = element(virtualbox_vm.node.*.network_adapter.0.ipv4_address, 2)
}

Steps to Proceed:

  1. Adjust the host interface: Make sure that the correct bridge adapter is used, such as "Intel(R) Wireless-AC 9560 160MHz".

Run Terraform:

  • Initialize Terraform:
terraform init
  • Check the plan:
terraform plan
  • Apply the configuration:
terraform apply

Hope this will help you to run the terraform on windows for VirtualBox. Setup your home lab for Terraform and enjoy the learning.

0
Subscribe to my newsletter

Read articles from Md Rasel directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Md Rasel
Md Rasel