unable to deploy the azure vm using terraform code

Anil Kumar Saripadi 40 Reputation points
2025-05-30T20:01:39.0533333+00:00

Hi,

Unable to deploy the azure compute gallery image vm using terraform code

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
9,013 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Siva Pavuluri 490 Reputation points Microsoft External Staff Moderator
    2025-06-11T06:20:40.28+00:00

    Hi Anil Kumar Saripadi,

    You are creating a Linux VM using a custom image from your Compute Gallery, and you provided the SKU, publisher, and offer using your own naming convention, as shown in the screenshot. I also provided similar names accordingly.
    User's image

    Terraform main.tf file:

    terraform {
      required_providers {
        azurerm = {
          source  = "hashicorp/azurerm"
          version = "3.0.0"
        }
      }
    }
    
    # Configure the Microsoft Azure Provider
    provider "azurerm" {
      features {}
    }
    
    resource "azurerm_resource_group" "myresource19981" {
      name     = "1998myresource-resources"
      location = "East Us"
    }
    
    resource "azurerm_virtual_network" "virtualnetwork" {
      name                = "virtualnetwork-vnet1"
      address_space       = ["10.0.0.0/16"]
      location            = azurerm_resource_group.myresource19981.location
      resource_group_name = azurerm_resource_group.myresource19981.name
    }
    
    resource "azurerm_subnet" "example" {
      name                 = "example-subnet1"
      resource_group_name  = azurerm_resource_group.myresource19981.name
      virtual_network_name = azurerm_virtual_network.virtualnetwork.name
      address_prefixes     = ["10.0.1.0/24"]
    }
    
    resource "azurerm_network_interface" "example" {
      name                = "example-nic1"
      location            = azurerm_resource_group.myresource19981.location
      resource_group_name = azurerm_resource_group.myresource19981.name
    
      ip_configuration {
        name                          = "internal"
        subnet_id                     = azurerm_subnet.example.id
        private_ip_address_allocation = "Dynamic"
      }
    }
    
    resource "azurerm_linux_virtual_machine" "example" {
      name                = "example-vm1"
      resource_group_name = azurerm_resource_group.myresource19981.name
      location            = azurerm_resource_group.myresource19981.location
      size                = "Standard_D2s_v3"
    
      network_interface_ids = [
        azurerm_network_interface.example.id,
      ]
    
      admin_username = "adminstrator"
    
      # Recommended: Use SSH key authentication
      admin_ssh_key {
        username   = "adminstrator"
        public_key = file("C:\\Users\\username\\.ssh\\id_rsa.pub") # Path to your SSH public key
      }
    
      source_image_id = "/subscriptions/subscriptions-id/resourceGroups/vm-win/providers/Microsoft.Compute/galleries/terraform/images/rocky/versions/9.4.0"
    
      os_disk {
        caching              = "ReadWrite"
        storage_account_type = "Standard_LRS"
        name                 = "example-osdisk"
      }
    
    }
    
    

    I successfully created the main.tf Terraform file using the custom image. Whenever you execute the Terraform file, please make sure to validate it and check if the format is correct. Also, remember to run terraform plan before applying.

    Please find the screenshot of the VM created using a custom image through Terraform.

    User's image

    If you found information is helpful, please click "Upvote" on the post to let us know.

    If you have any further queries feel free to ask us we are happy to assist you.

    Thank You.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.