skip to Main Content

I would like to add a new VM to existing resource group in Azure which already contains 3 VMs. While running tf apply, it throws an error saying the resource group already exists.

2

Answers


  1. I would like to add a new VM to existing resource group in Azure which already contains 3 VMs. While running tf apply, it throws an error saying the resource group already exists.

    If you want to create a VM in an existing resource group instead of creating a new resource, you can use a data block to import the existing resource group.

    Here is the updated Terraform code for creating 3 resources in an existing Resource Group.

        provider  "azurerm"  {
        features  {}
        }
        data "azurerm_resource_group" "example" {
          name = "Existing-RG-Name" 
          }
        
        resource  "azurerm_virtual_network"  "vnet"  {
        name = "venkat-vnet"
        address_space = ["10.0.0.0/16"]
        location = data.azurerm_resource_group.example.location
        resource_group_name = data.azurerm_resource_group.example.name
           }
        resource  "azurerm_subnet"  "subnet"  {
        name = "vm-subnet"
        resource_group_name = data.azurerm_resource_group.example.name
        virtual_network_name = azurerm_virtual_network.vnet.name
        address_prefixes = ["10.0.1.0/24"]
        }
        resource  "azurerm_network_interface"  "samplenic"  {
        name = "VM-nic"
        location = data.azurerm_resource_group.example.location
        resource_group_name = data.azurerm_resource_group.example.name
        ip_configuration  {
        name = "internal"
        subnet_id = azurerm_subnet.subnet.id
        private_ip_address_allocation = "Dynamic"
        }
        }
        resource  "azurerm_windows_virtual_machine"  "myvm"  {
        name = "venkat-vm"
        location = data.azurerm_resource_group.example.location
        resource_group_name = data.azurerm_resource_group.example.name
        network_interface_ids = [azurerm_network_interface.samplenic.id]
        size = "Standard_B1ls"
        admin_username = "Venkat"
        admin_password = "Password1234!"
        os_disk  {
        name = "VM-osdisk"
        caching = "ReadWrite"
        storage_account_type = "Standard_LRS"
        }
        source_image_reference  {
        publisher = "MicrosoftWindowsServer"
        offer = "WindowsServer"
        sku = "2019-Datacenter"
        version = "latest"
        }
        } 
    

    Terraform Apply:
    enter image description here

    Reference: Data Source: azurerm_resource_group

    Login or Signup to reply.
  2. You can use terraform module by Azure https://github.com/Azure/terraform-azurerm-compute

    provider  "azurerm"  {
      features  {}
    }
    data "azurerm_resource_group" "example" {
      name = "Existing-RG-Name" 
    }
    module "linuxservers" {
      source              = "Azure/compute/azurerm"
      resource_group_name = data.azurerm_resource_group.example.name
      vm_os_simple        = "UbuntuServer"
      public_ip_dns       = ["linsimplevmips"]
      vnet_subnet_id      = module.network.vnet_subnets[0]
    }
        
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search