{"id":3621,"date":"2026-01-14T15:33:10","date_gmt":"2026-01-14T21:33:10","guid":{"rendered":"https:\/\/microsoftgeek.com\/?p=3621"},"modified":"2026-01-14T15:34:32","modified_gmt":"2026-01-14T21:34:32","slug":"build-an-azure-homelab-using-terraform","status":"publish","type":"post","link":"https:\/\/microsoftgeek.com\/?p=3621","title":{"rendered":"Build an Azure Homelab using Terraform"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"820\" height=\"410\" src=\"https:\/\/microsoftgeek.com\/wp-content\/uploads\/2026\/01\/azure-homelab-using-terraform-e1663417328578-820x410-1.png\" alt=\"\" class=\"wp-image-3622\" srcset=\"https:\/\/microsoftgeek.com\/wp-content\/uploads\/2026\/01\/azure-homelab-using-terraform-e1663417328578-820x410-1.png 820w, https:\/\/microsoftgeek.com\/wp-content\/uploads\/2026\/01\/azure-homelab-using-terraform-e1663417328578-820x410-1-300x150.png 300w, https:\/\/microsoftgeek.com\/wp-content\/uploads\/2026\/01\/azure-homelab-using-terraform-e1663417328578-820x410-1-768x384.png 768w\" sizes=\"auto, (max-width: 820px) 100vw, 820px\" \/><\/figure>\n\n\n\n<p>Building an azure homelab can help give you the opportunity to study for certifications or help grow your career as a cloud engineer. Using terraform to build your homelab will allow you to spin up and destroy your environment everyday to save on cost. In this article we will go over the following task to help get your azure homelab built:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create a Microsoft azure account<\/li>\n\n\n\n<li>Install the azure cli<\/li>\n\n\n\n<li>Setup terraform on your pc<\/li>\n\n\n\n<li>Install visual studio code or the editor of your choice<\/li>\n\n\n\n<li>Build your azure homelab using terraform code<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"0-creating-a-microsoft-azure-account\">Creating a microsoft azure account<\/h2>\n\n\n\n<p>First you need to head over to\u00a0<a href=\"https:\/\/azure.microsoft.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">azure.microsoft.com<\/a>\u00a0and signup for a free account. Azure provides a selection of free services forever such as windows servers, linux servers, databases and more. As you go through the initial signup process, you will be asked for a credit card. This will not charge you anything.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-installing-the-azure-cli\">Installing the azure cli<\/h2>\n\n\n\n<p>In order to utilize some of the commands we will need to login to azure from the cli, we need to install the azure cli. Microsoft provides great documentation on all the commands and how to become more familiar with it. Visit their download page&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/cli\/azure\/\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>&nbsp;and download the appropriate package for your computer.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-installing-terraform\">Installing Terraform<\/h2>\n\n\n\n<p>Terraform needs to be downloaded from terraform.io and follow these steps to get terraform to work correctly on your pc:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First unzip the folder<\/li>\n\n\n\n<li>create a folder called terraform on your C: drive<\/li>\n\n\n\n<li>Then create another folder inside the terraform folder called bin<\/li>\n\n\n\n<li>Go into the advanced system settings on your computer if using windows<\/li>\n\n\n\n<li>Select Environment Variables<\/li>\n\n\n\n<li>Then add the path to the bin folder in the user variables.<\/li>\n<\/ul>\n\n\n\n<p>This now allows you to run the terraform command in the command prompt without having to use the absolute path.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-choosing-a-code-editor-of-your-choice\">Choosing a code editor of your choice<\/h2>\n\n\n\n<p>Next choose a code editor of your choice. An editor allows you to easily format code for various programming languages. Here is a list of possible editors of your choice:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>visual studio code<\/li>\n\n\n\n<li>atom<\/li>\n\n\n\n<li>sublime text<\/li>\n\n\n\n<li>notepad++<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4-build-your-azure-homelab-using-terraform\">Build your azure homelab using terraform<\/h2>\n\n\n\n<p>First lets create our terraform folder structure. Create a folder called azurehomelab and inside of that folder create a folder called main.tf. This folder is where we will put all of our terraform code.<\/p>\n\n\n\n<p>In your editor of choice, open up the folder and select the main.tf file. To create our azure homelab we will need the following resources created by terraform:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>azure resource group<\/li>\n\n\n\n<li>network security group that allows any network to access port 3389 for rdp access<\/li>\n\n\n\n<li>create a virtual network with a cidr of 10.0.0.0\/16 and a subnet in that vnet with the 10.0.1.0\/24 prefix<\/li>\n\n\n\n<li>a network interface and public ip address we can use to access our azure homelab server<\/li>\n\n\n\n<li>a windows 2016 virtual machine<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-create-your-resource-group\">Create your resource group<\/h3>\n\n\n\n<p>At the top of the main.tf file we need to add our terraform block and required providers. This section of the code also creates the azure resource group in the specified region. We also need the \u201cdata\u201d and \u201coutput blocks as well. We need to do this because we have no way to pull the subnet id natively from the azurerm_virtual_network block. Without the Id, we would not be able to configure the network interface block down below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>terraform {\n  required_providers {\n    azurerm = {\n      source  = \"hashicorp\/azurerm\"\n      version = \"=3.22.0\"\n    }\n  }\n}\n# Configure the Microsoft Azure Provider\nprovider \"azurerm\" {\n  features {}\n}\n# Create a resource group\nresource \"azurerm_resource_group\" \"homelabrg\" {\n  name     = \"homelab\"\n  location = \"East US\"\n}\ndata \"azurerm_subnet\" \"example\" {\n  name                 = \"subnet1\"\n  virtual_network_name = \"homelab-network\"\n  resource_group_name  = \"homelab\"\n}\noutput \"subnet_id\" {\n  value = data.azurerm_subnet.example.id\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-azure-security-group\">Azure security group<\/h3>\n\n\n\n<p>Next we will create our security group and then a rule that will allow us to remote into the instance.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>resource \"azurerm_network_security_group\" \"homelabsg\" {\n  name                = \"homelab-security-group\"\n  location            = azurerm_resource_group.homelabrg.location\n  resource_group_name = azurerm_resource_group.homelabrg.name\n\n  security_rule {\n    name                       = \"allow rdp inbound\"\n    priority                   = 100\n    direction                  = \"Inbound\"\n    access                     = \"Allow\"\n    protocol                   = \"Tcp\"\n    source_port_range          = \"*\"\n    destination_port_range     = \"3389\"\n    source_address_prefix      = \"*\"\n    destination_address_prefix = \"*\"\n  }\n\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-building-your-azure-virtual-network\">Building your azure virtual network<\/h3>\n\n\n\n<p>The next terraform resource block is the azurerm_virtual_network. This will define the address space and subnet being used.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>resource \"azurerm_virtual_network\" \"homelabvnet\" {\n  name                = \"homelab-network\"\n  location            = azurerm_resource_group.homelabrg.location\n  resource_group_name = azurerm_resource_group.homelabrg.name\n  address_space       = &#91;\"10.0.0.0\/16\"]\n  dns_servers         = &#91;\"10.0.0.4\", \"10.0.0.5\"]\n\n  subnet {\n    name           = \"subnet1\"\n    address_prefix = \"10.0.1.0\/24\"\n  }\n\n  tags = {\n    environment = \"homelab\"\n  }\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-azure-network-interface-amp-public-ip-address\">Azure network interface &amp; public IP address<\/h3>\n\n\n\n<p>These next two pieces are really critical for the creation of the azure virtual machine. Without the network interface and public ip address, you will not be able to access the server from home.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>resource \"azurerm_public_ip\" \"example\" {\n  name                = \"homelabpublicip\"\n  resource_group_name = azurerm_resource_group.homelabrg.name\n  location            = azurerm_resource_group.homelabrg.location\n  allocation_method   = \"Static\"\n\n  tags = {\n    environment = \"homelab\"\n  }\n}\n\n\nresource \"azurerm_network_interface\" \"example\" {\n  name                = \"windowshomelab-nic\"\n  location            = azurerm_resource_group.homelabrg.location\n  resource_group_name = azurerm_resource_group.homelabrg.name\n\n\n  ip_configuration {\n    name                          = \"internal\"\n    subnet_id                     = data.azurerm_subnet.example.id\n    private_ip_address_allocation = \"Dynamic\"\n    public_ip_address_id          = azurerm_public_ip.example.id\n  }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9-creating-a-virtual-machine-in-azure-using-terraform\">Creating a virtual machine in azure using terraform<\/h3>\n\n\n\n<p>Last but not least we need to choose our azure virtual machine. This will configure the image we will be using, the os disk and the username and password to login. Best practice would be to utilize secrets to pass the password into the file vs clear text. Since this is a homelab we can destroy and bring up at any time we can keep it simple.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>resource \"azurerm_windows_virtual_machine\" \"example\" {\n  name                = \"example-machine\"\n  resource_group_name = azurerm_resource_group.homelabrg.name\n  location            = azurerm_resource_group.homelabrg.location\n  size                = \"Standard_F2\"\n  admin_username      = \"adminuser\"\n  admin_password      = \"P@$$w0rd1234!\"\n  network_interface_ids = &#91;\n    azurerm_network_interface.example.id\n  ]\n\n  os_disk {\n    caching              = \"ReadWrite\"\n    storage_account_type = \"Standard_LRS\"\n  }\n\n  source_image_reference {\n    publisher = \"MicrosoftWindowsServer\"\n    offer     = \"WindowsServer\"\n    sku       = \"2016-Datacenter\"\n    version   = \"latest\"\n  }\n}<\/code><\/pre>\n\n\n\n<p>The output block from the first snippet of code above will provide you with the outside ip address you can utilize to rdp into the server. Now if this is your first time using terraform you will have to complete a couple commands:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u201cTerraform init\u201d this will initialize and download the required providers<\/li>\n\n\n\n<li>\u201cTerraform Plan\u201d will allow you see what will be changed. Its always best practice to run this first<\/li>\n\n\n\n<li>\u201cTerraform Apply\u201d will then Configure all of your azure homelab resources<\/li>\n\n\n\n<li>\u201cTerraform Destroy\u201d will destroy all of the resources once your are done using them. This could also help save money when not using them.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"10-conclusion\">Conclusion<\/h2>\n\n\n\n<p>Now that we have the full homelab setup, you will be able to expand on this configuration and build more resources. As your skills in azure continue to grow, its always important to keep as much of your lab in terraform. This will help keep your resources in one configuration and help eliminate an expensive bill if you forget to delete anything manually.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Building an azure homelab can help give you the opportunity to study for certifications or help grow your career as a cloud engineer. Using terraform to build your homelab will allow you to spin up and destroy your environment everyday to save on cost. In this article we will go over the following task to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69,35,77,85,84],"tags":[],"class_list":["post-3621","post","type-post","status-publish","format-standard","hentry","category-azure","category-cloud-computing","category-devops","category-iac","category-terraform"],"_links":{"self":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/3621","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3621"}],"version-history":[{"count":3,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/3621\/revisions"}],"predecessor-version":[{"id":3625,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/3621\/revisions\/3625"}],"wp:attachment":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}