Deploying Azure Infrastructure Using Terraform and GitHub: A Step-by-Step Guide


Introduction
This guide provides a comprehensive walkthrough for provisioning Azure resources using Terraform, integrating with GitHub for version control. It covers the setup of necessary tools, configuration of Terraform files, and deployment of resources to Azure.
Prerequisites
Before proceeding, ensure the following tools are installed and configured:
1. Microsoft Azure Account with an Active Subscription
To work with Azure resources, you'll need an Azure account with an active subscription. If you don't have one, go to Azure's official website and sign up for an account. You can start with a free trial that gives you credits to use on Azure services.
Once your account is set up, head over to the Azure Portal to manage your resources. The portal gives you a graphical interface to create and manage your resources on Azure.
2. Install Terraform
Terraform is the tool that allows you to define your infrastructure as code and provision it on Azure. Regardless of how you choose to install Terraform, whether using Chocolatey or manually, you’ll need to make sure it's correctly installed and added to your system’s PATH for easy access.
Option 1: Using Chocolatey (Recommended for Windows)
To get started with Terraform on Windows, Chocolatey—a package manager for Windows—is a quick and easy way to install it.
Open PowerShell as Administrator.
Run the following command to install Terraform:
choco install terraform
After the installation is complete, verify it by checking the Terraform version:
terraform --version
You should see the Terraform version number if the installation was successful.
Option 2: Manually Installing Terraform
If you prefer to install Terraform manually, head over to the Terraform download page. Choose the correct binary for your operating system, download it, and extract it.
Once extracted, you will need to add the Terraform executable to your system’s PATH so that you can run it from the command line. Follow these steps:
Copy the path where you extracted the Terraform binary.
Open Environment Variables on your system.
Under System variables, find and select the
Path
variable, then click Edit.Click New and paste the path to the folder containing the Terraform executable.
Click OK to save.
Now, you should be able to run terraform
from any terminal window.
3. Install Azure CLI
The Azure CLI allows you to interact with Azure resources directly from the command line. It’s necessary for authenticating Terraform with Azure and managing your resources.
Download the Azure CLI from Microsoft's official page.
After installing, open a terminal and log in to your Azure account by running:
az login
This will prompt a browser window to appear for you to sign in to your Azure account.
Once authenticated, the Azure CLI will give you access to manage Azure resources directly from the terminal, which Terraform will leverage when provisioning infrastructure.
4. Install the Terraform Extension in VS Code
VS Code is a lightweight yet powerful code editor that will be used for writing Terraform configuration files. VS Code will be where you write your .tf
files that define your infrastructure.
If you don't have Visual Studio Code yet, download it from the official website and install it with the default settings.
Once installed, open VS Code and navigate to the Extensions view by pressing
Ctrl+Shift+X
.Search for Terraform by HashiCorp, and install the official extension. This extension provides syntax highlighting, code snippets, and other features that make writing Terraform configurations easier.
5. Create a GitHub Account
Terraform code will be stored and managed using GitHub. If you don’t already have a GitHub account, sign up at https://github.com.
Step 1: Create a Local Project Folder
Start by creating a new folder on your computer — just like you would for any other project.
You can do this by right-clicking on your desktop (or any preferred location), selecting New > Folder, and naming it something meaningful like terraform-azure-setup
.
This folder is where all your Terraform configuration files will live.
Step 2: Open the Folder in VS Code
Once the folder is created, open it in Visual Studio Code.
Launch VS Code.
Click on File > Open Folder.
Navigate to your newly created folder and open it.
This gives you a clean workspace to begin writing your Terraform files.
Step 3: Create a New GitHub Repository
Go to GitHub and create a new repository.
Step 4: Clone the Repository into Your Local Folder Using VS Code
After creating your repository on GitHub, return to VS Code where your local folder (e.g., terraform-azure-setup
) is already open. Open the Command Palette (Ctrl+Shift+P
) or go to the Source Control tab on the sidebar, then select Git: Clone.
Select "Clone from GitHub"
When prompted, choose Clone from GitHub. This will link VS Code directly to your GitHub account.
Choose the repository
Once connected to GitHub, a list of your repositories will appear. Find and select the repository you want to clone.
Select the local folder
After selecting the repository, VS Code will prompt you to choose a local folder where you want to clone the repository. Navigate to the folder on your machine where you want the repository to be stored or create a new one.
Open the cloned repository
After the repository has been cloned to your local machine, VS Code will ask if you want to open the repository. Click Open (or Yes) to load the project into your workspace.
Repository is ready to work
Your local folder is now connected to your GitHub repository. Any changes made locally can be tracked, committed, and pushed back to GitHub from within VS Code.
Step 5: Create the main.tf
File and Add Terraform Configuration
Create the main.tf
file
Now that the folder is connected to GitHub and you're ready to start writing code, the next step is to create the main.tf
file. This is the core configuration file where you'll define your infrastructure.
Add the basic configuration
In the main.tf
file, you can start by adding a basic configuration. To set up an Azure resource group, visit the Terraform Registry for Azure and copy the relevant configuration snippet. Here’s a simple example of what the configuration could look like:
In the main.tf
file, you can start by adding a basic configuration. To set up an Azure resource group, visit the Terraform Registry for Azure and copy the relevant configuration snippet.
Here’s a simple example of what the configuration could look like:
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "4.28.0"
}
}
}
provider "azurerm" {
# Configuration options
}
Paste the following Terraform configuration code into the file. This will define your Azure resource group
Add the features
block and subscription ID to the configuration to enable Azure integration and specify your Azure account. It should be something similar to this
provider "azurerm" {
features {}
# Add your Azure subscription ID here
subscription_id = "your-subscription-id" # Replace with your actual subscription ID
}
Once you’ve added the configuration, save the main.tf
file. This marks the end of the configuration part, and you’re ready to run your Terraform commands.
Step 6: Run terraform init
After you've saved the main.tf
file with your configuration, the next thing to do is initialize Terraform. This will set up the environment and download the necessary providers.
Open your terminal inside VS Code. Navigate to the folder where your main.tf
file is located. You can do this using the cd
(change directory) command:
Run terraform init
to initialize the working directory:
terraform init
Click Enter
Run terraform init
to initialize the working directory;
terraform init
When you run terraform init
, you should see output indicating that Terraform is installing the required providers and setting up the workspace. It will look something like this:
This step ensures everything is set up and ready for managing resources with Terraform.
Step 7: Add Resources
Next, you'll start defining the actual infrastructure you want to deploy in Azure. Here’s how you can break it down step by step:
Create a Resource Group
In your main.tf
file, you need to define a Resource Group in Azure, which acts as a container for your resources.
Add the resource block for the Azure resource group:
resource "azurerm_resource_group" "test" {
name = "acctestrg"
location = "West US 2"
}
name
: This is the name of your resource group in Azure.
location
: This specifies the Azure region (e.g., "West US 2").
Create a Virtual Network
Next, you'll create a Virtual Network (VNet) to define the networking for your resources.
Add the resource block for the Azure virtual network:
address_space
: Defines the IP address space for the virtual network.
location
andresource_group_name
: Link the VNet to the resource group you created earlier.
resource "azurerm_virtual_network" "test" {
name = "acctvn"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
}
Create a Subnet
Now, create a Subnet within the virtual network you just created.
Add the resource block for the Azure subnet:
resource "azurerm_subnet" "test" {
name = "acctsub"
resource_group_name = azurerm_resource_group.test.name
virtual_network_name = azurerm_virtual_network.test.name
address_prefixes = ["10.0.2.0/24"]
}
address_prefixes
: This specifies the range of IP addresses for the subnet.
Terraform allows you to define as many resources as your architecture requires, all in the same file or across multiple files in the same directory. These can include things like:
Virtual Networks and Subnets
Public IP Addresses
Load Balancers
Virtual Machines
Managed Disks
Availability Sets
Network Interfaces
...and many more
Here’s a brief snippet showing how additional resources might be structured in the same configuration:
resource "azurerm_public_ip" "example" {
name = "public-ip"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
allocation_method = "Static"
}
resource "azurerm_virtual_machine" "example" {
name = "example-vm"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
network_interface_ids = [azurerm_network_interface.test.id]
vm_size = "Standard_DS1_v2"
# ...
}
You don’t need to add everything at once. Start with the essentials, then gradually expand based on what your infrastructure needs.
You can find templates and configuration examples in the Terraform Azure Provider documentation.
Step 8: Authenticate with Azure
Before you can apply any changes, you need to authenticate Terraform with your Azure account. To do this, you’ll use the Azure CLI. If you haven't already logged in to Azure using the CLI, open Windows PowerShell and run the following command:
az login
This command will open a web page where you can log in with your Azure credentials. Once logged in, the CLI will show confirmation that you are authenticated.
This step ensures Terraform has the necessary permissions to create and manage resources on your Azure account.
Step 9: Run terraform plan
After adding your resources (Resource Group, Virtual Network, and Subnet), it’s time to check your configuration and make sure Terraform knows what to do. Use the terraform plan
command
Run terraform plan
in your terminal. This command will show you the execution plan. It tells you what Terraform will do (e.g., create the resource group, virtual network, subnet etc.).
terraform plan -out plan.tfplan
When working with Terraform, it's important to keep your configuration files and execution plans separate. Instead of using main.tf
to store your execution plan, the recommended approach is to use a distinct file, like plan.tfplan
, when running terraform plan -out plan.tfplan
.
This ensures that:
Your
main.tf
configuration remains untouched, allowing you to continue editing and managing your infrastructure code.The execution plan is stored separately in a binary file (
plan.tfplan
), which provides clarity and allows you to apply the exact changes Terraform has planned.By keeping the plan file distinct, you can review the changes before applying them, ensuring more controlled and predictable infrastructure management.
Your output should look like this.
Then Run terraform init
in your terminal. Your output should look like this.
Step 10: Apply the Configuration with terraform apply
You can proceed to apply the changes and create the resources in Azure. Run terraform apply
in your terminal:
terraform apply plan.tfplan
Your output should look like this.
Once the resources are created, Terraform will output a success message. You can now verify the creation of these resources directly in the Azure portal.
Step 11: Verify the Resources
After running terraform apply
, you can log into the Azure portal and verify that the resources were created correctly:
Go to Resource Groups to see if the resource group
acctestrg
was created.Check Virtual Networks to confirm that
acctvn
exists.Look at Subnets to make sure that
acctsub
is available.
By following these steps, you've successfully set up Terraform on your Windows machine, configured Azure CLI, and deployed resources to Azure using Terraform within VS Code.
Subscribe to my newsletter
Read articles from Mercy Aderemi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
