I would like to run terraform plan -var-file -out and then apply the plan but I get the following error.
Here's my main.tf, variable.tf and networking.tfvars file below:
###### Resource Group ##############
resource "azurerm_resource_group" "resourcegroupname" {
name = "${var.resourcegrouprefix}-RG"
location = "${var.location}"
}
###### VNET ##############
resource "azurerm_virtual_network" "vnet" {
name = "${var.vnet-prefix}-vnet"
resource_group_name = "${azurerm_resource_group.resourcegroupname.name}"
location = "${azurerm_resource_group.resourcegroupname.location}"
address_space = "${var.vnetcidr}"
}
# address_space = ["10.0.0.0/20"]
###### Subnets ##############
resource "azurerm_subnet" "subnet1" {
name = "${var.subnet1-prefix}"
virtual_network_name = "${azurerm_virtual_network.vnet.name}"
resource_group_name = "${azurerm_resource_group.resourcegroupname.name}"
address_space = "${var.subnet1cidr}"
}
resource "azurerm_subnet" "subnet2" {
name = "${var.subnet2-prefix}"
virtual_network_name = "${azurerm_virtual_network.vnet.name}"
resource_group_name = "${azurerm_resource_group.resourcegroupname.name}"
address_space = "${var.subnet2cidr}"
}
resource "azurerm_subnet" "subnet3" {
name = "${var.subnet3-prefix}"
virtual_network_name = "${azurerm_virtual_network.vnet.name}"
resource_group_name = "${azurerm_resource_group.resourcegroupname.name}"
address_space = "${var.subnet3cidr}"
Variables File
variable "resourcegrouprefix" {
description = "The prefix used for all resources in VNET RG"
default = ""
}
variable "vnet-prefix" {
description = "The prefix used for VNET resource"
default = ""
}
variable "vnetcidr" {
default = ""
}
variable "subnet1cidr" {
default = ""
}
variable "subnet2cidr" {
default = ""
}
variable "subnet3cidr" {
default = ""
}
Networking.tfvars
resourcegrouprefix = "networking"
vnet-prefix = "networking"
vnetcidr = "10.0.0.0/20"
subnet1-prefix = "untrust"
subnet2-prefix = "trust"
subnet3-prefix = "mgmt"
subnet1cidr = "10.0.0.0/24"
subnet2cidr = "10.0.1.0/24"
subnet3cidr = "10.0.2.0/24"
Command that I'm trying to execute:
terraform plan -var-file="networking.tfvars" -out="networkingplan.out"
Error message:
azurerm_subnet.subnet1: "address_prefix": required field is not set
azurerm_subnet.subnet1: : invalid or unknown key: address_space
azurerm_subnet.subnet2: "address_prefix": required field is not set
azurerm_subnet.subnet2: : invalid or unknown key: address_space
azurerm_subnet.subnet3: "address_prefix": required field is not set
azurerm_subnet.subnet3: : invalid or unknown key: address_space
azurerm_subnet.subnet4: "address_prefix": required field is not set
azurerm_subnet.subnet4: : invalid or unknown key: address_space
azurerm_subnet.subnet5: "address_prefix": required field is not set
azurerm_subnet.subnet5: : invalid or unknown key: address_space
azurerm_subnet.subnet6: "address_prefix": required field is not set
azurerm_subnet.subnet6: : invalid or unknown key: address_space
azurerm_virtual_network.vnet: address_space: should be a list
Fixed the error message
###### Resource Group ##############
resource "azurerm_resource_group" "resourcegroupname" {
name = "${var.resourcegrouprefix}-RG"
location = "${var.location}"
}
###### VNET ##############
resource "azurerm_virtual_network" "vnet" {
name = "${var.vnet-prefix}-vnet"
resource_group_name = "${azurerm_resource_group.resourcegroupname.name}"
location = "${azurerm_resource_group.resourcegroupname.location}"
address_space = ["${var.vnetcidr}"]
}
# address_space = ["10.0.0.0/20"]
###### Subnets ##############
resource "azurerm_subnet" "subnet1" {
name = "${var.subnet1-prefix}"
virtual_network_name = "${azurerm_virtual_network.vnet.name}"
resource_group_name = "${azurerm_resource_group.resourcegroupname.name}"
address_prefix = "${var.subnet1cidr}"
}
resource "azurerm_subnet" "subnet2" {
name = "${var.subnet2-prefix}"
virtual_network_name = "${azurerm_virtual_network.vnet.name}"
resource_group_name = "${azurerm_resource_group.resourcegroupname.name}"
address_prefix = "${var.subnet2cidr}"
}
resource "azurerm_subnet" "subnet3" {
name = "${var.subnet3-prefix}"
virtual_network_name = "${azurerm_virtual_network.vnet.name}"
resource_group_name = "${azurerm_resource_group.resourcegroupname.name}"
address_prefix = "${var.subnet3cidr}"
}
Related
I am trying to setup a databricks into subet and protect it by firewalls using the following code in Terraform:
Setup resource group:
provider "azurerm" {
features {
resource_group {
prevent_deletion_if_contains_resources = false
}
}
}
resource "azurerm_resource_group" "main_resource_group" {
name = var.resource_group_name
location = var.resource_group_location
}
Setup virtual network:
resource "azurerm_virtual_network" "test_vnet" {
name = var.vnet_name
address_space = ["10.0.0.0/16"]
location = var.resource_group_location
resource_group_name = var.resource_group_name
}
Setup subnets:
resource "azurerm_subnet" "private_snet" {
name = "subnet-private"
resource_group_name = var.resource_group_name
virtual_network_name = var.vnet_name
address_prefixes = ["10.0.1.0/24"]
delegation {
name = "databricksprivatermdelegation"
service_delegation {
name = "Microsoft.Databricks/workspaces"
}
}
}
resource "azurerm_subnet" "public_snet" {
name = "subnet-public"
resource_group_name = var.resource_group_name
virtual_network_name = var.vnet_name
address_prefixes = ["10.0.2.0/24"]
delegation {
name = "databrickspublicdelegation"
service_delegation {
name = "Microsoft.Databricks/workspaces"
}
}
}
Setup firewals:
resource "azurerm_network_security_group" "private_empty_nsg" {
name = "firewall-private"
location = var.resource_group_location
resource_group_name = var.resource_group_name
}
resource "azurerm_subnet_network_security_group_association" "private_nsg_asso" {
subnet_id = azurerm_subnet.private_snet.id
network_security_group_id = azurerm_network_security_group.private_empty_nsg.id
}
resource "azurerm_network_security_group" "public_empty_nsg" {
name = "firewall-public"
location = var.resource_group_location
resource_group_name = var.resource_group_name
}
resource "azurerm_subnet_network_security_group_association" "public_nsg_asso" {
subnet_id = azurerm_subnet.public_snet.id
network_security_group_id = azurerm_network_security_group.public_empty_nsg.id
}
And finally setup the databricks:
resource "azurerm_databricks_workspace" "forex_price_databricks" {
name = "databricks-test"
location = var.resource_group_location
resource_group_name = var.resource_group_name
sku = "standard"
custom_parameters {
virtual_network_id = azurerm_virtual_network.test_vnet.id
public_subnet_name = azurerm_subnet.public_snet.name
public_subnet_network_security_group_association_id = azurerm_network_security_group.public_empty_nsg.id
private_subnet_name = azurerm_subnet.private_snet.name
private_subnet_network_security_group_association_id = azurerm_network_security_group.private_empty_nsg.id
}
}
However, when i run the code in the first try i got the below error:
Error: Code="ResourceNotFound" Message="The Resource 'Microsoft.Network/virtualNetworks/my-vnet' under resource group 'My-Resource-Group' was not found.
So, the question is:
Why the Virtual Network is not created ? or cannot be found ?
Update:
When i remove the
resource_group {
prevent_deletion_if_contains_resources = false
}
I used to have this line, becuse i usually run the terraform destroy and i don't want t remove my resource group. However, even if i remove it ,I got the below error:
Message="Operation was canceled." Details=[{"code":"
CanceledAndSupersededDueToAnotherOperation","message":"Operation PutVirtualNetworkOperation was canceled and superseded by operation PutSubnetOpe
ration
Are you able to reproduce the same error ?
I am creating a virtual network using Terraform and the subnets are created within the virtual network resource block (instead of using its own resource block).
I am trying to associate some route tables to the subnets, however I'm not sure how to obtain the subnet resource ID when the subnets are created within virtual network, could someone help?
main.tf
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "3.24.0"
}
}
}
provider "azurerm" {
skip_provider_registration = true
features {}
}
###########################
# RESOURCE GROUP CREATION #
###########################
resource "azurerm_resource_group" "rg" {
name = var.rg.name
location = var.rg.location
}
############################
# VIRTUAL NETWORK CREATION #
############################
resource "azurerm_virtual_network" "vnet" {
name = var.vnet.name
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
address_space = var.vnet.address_space
dns_servers = var.vnet.dns_servers
subnet {
name = var.vnet.subnet1_name
address_prefix = var.vnet.subnet1_address_prefix
}
subnet {
name = var.vnet.subnet2_name
address_prefix = var.vnet.subnet2_address_prefix
}
subnet {
name = var.vnet.subnet3_name
address_prefix = var.vnet.subnet3_address_prefix
}
subnet {
name = var.vnet.subnet4_name
address_prefix = var.vnet.subnet4_address_prefix
}
subnet {
name = var.vnet.subnet5_name
address_prefix = var.vnet.subnet5_address_prefix
}
}
########################
# ROUTE TABLE CREATION #
########################
resource "azurerm_route_table" "fslogix_rt" {
name = var.rt.fslogix.name
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
disable_bgp_route_propagation = false
}
resource "azurerm_route_table" "back_office_rt" {
name = var.rt.back_office.name
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
disable_bgp_route_propagation = false
}
resource "azurerm_route_table" "dev_dev_rt" {
name = var.rt.dev_dev.name
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
disable_bgp_route_propagation = false
}
resource "azurerm_route_table" "dev_prod_rt" {
name = var.rt.dev_prod.name
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
disable_bgp_route_propagation = false
}
resource "azurerm_route_table" "front_office_rt" {
name = var.rt.front_office.name
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
disable_bgp_route_propagation = false
}
##################
# ROUTE CREATION #
##################
resource "azurerm_route" "fslogix_routes" {
for_each = var.fslogix_routes
name = each.value.name
resource_group_name = azurerm_resource_group.rg.name
route_table_name = azurerm_route_table.fslogix_rt.name
address_prefix = each.value.address_prefix
next_hop_type = each.value.next_hop_type
next_hop_in_ip_address = each.value.next_hop_in_ip_address
}
resource "azurerm_route" "back_office_routes" {
for_each = var.back_office_routes
name = each.value.name
resource_group_name = azurerm_resource_group.rg.name
route_table_name = azurerm_route_table.back_office_rt.name
address_prefix = each.value.address_prefix
next_hop_type = each.value.next_hop_type
next_hop_in_ip_address = each.value.next_hop_in_ip_address
}
resource "azurerm_route" "dev_dev_routes" {
for_each = var.developer_dev_routes
name = each.value.name
resource_group_name = azurerm_resource_group.rg.name
route_table_name = azurerm_route_table.dev_dev_rt.name
address_prefix = each.value.address_prefix
next_hop_type = each.value.next_hop_type
next_hop_in_ip_address = each.value.next_hop_in_ip_address
}
resource "azurerm_route" "dev_prod_routes" {
for_each = var.developer_prod_routes
name = each.value.name
resource_group_name = azurerm_resource_group.rg.name
route_table_name = azurerm_route_table.dev_prod_rt.name
address_prefix = each.value.address_prefix
next_hop_type = each.value.next_hop_type
next_hop_in_ip_address = each.value.next_hop_in_ip_address
}
resource "azurerm_route" "front_office_routes" {
for_each = var.front_office_routes
name = each.value.name
resource_group_name = azurerm_resource_group.rg.name
route_table_name = azurerm_route_table.front_office_rt.name
address_prefix = each.value.address_prefix
next_hop_type = each.value.next_hop_type
next_hop_in_ip_address = each.value.next_hop_in_ip_address
}
###########################
# ROUTE TABLE ASSOICATION #
###########################
resource "azurerm_subnet_route_table_association" "fslogix_rta" {
subnet_id = ???????????????????????????????????????
route_table_id = azurerm_route_table.fslogix_rt.id
}
You can get all the subnet_id with:
azurerm_virtual_network.vnet.subnet.*.id
You can get the first subnet_id with:
azurerm_virtual_network.vnet.subnet.*.id[0]
And the second subnet_id with:
azurerm_virtual_network.vnet.subnet.*.id[1]
And go on...
Hope this helps!
Im quite new to Terraform so maybe i make a very basic mistake but after multiple hours maybe here someone can help me out.
So i tried to peer to vlans together. I viewed multiple tutorials about it and the only difference between my configuration i can see is that i want to make a peering between 2 vlans that are in 2 different resource groups. I also noticed that if i put the peering in one of the 2 vlan ressource groups i have fever errors.
error
#Creating Resource Groups
resource "azurerm_resource_group" "network" {
name = "network"
location = "West Europe"
}
resource "azurerm_resource_group" "front" {
name = "front"
location = "West Europe"
}
resource "azurerm_resource_group" "middle" {
name = "middle"
location = "West Europe"
}
resource "azurerm_resource_group" "back" {
name = "back"
location = "West Europe"
}
resource "azurerm_resource_group" "peerings" {
name = "peerings"
location = "West Europe"
}
#Creating Virtual Networks
resource "azurerm_virtual_network" "network" {
name = "network"
location = azurerm_resource_group.network.location
resource_group_name = azurerm_resource_group.network.name
address_space = ["10.1.0.0/16"]
subnet {
name = "default"
address_prefix = "10.1.0.0/24"
}
subnet {
name = "gatewaysubnet"
address_prefix = "10.1.1.0/24"
}
subnet {
name = "azurefirewallsubnet"
address_prefix = "10.1.3.0/24"
}
subnet {
name = "azurebastionsubnet"
address_prefix = "10.1.2.0/24"
}
}
resource "azurerm_virtual_network" "front" {
name = "network"
location = azurerm_resource_group.front.location
resource_group_name = azurerm_resource_group.front.name
address_space = ["10.2.0.0/16"]
}
resource "azurerm_virtual_network" "middle" {
name = "network"
location = azurerm_resource_group.middle.location
resource_group_name = azurerm_resource_group.middle.name
address_space = ["10.3.0.0/16"]
}
resource "azurerm_virtual_network" "back" {
name = "network"
location = azurerm_resource_group.back.location
resource_group_name = azurerm_resource_group.back.name
address_space = ["10.4.0.0/16"]
}
#Create peerings
#network <--> front
resource "azurerm_virtual_network_peering" "networktofront" {
name = "networktofront"
resource_group_name = azurerm_resource_group.peerings.name
virtual_network_name = azurerm_virtual_network.network.name
remote_virtual_network_id = azurerm_virtual_network.front.id
}
resource "azurerm_virtual_network_peering" "fronttonetwork" {
name = "fronttonetwork"
resource_group_name = azurerm_resource_group.peerings.name
virtual_network_name = azurerm_virtual_network.front.name
remote_virtual_network_id = azurerm_virtual_network.network.id
}
#network <--> middle
resource "azurerm_virtual_network_peering" "networktomiddle" {
name = "networktomiddle"
resource_group_name = azurerm_resource_group.peerings.name
virtual_network_name = azurerm_virtual_network.network.name
remote_virtual_network_id = azurerm_virtual_network.middle.id
}
resource "azurerm_virtual_network_peering" "middletonetwork" {
name = "middletonetwork"
resource_group_name = azurerm_resource_group.peerings.name
virtual_network_name = azurerm_virtual_network.middle.name
remote_virtual_network_id = azurerm_virtual_network.network.id
}
#network <--> back
resource "azurerm_virtual_network_peering" "networktoback" {
name = "networktoback"
resource_group_name = azurerm_resource_group.peerings.name
virtual_network_name = azurerm_virtual_network.network.name
remote_virtual_network_id = azurerm_virtual_network.back.id
}
resource "azurerm_virtual_network_peering" "backtonetwork" {
name = "backtonetwork"
resource_group_name = azurerm_resource_group.peerings.name
virtual_network_name = azurerm_virtual_network.back.name
remote_virtual_network_id = azurerm_virtual_network.network.id
}
Virtual Network Peerings are a subset of the Virtual Network Resource (Microsoft.Network/virtualNetworks/network/virtualNetworkPeerings) and it is therefore not possible to carve these out into different resource groups.
Besides that, your code is accurate and should work as soon as you create the peerings in the corresponding virtual network resource groups:
#Creating Resource Groups
resource "azurerm_resource_group" "network" {
name = "network"
location = "West Europe"
}
resource "azurerm_resource_group" "front" {
name = "front"
location = "West Europe"
}
resource "azurerm_resource_group" "middle" {
name = "middle"
location = "West Europe"
}
resource "azurerm_resource_group" "back" {
name = "back"
location = "West Europe"
}
#Creating Virtual Networks
resource "azurerm_virtual_network" "network" {
name = "network"
location = azurerm_resource_group.network.location
resource_group_name = azurerm_resource_group.network.name
address_space = ["10.1.0.0/16"]
subnet {
name = "default"
address_prefix = "10.1.0.0/24"
}
subnet {
name = "gatewaysubnet"
address_prefix = "10.1.1.0/24"
}
subnet {
name = "azurefirewallsubnet"
address_prefix = "10.1.3.0/24"
}
subnet {
name = "azurebastionsubnet"
address_prefix = "10.1.2.0/24"
}
}
resource "azurerm_virtual_network" "front" {
name = "network"
location = azurerm_resource_group.front.location
resource_group_name = azurerm_resource_group.front.name
address_space = ["10.2.0.0/16"]
}
resource "azurerm_virtual_network" "middle" {
name = "network"
location = azurerm_resource_group.middle.location
resource_group_name = azurerm_resource_group.middle.name
address_space = ["10.3.0.0/16"]
}
resource "azurerm_virtual_network" "back" {
name = "network"
location = azurerm_resource_group.back.location
resource_group_name = azurerm_resource_group.back.name
address_space = ["10.4.0.0/16"]
}
#Create peerings
#network <--> front
resource "azurerm_virtual_network_peering" "networktofront" {
name = "networktofront"
resource_group_name = azurerm_resource_group.network.name
virtual_network_name = azurerm_virtual_network.network.name
remote_virtual_network_id = azurerm_virtual_network.front.id
}
resource "azurerm_virtual_network_peering" "fronttonetwork" {
name = "fronttonetwork"
resource_group_name = azurerm_resource_group.front.name
virtual_network_name = azurerm_virtual_network.front.name
remote_virtual_network_id = azurerm_virtual_network.network.id
}
#network <--> middle
resource "azurerm_virtual_network_peering" "networktomiddle" {
name = "networktomiddle"
resource_group_name = azurerm_resource_group.network.name
virtual_network_name = azurerm_virtual_network.network.name
remote_virtual_network_id = azurerm_virtual_network.middle.id
}
resource "azurerm_virtual_network_peering" "middletonetwork" {
name = "middletonetwork"
resource_group_name = azurerm_resource_group.middle.name
virtual_network_name = azurerm_virtual_network.middle.name
remote_virtual_network_id = azurerm_virtual_network.network.id
}
#network <--> back
resource "azurerm_virtual_network_peering" "networktoback" {
name = "networktoback"
resource_group_name = azurerm_resource_group.network.name
virtual_network_name = azurerm_virtual_network.network.name
remote_virtual_network_id = azurerm_virtual_network.back.id
}
resource "azurerm_virtual_network_peering" "backtonetwork" {
name = "backtonetwork"
resource_group_name = azurerm_resource_group.back.name
virtual_network_name = azurerm_virtual_network.back.name
remote_virtual_network_id = azurerm_virtual_network.network.id
}
Varible.tf
variable "vnet" {
type = map(any)
description = "creating rg and vmet"
default = {
"rg1" = {
vnet_name = "vnet1"
address = ["10.0.0.0/16"]
subnet_name = ["snet1", "snet2"]
subnet_address = ["10.1.0.0/24", "10.2.0.0/24"]
location = "south india"
}
}
}
main.tf
resource "azurerm_subnet" "mysubnet" {
for_each = var.vnet
name = each.value["subnet_name"]
address_prefixes = each.value["subnet_address"]
address_prefixes = each.value["subnet_address"]
virtual_network_name = each.value["vnet_name"]
resource_group_name = each.key
}
Error:
Error: Incorrect attribute value type
on main.tf line 25, in resource "azurerm_subnet" "mysubnet":
name = each.value["subnet_name"]
each.value["subnet_name"] is tuple with 2 elements
Inappropriate value for attribute "name": string required.
How to iterate to create multiple subnet ?
You have to flatten your variable first. For example:
locals {
vnet_flat = merge([
for group_name, details in var.vnet:
{for idx in range(length(details.subnet_name)):
"${group_name}-${idx}" => {
group_name = group_name
vnet_name = details.vnet_name
address = details.address
subnet_name = details.subnet_name[idx]
subnet_address = details.subnet_address[idx]
location = details.location
}
}
]...)
}
resource "azurerm_subnet" "mysubnet" {
for_each = local.vnet_flat
name = each.value.subnet_name
address_prefixes = [each.value.subnet_address]
virtual_network_name = each.value.vnet_name
resource_group_name = each.value.group_name
}
The ... is for Expanding Function Arguments.
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"
}
}
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
}
}
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