---
title: Security group is not configured
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: >-
  Docs > Datadog Security > Code Security > Infrastructure as Code (IaC)
  Security > IaC Security Rules > Security group is not configured
---

> For the complete documentation index, see [llms.txt](https://docs.datadoghq.com/llms.txt).

# Security group is not configured

{% callout %}
# Important note for users on the following Datadog sites: app.ddog-gov.com, us2.ddog-gov.com

{% alert level="danger" %}
This product is not supported for your selected [Datadog site](https://docs.datadoghq.com/getting_started/site.md). ({% placeholder "user-datadog-site-name" /%}).
{% /alert %}

{% /callout %}

## Metadata{% #metadata %}

**Id:** `terraform-azure-security-group-is-not-configured` 

**Provider:** Azure

**Platform:** Terraform

**Severity:** High

**Category:** Insecure Configurations

#### Learn More{% #learn-more %}

- [Provider Reference](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_network)

### Description{% #description %}

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.

Secure example:

```terraform
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"
    security_group = "securitygroup-id"
  }
}

resource "azurerm_subnet" "example" {
  name                 = "subnet2"
  resource_group_name  = "example-rg"
  virtual_network_name = "example-network"
  address_prefixes     = ["10.1.2.128/25"]
}

resource "azurerm_subnet_network_security_group_association" "example" {
  subnet_id                 = azurerm_subnet.example.id
  network_security_group_id = "securitygroup-id"
}
```

Vulnerable example:

```terraform
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{% #compliant-code-examples %}

```terraform
#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.id
  network_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                = 2
  name                 = "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                     = 2
  subnet_id                 = azurerm_subnet.negative4[count.index].id
  network_security_group_id = "nsg-id"
}

resource "azurerm_subnet" "negative5" {
  for_each             = toset(["a", "b"])
  name                 = each.key
  resource_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.negative5
  subnet_id                 = each.value.id
  network_security_group_id = "nsg-id"
}
```

## Non-Compliant Code Examples{% #non-compliant-code-examples %}

```terraform
#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"]
}
```
