A Step-by-Step Guide on Creating a Resource Group, Virtual Network and Subnet in Azure with Terraform.

donhadley22

Bernard Chika Uwaezuoke

Posted on August 23, 2023

A Step-by-Step Guide on Creating a Resource Group, Virtual Network and Subnet in Azure with Terraform.

Introduction

Welcome to this in-depth blog series where we unravel the art of provisioning Azure resources using Terraform, a powerful Infrastructure as Code (IaC) tool. In this digital era, orchestrating cloud infrastructure efficiently is paramount, and Terraform provides the brush strokes you need to paint your Azure canvas with precision. This article will guide you through the process of creating a resource group, a virtual network, and a subnet in Azure using Terraform, equipping you with the skills to architect and manage cloud resources seamlessly.

What is Terraform?

Terraform is an open-source infrastructure as code (IaC) tool developed by HashiCorp. It allows you to define, manage, and provision infrastructure resources in a declarative way using configuration files. These configuration files, written in HashiCorp Configuration Language (HCL), describe the desired state of your infrastructure components such as virtual machines, networks, storage, and other cloud services.

Terraform provides a way to manage infrastructure across various cloud providers such as AWS, Azure, Google Cloud, Oracle etc. and even on-premises environments. It helps automate the process of creating, updating, and destroying resources by treating infrastructure as code, enabling you to version-control your infrastructure configurations and collaborate more effectively with your team.

Some Terraform key concepts

1. Providers: These are plugins that interface with various cloud providers' APIs. Each provider offers resource types that Terraform can manage, such as virtual machines, networks, databases, and more.

2. Resources: These are the fundamental building blocks of your infrastructure, representing the individual components you want to manage. For example, an AWS instance or an Azure virtual machine might be represented as a resource.

3. Modules: Modules allow you to encapsulate and reuse configurations. They can be thought of as reusable blueprints for creating infrastructure components.

4. State: Terraform maintains a state file that keeps track of the current state of your infrastructure as defined in your configuration files. This state file is used to determine the difference between the desired state and the actual state and to perform updates accordingly.

5. Execution Plan: Terraform generates an execution plan before making any changes to your infrastructure. This plan outlines what actions Terraform will take to reach the desired state, allowing you to review and approve changes before they are applied.

6.Apply: The "terraform apply" command is used to apply the changes defined in your configuration files to your infrastructure. It provisions or updates resources based on the execution plan.

Prerequisites

  1. Install VScode (IDE)
  2. Install Terraform
  3. Azure Account
  4. Install WSL Ubuntu
  5. Azure CLI

Let's dive in now!

Download and install Visual Studio Code
Use the link bellow to download and install Microsoft VScode.

https://code.visualstudio.com/download
Download MS Visual Studio Code – VSCode

Image description

- Select the suitable one for your operating system. We are selecting Windows for this article.

Click Install/Launch/
Sign in to MS Visual Studio with your Microsoft Account To create a password for the default user.

Configure Visual Studio to run Ubuntu terminal

  • Open the VScode and click on the extension icon as shown below.

Image description

  • Type remote wsl in the search bar select and then install.

Image description

Install WSL Ubuntu from Microsoft Store

  • Search for Microsoft Store on your system.

  • In Microsoft Store, Search for, download, and INSTALL Ubuntu

  • Unpack the App.

Install Terraform in your VScode

Use the documentation in the link below to Install Terraform
https://cloudlinuxtech.com/install-terraform-on-ubuntu-uninstall-terraform/

  • Copy and paste these commands, one line at a time:
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -

sudo apt-add-repository "deb [arch=$(dpkg --print-architecture)] https://apt.releases.hashicorp.com
$(lsb_release -cs) main"

$sudo apt update
$sudo apt install terraform
$ terraform --version
$ which terraform
Enter fullscreen mode Exit fullscreen mode
  • Click on the extension icon by the left of your VScode search for and install these extensions indicated in the image below:

Image description

Configure Provider (Azure)

  • Now download and install azure-cli for ubuntu with these commands one after the other:
sudo apt update
sudo curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash 
az --version
Enter fullscreen mode Exit fullscreen mode

Now that we are done setting-up our working environment, let's now commence the main business of deploying our resources using terraform.

Deploying our resources

  • Create a folder on our desktop named it AZURE TERRAFORM1.

  • Open the folder using VScode

  • Create two files named provider.tf and main.tf respectively.

Image description

  • Click on the terminal button on the top of the VScode to open your interface below and select the wsl ubuntu to display your shell promptin order to be able to run your Linux commands.

Image description

  • Go to the Terraform registry for Azure on the link below to get your templates for deploying Azure resources.

https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs

  • In the provider.tf file, copy the provider configuration from the terraform registry and paste. Then go to the terminal and initialize terraform by running this command.

terraform init

Image description

You will get this notification indicating that the initialization was successful.

Image description

  • Login to Azure by running the az login command.

Image description

This will open a browser tab for you to provide your login credentials.

  • Select the appropriate account and sign-in. Image description

The notification below will be displayed on the browser indicating that your login was successful.

Image description

The account and subscription details will be displayed in your terminal.

Image description

  • Go to the main.tf file and start deploying your resources.

Provisioning a Resource Group

  • Go to the Terraform Azure registry and copy a template for Resource group by typing the resource group in the search bar.

  • Customize to your preference by editing the values of the parameters resource, name and location to suite your specification. The unique id is the element in the quotation mark after the resource.
    In this instance, the resource is "azurerm_resource_group" we specified the unique id as Proj-rg. The name is project1-RG and the location is east us

Image description

When we are done defining our resource in HashiCorp Configuration Language, we run the terraform init command again to start the process of provisioning.

Image description

  • Then terraform plan command, to ascertain that what we intend doing is indeed what terraform will do.

Image description

  • We can now run the terraform apply command to deploy the resource.

Image description

We will be prompted by terraform if we want to go ahead with the provisioning, and we type yes to approve.

Image description

Terraform will deploy our resource for us as specified.

  • View the provisioned resource in *Azure portal *.

Image description

To Provision a Virtual Network

Now we want to create a Virtual Network within the resource group we just created.

  • Go to the Terraform Registry for Azure and copy the template for provisioning a virtual network and paste it right below the resource group code in the main.tf file in our code editor.

  • Edit it to our specification as shown in the image below.

Image description

  • When we are done editing to our specification, by providing the unique id vnet1, name project1-network, resource group Proj-rg, location specified with the resource group unique id and the private IPv4 range 10.0.0.0/16. We conslude the process by running these same three commands one after the other to deploy the virtual network to Azure cloud.
terraform init
terraform plan
terraform apply
Enter fullscreen mode Exit fullscreen mode

Image description

  • View our Virtual Network in Azure Portal.

Image description

Provisioning a subnet in our Virtual network

  • We begin by going to the terraform library again to search for subnet and copy the desired template and place it right below the virtual network code on our editor.

  • We continue by providing our customized value for the parameters in the template as shown in the image below.
    In this instance, the unique id for our subnet is example and the name is example-subnet.

  • We provide the resource group and virtual network parameters values using there unique Proj-rg and vnet1 respectively.

  • We also provide the IPv4 address range for the subnet 10.0.1.0/24.

Image description

  • We conclude the process by running the terraform commands one after the other again.
terraform init
terraform plan
terraform apply
Enter fullscreen mode Exit fullscreen mode
  • We view our subnet in Azure portal.

Image description

  • We can also see the address range of the virtual network the resource group in the Azure portal.

Image description

Below is our module, which we can push to Github for future us and to be used by others.

Image description

Destroying our resources
As convention demands, we clean-up our cloud account by deleting resources that are no longer in use.
To destroy our resources, using terraform, we just run the command terraform destroy on out terminal and type yes when prompted or add the argument --auto-approve to approve the removal automatically.

Image description

Image description
From the above images, we can see that the resources we provisioned have been destroyed.

With this, we come to this guide on using terraform for Azure.

Conclusion
In this step-by-step guide, we've embarked on a journey into the world of cloud infrastructure provisioning with Terraform on the Microsoft Azure platform. We've covered the fundamentals, walked through the essential setup, and explored how to create and manage resources in a structured and efficient manner.

As you venture forward, consider exploring advanced Terraform topics such as managing state, handling secrets, and **integrating **with other tools in the DevOps ecosystem. Remember, learning is a continuous journey, and each step you take deepens your mastery.

Thank you for joining us on this exploration of Terraform and Azure. Please, feel free to share your thoughts and questions in the comments section, and remember, to subscribe and follow us for more articles like this.

💖 💪 🙅 🚩
donhadley22
Bernard Chika Uwaezuoke

Posted on August 23, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related