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

S3CloudHub - Sep 4 - - Dev Community

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.

Image description

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[https://azure.microsoft.com/en-us/pricing/purchase-options/azure-account?icid=azurefreeaccount].
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[https://learn.microsoft.com/en-us/cli/azure/install-azure-cli].

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
  }
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Step 3: Configuring GitHub Secrets
For the Azure Login action to work, you need to set up secrets in your GitHub repository.

In your Azure account, create a service principal:

az ad sp create-for-rbac --name "myApp" --role="Contributor" --scopes="/subscriptions/{subscription-id}" --sdk-auth
Enter fullscreen mode Exit fullscreen mode

This command will output a JSON object with your credentials.

  1. 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:

Initialize Terraform.
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:-
image alt text here

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

WhatsApp:-https://www.whatsapp.com/channel/0029VaeX6b73GJOuCyYRik0i
facebook:-https://www.facebook.com/S3CloudHub
youtube:-https://www.youtube.com/@s3cloudhub

Connect with us today and enhance your learning journey!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player