I would like to do this:
resource "azurerm_subnet" "database" {
name = "DatabaseSubnet"
resource_group_name = var.resource_group_name
virtual_network_name = azurerm_virtual_network.vnet_hub.name
# this ranges from 10.0.3.1 through 10.0.3.254
address_prefixes = ["10.0.3.0/24"]
service_endpoints = ["Microsoft.Storage"]
private_endpoint_network_policies_enabled = false
private_link_service_network_policies_enabled = false
delegation {
name = "fs"
service_delegation {
name = "Microsoft.DBforPostgreSQL/flexibleServers"
actions = [
"Microsoft.Network/virtualNetworks/subnets/join/action",
]
}
}
}
resource "azurerm_subnet_network_security_group_association" "private_database" {
subnet_id = azurerm_subnet.database.id
network_security_group_id = var.private_subnet_nsg_id
}
However, I get a RequestDisallowedByPolicy
error, more specifically
Subnets should have a Network Security Group
I know I can create subnets directly within an azurerm_virtual_network
resource, like this:
resource "azurerm_virtual_network" "vnet_hub" {
name = "HubVnet"
location = var.location
resource_group_name = var.resource_group_name
# this ranges from 10.0.0.1 through 10.0.3.254
address_space = ["10.0.0.0/22"]
tags = var.tags
subnet {
name = "AzureFirewallSubnet"
address_prefix = "10.0.0.0/24"
}
subnet {
name = "JumpboxSubnet"
address_prefix = "10.0.1.0/24"
security_group = var.public_subnet_nsg_id
}
subnet {
name = "ResourcesSubnet"
address_prefix = "10.0.2.0/24"
security_group = var.private_subnet_nsg_id
}
}
That includes a network security group straight away. However, I really need the delegation
feature for my postgres server, and I have not been able to find a way to make that happen without defining a subnet outside of the azurerm_virtual_network
.
It seems like azure doesn’t like the idea to create the subnet without network security group, and then associating that subnet with an actual network security group.
What can I do?