Azure VM Creation using Terraform with GitHub Actions Pipeline[2024]

S3CloudHub
3 min readSep 4, 2024

--

Introduction

In today’s cloud-driven world, Infrastructure as Code (IaC) has become the backbone of modern IT operations. Terraform, an open-source IaC tool, allows you to define and provision data center infrastructure using a high-level configuration language. When combined with the automation capabilities of GitHub Actions, you can achieve seamless deployment pipelines for your Azure infrastructure.

This blog will guide you through the process of creating a Virtual Machine (VM) in Azure using Terraform, automated via a GitHub Actions pipeline.

Prerequisites

Before diving into the technical details, ensure you have the following:

  • Azure Account: You need an active Azure subscription. If you don’t have one, you can create a free account here.
  • GitHub Account: A GitHub repository where you’ll store your Terraform code and GitHub Actions workflows.
  • Terraform Installed: Ensure that Terraform is installed on your local machine. You can download it from the official Terraform website.
  • Azure CLI: Install the Azure CLI for managing your Azure resources. Follow the installation guide here.

Step 1: Writing the Terraform Configuration

First, create a new directory for your Terraform configuration files. Inside this directory, create a file named main.tf. This file will contain the configuration for provisioning an Azure VM.

Here’s a basic example:

provider "azurerm" {
features = {}
}

resource "azurerm_resource_group" "example" {
name = "example-resources"
location = "East US"
}

resource "azurerm_virtual_network" "example" {
name = "example-network"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
}

resource "azurerm_subnet" "example" {
name = "example-subnet"
resource_group_name = azurerm_resource_group.example.name
virtual_network_name = azurerm_virtual_network.example.name
address_prefixes = ["10.0.1.0/24"]
}

resource "azurerm_network_interface" "example" {
name = "example-nic"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name

ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.example.id
private_ip_address_allocation = "Dynamic"
}
}

resource "azurerm_virtual_machine" "example" {
name = "example-vm"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
network_interface_ids = [azurerm_network_interface.example.id]
vm_size = "Standard_DS1_v2"

storage_os_disk {
name = "example-os-disk"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}

storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}

os_profile {
computer_name = "hostname"
admin_username = "adminuser"
admin_password = "P@ssw0rd1234!"
}

os_profile_linux_config {
disable_password_authentication = false
}
}

Step 2: Setting Up the GitHub Actions Pipeline

Next, let’s automate the deployment process using GitHub Actions. Create a .github/workflows directory in your repository, and inside it, create a file named deploy.yml.

Here’s a sample workflow file:

name: 'Terraform Deploy'

on:
push:
branches:
- main

jobs:
terraform:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup Terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_version: 1.0.0

- name: Azure Login
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}

- name: Terraform Init
run: terraform init

- name: Terraform Apply
run: terraform apply -auto-approve

Step 3: Configuring GitHub Secrets

For the Azure Login action to work, you need to set up secrets in your GitHub repository.

  1. In your Azure account, create a service principal:
az ad sp create-for-rbac --name "myApp" --role="Contributor" --scopes="/subscriptions/{subscription-id}" --sdk-auth
  • This command will output a JSON object with your credentials.

2. Copy the JSON output and add it to your GitHub repository’s secrets as AZURE_CREDENTIALS.

Step 4: Running the Pipeline

Push your code to the main branch of your GitHub repository. This action will trigger the GitHub Actions workflow, which will:

  1. Initialize Terraform.
  2. Apply the Terraform configuration to create the Azure VM.

You can monitor the pipeline’s progress in the Actions tab of your GitHub repository.

Explore more detailed content and step-by-step guides on our YouTube channel:-

Conclusion

By combining Terraform with GitHub Actions, you’ve created a powerful, automated pipeline to manage your Azure infrastructure. This setup ensures that your infrastructure is always up-to-date and consistent across environments, making it easier to manage and scale.

Feel free to extend this pipeline by adding more complex configurations, security policies, or integrating it with other tools like Ansible or Kubernetes.

Connect with Us!

Stay connected with us for the latest updates, tutorials, and exclusive content:

Connect with us today and enhance your learning journey!

--

--