2
votes

Also for public ip id getting: "

Error: Can not parse "ip_configuration.0.public_ip_address_id" as a resource id: Cannot parse Azure ID: parse module.resource.azurerm_public_ip.primary.id: invalid URI for request "

As the network is a nested module for the resource module, will you please suggest, where I'm missing?

main.tf file:

#Select provider
provider "azurerm" {
  subscription_id = "xxxxxxxxxxxxxxxxxxxxxx"
  version = "~> 2.2"
  features {}
}

module "resource" {
  source = "./modules/resource"
  resource_group_name = "DevOpsPoc-primary"
  location = "southeastasia"
}

module "network" {
  source = "./modules/network"
  virtual_network = "primaryvnet"
  subnet = "primarysubnet"
  address_space = "192.168.0.0/16"
  address_prefix = "192.168.1.0/24"
  public_ip = "backendvmpip"
  location = "southeastasia"
  primary_nic = "backendvmnic"
  #vnet_subnet_id = element(module.network.vnet_subnets, 0)
  primary_ip_conf = "backendvm"
}

resource module main.tf file:

resource "azurerm_resource_group" "primary" {
  name     = "var.resource_group_name"
  location = "var.location"
  tags = {
        environment = "env"
    }
}

network module main.tf file:

#Create Virtual Network in Primary Resource Group
resource "azurerm_virtual_network" "primary" {
  name                = "var.virtual_network"
  resource_group_name = "module.resource.azurerm_resource_group.primary.name"
  address_space       = ["var.address_space"]
  location            = "module.resource.azurerm_resource_group.primary.location"
  tags = {
        environment = "env"
    }
}

#Create Subnet in Virtual Network
resource "azurerm_subnet" "primary" {
  name                 = "var.subnet"
  resource_group_name  = "module.resource.azurerm_resource_group.primary.name"
  virtual_network_name = "module.resource.azurerm_virtual_network.primary.name"
  address_prefix       = "var.address_prefix"
#  tags = {
#        environment = "env"
#    }
}

output "subnet_id"{
  value = "module.resource.azurerm_subnet.primary.id"
  }

#Create public IP address
resource "azurerm_public_ip" "primary" {
    name                         = "var.public_ip"
    location                     = "module.resource.azurerm_resource_group.primary.location"
    resource_group_name          = "module.resource.azurerm_resource_group.primary.name"
    allocation_method            = "Dynamic"

    tags = {
        environment = "env"
    }
}

output "public_ip_id"{
  value = "module.resource.azurerm_public_ip.id"
  }

#Create Network Interface
resource "azurerm_network_interface" "primary" {
  name                = "var.primary_nic"
  location            = "module.resource.azurerm_resource_group.primary.location"
  resource_group_name = "module.resource.azurerm_resource_group.primary.name"

  ip_configuration {
        name                           = "var.primary_ip_conf"
        subnet_id                      = "module.resource.azurerm_subnet.primary.id"
        private_ip_address_allocation  = "Dynamic"
        public_ip_address_id           = "module.resource.azurerm_public_ip.primary.id"
    }
  tags = {
        environment = "env"
    }
}
2
I'm curious that, you don't have string interpolcation in strings like var.public_ip or module.resource.azurerm_subnet.primary.idBerkhan Berkdemir

2 Answers

1
votes

There are some places need to be corrected in your codes:

  • You don't need double quotes"" in variables or expression refers to Interpolation Syntax. For example "var.virtual_network" should be var.virtual_network.
  • You can directly reference resources in the same main.tf file instead of from the module block. For example, change virtual_network_name = "module.resource.azurerm_virtual_network.primary.name" to virtual_network_name = azurerm_virtual_network.primary.name in the resource "azurerm_subnet" block.
  • The syntax for referencing module outputs is ${module.NAME.OUTPUT}, where NAME is the module name given in the header of the module configuration block and OUTPUT is the name of the output to reference. You can declare resource group name and location in module "network" instead of using it from the ./modules/network/main.tf file.

Here is the working code and you could get more references in this document:

main.tf file in the root directory

module "resource" {
  source = "./modules/resource"
  resource_group_name = "DevOpsPoc-primary"
  location = "southeastasia"
}


module "network" {
  source = "./modules/network"
  resource_group_name = module.resource.RGname
  location = module.resource.location
  virtual_network = "primaryvnet"
  subnet = "primarysubnet"
  address_space = ["192.168.0.0/16"]
  address_prefix = "192.168.1.0/24"
  public_ip = "backendvmpip"
  primary_nic = "backendvmnic"
  #vnet_subnet_id = element(module.network.vnet_subnets, 0)
  primary_ip_conf = "backendvm"
}

main.tf in the directory ./modules/resource

variable "resource_group_name" {}
variable "location" {}

resource "azurerm_resource_group" "primary" {
  name     = var.resource_group_name
  location = var.location

}

output "RGname" {
  value = "${azurerm_resource_group.primary.name}"
}

output "location" {
    value = "${azurerm_resource_group.primary.location}"
}

main.tf in the directory ./modules/network and also declare the variables in the same directory.

#Create Virtual Network in Primary Resource Group
resource "azurerm_virtual_network" "primary" {
  name                = var.virtual_network
  resource_group_name = var.resource_group_name
  address_space       = var.address_space
  location            = var.location

}

#Create Subnet in Virtual Network
resource "azurerm_subnet" "primary" {
  name                 = var.subnet
  resource_group_name  = var.resource_group_name
  virtual_network_name = azurerm_virtual_network.primary.name
  address_prefix       = var.address_prefix

}

output "subnet_id"{
  value = azurerm_subnet.primary.id
  }

#Create public IP address
resource "azurerm_public_ip" "primary" {
    name                         = var.public_ip
    location                     = var.location
    resource_group_name          = var.resource_group_name
    allocation_method            = "Dynamic"

}

output "public_ip_id"{
  value = azurerm_public_ip.primary.id
  }


#Create Network Interface
resource "azurerm_network_interface" "primary" {
  name                = var.primary_nic
  location            = var.location
  resource_group_name = var.resource_group_name

  ip_configuration {
        name                           = var.primary_ip_conf
        subnet_id                      = azurerm_subnet.primary.id
        private_ip_address_allocation  = "Dynamic"
        public_ip_address_id           = azurerm_public_ip.primary.id
    }

}
0
votes

I had a similar error when setting up an Azure App Service using Terraform.

module.app_service.azurerm_app_service.app_service: Creating...

│ Error: Cannot parse Azure ID: parse "27220": invalid URI for request
│ 
│   with module.app_service.azurerm_app_service.app_service,
│   on ../../../modules/azure/app-service/main.tf line 1, in resource "azurerm_app_service" "app_service":
│    1: resource "azurerm_app_service" "app_service" {

Here's how I fixed it:

The issue was that I used the wrong value for the App Service Plan ID in my module.

I was using 27220 as the App Service Plan ID, instead of the actual value of the App Service Plan ID which of this format:

"/subscriptions/fec545cd-bead-43ba-84c6-5738cdc7e458/resourceGroups/MyDevRG/providers/Microsoft.Web/serverfarms/MyDevLinuxASP"

That's all