简体   繁体   中英

How to use existing resources from another resource group to make deployments in new resource group using Terraform

I want to create resources in a new resource group but i want to use a virtual network for those resources which is in another resource group. How do i do this? For example, i want to create redis/postgresql in resourcegroupA but i want to make use of the virtual network which is in resourcegroupB. Is it possible?

This is the resource group from where i am retrieving the vnet-

    resource "azurerm_resource_group" "azresourcegroup" {
      name        = 
      "resourcegroupA"
      location    = var.resource_group_location
    }

    #-----CREATING VIRTUAL NETWORK-----

    resource "azurerm_virtual_network" "vnet2" {
       name                = "virtualnetworkA"
       location            = azurerm_resource_group.azresourcegroup.location
       resource_group_name = azurerm_resource_group.azresourcegroup.name
       address_space       = [var.virtual_network_address_prefix_infra,var.virtual_network_address_prefix]

I retrieved it while using it for another resource group like this-

    data "azurerm_resource_group" "azresourcegroup" {
      name        = "resoucegroupA"
    }

    data "azurerm_virtual_network" "vnet2" {
     name                = "virtualnetworkA"
      resource_group_name = data.azurerm_resource_group.azresourcegroup.name
    }

I want to use the above virtual network but want to create the other resources in the new resource group which is-

     resource "azurerm_resource_group" "main" {
      name        = "resourcegroupB"
      location    = var.resource_group_location
    }

I am making use of module to create redis cache that requires the vnet which is created in other RG-

    module "rediscache" {
      source                             = "../../modules/rediscache"
      prefix                             = var.prefix
      environmentType                    = var.environmentType
      virtual_network_name               = var.virtual_network_name
      unique_identifier                  = var.unique_identifier_kube
      resource_group_name                = azurerm_resource_group.main.name
      resource_group_location            = var.resource_group_location
      redis_subnet_address_prefix        = var.redis_subnet_address_prefix
      azurerm_virtual_network_name       = data.azurerm_virtual_network.vnet2.name
      azurerm_log_analytics_workspace_id = azurerm_log_analytics_workspace.workspace.id
    }

To simplify this, vnet is created in other resource group and redis in another one.But i want to use that vnet. also if i change the resource group name argument used in module, from azurerm_resource_group.main.name to data.azurerm_resource_group.azresourcegroup.name then it creates the redis in the 1st resource group which i dont want. Please help.

Of course, it's possible. The only condition is that the virtual network should be in the same location. Then you can quote it in the Terraform code via the Terraform Data Source azurerm_virtual_network like this:

data "azurerm_virtual_network" "example" {
  name                = "production"
  resource_group_name = "networking"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM