Network Security Groups (NSGs) in Azure provide filtering of network traffic to and from Azure resources within an Azure virtual network. When a subnet is not configured with an NSG, it lacks essential security controls that filter inbound and outbound traffic, potentially exposing resources to unauthorized access and network-based attacks.
A subnet is protected in one of two ways, and the rule checks both:
For subnets defined inline in an azurerm_virtual_network, the security_group attribute must be set to a non-empty NSG reference.
For standalone azurerm_subnet resources, an azurerm_subnet_network_security_group_association must reference the subnet.
Subnets that Azure doesn’t allow an NSG on (GatewaySubnet, AzureFirewallSubnet, AzureFirewallManagementSubnet and RouteServerSubnet) are not reported. Without this protection, workloads running in these subnets may be accessible from unwanted network sources, increasing the attack surface.
resource"azurerm_virtual_network""example"{name="example-network"address_space=["10.1.2.0/24"]location="West US"resource_group_name="example-rg"subnet{name="subnet1"address_prefix="10.1.2.0/25" // Missing security_group attribute
}}// No azurerm_subnet_network_security_group_association references this subnet
resource"azurerm_subnet""example"{name="subnet2"resource_group_name="example-rg"virtual_network_name="example-network"address_prefixes=["10.1.2.128/25"]}
Compliant Code Examples
#this code is a correct code for which the query should not find any result
resource"azurerm_virtual_network""negative1"{name="test-network"address_space=["10.1.2.0/24"]location="West US"resource_group_name="rg"subnet{name="subnet1"address_prefix="10.1.2.0/25"security_group="a"}}resource"azurerm_subnet""negative2"{name="subnet2"resource_group_name="rg"virtual_network_name="test-network"address_prefixes=["10.1.2.0/25"]}resource"azurerm_subnet_network_security_group_association""negative2"{subnet_id=azurerm_subnet.negative2.idnetwork_security_group_id="nsg-id"}resource"azurerm_subnet""negative3"{name="GatewaySubnet"resource_group_name="rg"virtual_network_name="test-network"address_prefixes=["10.1.3.0/27"]}resource"azurerm_subnet""negative4"{count=2name="subnet4-${count.index}"resource_group_name="rg"virtual_network_name="test-network"address_prefixes=["10.1.4.0/27"]}resource"azurerm_subnet_network_security_group_association""negative4"{count=2subnet_id=azurerm_subnet.negative4[count.index].idnetwork_security_group_id="nsg-id"}resource"azurerm_subnet""negative5"{for_each=toset(["a","b"])name=each.keyresource_group_name="rg"virtual_network_name="test-network"address_prefixes=["10.1.5.0/27"]}resource"azurerm_subnet_network_security_group_association""negative5"{for_each=azurerm_subnet.negative5subnet_id=each.value.idnetwork_security_group_id="nsg-id"}
Non-Compliant Code Examples
#this is a problematic code where the query should report a result(s)
resource"azurerm_virtual_network""positive1"{name="test-network"address_space=["10.1.2.0/24"]location="West US"resource_group_name="rg"subnet{name="subnet1"address_prefix="10.1.2.0/25"}}resource"azurerm_virtual_network""positive2"{name="test-network"address_space=["10.1.2.0/24"]location="West US"resource_group_name="rg"subnet{name="subnet1"address_prefix="10.1.2.0/25"security_group=""}}resource"azurerm_subnet""positive3"{name="subnet3"resource_group_name="rg"virtual_network_name="test-network"address_prefixes=["10.1.2.0/25"]}
1
2
rulesets:- Terraform / Azure # Rules to enforce / Azure.
Request a personalized demo
Get Started with Datadog
Ask AI
AI-generated responses may be inaccurate. Verify important info.