Azure Network Security Group

This page shows how to write Terraform and Azure Resource Manager for Network Security Group and write them securely.

azurerm_network_security_group (Terraform)

The Security Group in Network can be configured in Terraform with the resource name azurerm_network_security_group. The following sections describe 10 examples of how to use the resource and its parameters.

Example Usage from GitHub

security_group.tf#L1
resource "azurerm_network_security_group" "private_nsg" {
  name                = "private-nsg"
  location            = data.terraform_remote_state.rg.outputs.rg_location
  resource_group_name = data.terraform_remote_state.rg.outputs.rg_name
  tags                = var.tags
}
vnet.tf#L34
resource "azurerm_network_security_group" "controller_net" {
  name                = local.controller_net_nsg
  location            = var.location
  resource_group_name = azurerm_resource_group.boundary.name
}

nsg.tf#L3
resource "azurerm_network_security_group" "tf_main_subnet_a_access_sg" {
  name                = "az_main_subnet_a_access_nsg"
  resource_group_name = var.rg_name
  location            = var.tf_sample_location
}

nsg.tf#L3
resource "azurerm_network_security_group" "tf_main_subnet_a_access_sg" {
  name                = "az_main_subnet_a_access_nsg"
  resource_group_name = var.rg_name
  location            = var.tf_sample_location
}

vnet.tf#L33
resource "azurerm_network_security_group" "controller_net" {
  name                = local.controller_net_nsg
  location            = var.location
  resource_group_name = azurerm_resource_group.boundary.name
}

main_network_1.tf#L7
resource "azurerm_network_security_group" "nsg-bastionsubnet-vnet-1" {
  name                  = "nsg-bastionsubnet-vnet-sn-001"
  location              = azurerm_resource_group.rg-vnet-1.location
  resource_group_name   = azurerm_resource_group.rg-vnet-1.name
  tags = var.resource_tags

sgs.tf#L4
resource "azurerm_network_security_group" "mySecGroup" {
    name                = "sshtraffic"
    location            = azurerm_resource_group.rg.location
    resource_group_name = azurerm_resource_group.rg.name

    security_rule {
nsgs.tf#L1
resource "azurerm_network_security_group" "dk-01-sub-pub-nsg" {
  location            = azurerm_resource_group.dk-01-rg.location
  resource_group_name = azurerm_resource_group.dk-01-rg.name
  name                = "dk-01-sub-pub-nsg"

  security_rule {
security-groups.tf#L1
resource "azurerm_network_security_group" "jump-server" {
  name                = var.azure_jump_server_nsg_name
  location            = azurerm_resource_group.tanzu-resource-group.location
  resource_group_name = azurerm_resource_group.tanzu-resource-group.name

  security_rule {
05_nsg.tf#L1
resource "azurerm_network_security_group" "jisoo_nsg1" {
    name = "jisoo-nsg1"
    location = azurerm_resource_group.jisoo_rg.location
    resource_group_name = azurerm_resource_group.jisoo_rg.name

    security_rule {

Review your Terraform file for Azure best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

Security Best Practices for azurerm_network_security_group

There are 2 settings in azurerm_network_security_group that should be taken care of for security reasons. The following section explain an overview and example code.

risk-label

Ensure to disable RDP port from the Internet

It is better to disable the RDP port from the Internet. RDP access should not be accepted from the Internet (*, 0.0.0.0, /0, internet, any), and consider using the Azure Bastion Service.

risk-label

Ensure to disable SSH port from internet

It is better to disable the SSH port from the Internet. SSH access should not be accepted from the Internet (*, 0.0.0.0, /0, internet, any), and consider using the Azure Bastion Service.

Review your Azure Network settings

You can check if the azurerm_network_security_group setting in your .tf file is correct in 3 min with Shisho Cloud.

Parameters

Explanation in Terraform Registry

Manages a network security group that contains a list of network security rules. Network security groups enable inbound or outbound traffic to be enabled or denied.

NOTE on Network Security Groups and Network Security Rules: Terraform currently provides both a standalone Network Security Rule resource, and allows for Network Security Rules to be defined in-line within the Network Security Group resource. At this time you cannot use a Network Security Group with in-line Network Security Rules in conjunction with any Network Security Rule resources. Doing so will cause a conflict of rule settings and will overwrite rules.

Tips: Best Practices for The Other Azure Network Resources

In addition to the azurerm_network_security_rule, Azure Network has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.

risk-label

azurerm_network_security_rule

Ensure to set a more restrictive CIDR range for ingress from the internet

It is better to set a more restrictive CIDR range not to use very broad subnets. If possible, segments should be divided into smaller subnets.

risk-label

azurerm_network_watcher_flow_log

Ensure to enable Retention policy for flow logs and set it to enough duration

It is better to enable a retention policy for flow logs. Flow logs show us all network activity in the cloud environment and support us when we face critical incidents.

Review your Azure Network settings

In addition to the above, there are other security points you should be aware of making sure that your .tf files are protected in Shisho Cloud.

Microsoft.Network/networkSecurityGroups (Azure Resource Manager)

The networkSecurityGroups in Microsoft.Network can be configured in Azure Resource Manager with the resource name Microsoft.Network/networkSecurityGroups. The following sections describe how to use the resource and its parameters.

Example Usage from GitHub

NetworkSecurityGroupTest.test_open_ports.json#L41
                                "type": "Microsoft.Network/networkSecurityGroups",
                                "location": "eastus",
                                "properties": {
                                    "provisioningState": "Succeeded",
                                    "resourceGuid": "94a0c0a4-cdba-45b0-b2b1-b6f602d8981e",
                                    "securityRules": [
NetworkSecurityGroupTest.test_egress_policy_protocols.json#L41
                                "type": "Microsoft.Network/networkSecurityGroups",
                                "location": "eastus",
                                "properties": {
                                    "provisioningState": "Succeeded",
                                    "resourceGuid": "94a0c0a4-cdba-45b0-b2b1-b6f602d8981e",
                                    "securityRules": [
NetworkSecurityGroupTest.test_find_by_name.json#L41
                                "type": "Microsoft.Network/networkSecurityGroups",
                                "location": "eastus",
                                "properties": {
                                    "provisioningState": "Succeeded",
                                    "resourceGuid": "94a0c0a4-cdba-45b0-b2b1-b6f602d8981e",
                                    "securityRules": [
NetworkSecurityGroupTest.test_allow_ports_range_any.json#L41
                                "type": "Microsoft.Network/networkSecurityGroups",
                                "location": "eastus",
                                "properties": {
                                    "provisioningState": "Succeeded",
                                    "resourceGuid": "94a0c0a4-cdba-45b0-b2b1-b6f602d8981e",
                                    "securityRules": [
NetworkSecurityGroupTest.test_deny_port.json#L41
                                "type": "Microsoft.Network/networkSecurityGroups",
                                "location": "eastus",
                                "properties": {
                                    "provisioningState": "Succeeded",
                                    "resourceGuid": "94a0c0a4-cdba-45b0-b2b1-b6f602d8981e",
                                    "securityRules": [
NetworkSecurityGroupTest.test_allow_single_port.json#L41
                                "type": "Microsoft.Network/networkSecurityGroups",
                                "location": "eastus",
                                "properties": {
                                    "provisioningState": "Succeeded",
                                    "resourceGuid": "94a0c0a4-cdba-45b0-b2b1-b6f602d8981e",
                                    "securityRules": [
NetworkSecurityGroupTest.test_allow_multiple_ports.json#L41
                                "type": "Microsoft.Network/networkSecurityGroups",
                                "location": "eastus",
                                "properties": {
                                    "provisioningState": "Succeeded",
                                    "resourceGuid": "94a0c0a4-cdba-45b0-b2b1-b6f602d8981e",
                                    "securityRules": [
NetworkSecurityGroupTest.test_open_ports.json#L41
                                "type": "Microsoft.Network/networkSecurityGroups",
                                "location": "eastus",
                                "properties": {
                                    "provisioningState": "Succeeded",
                                    "resourceGuid": "94a0c0a4-cdba-45b0-b2b1-b6f602d8981e",
                                    "securityRules": [
network.template.json#L27
            "type": "Microsoft.Network/networkSecurityGroups",
            "apiVersion": "2020-11-01",
            "location": "[resourceGroup().location]",
            "properties": {
                "securityRules": [
                    {
NetworkSecurityGroupTest.test_egress_policy_protocols.json#L41
                                "type": "Microsoft.Network/networkSecurityGroups",
                                "location": "eastus",
                                "properties": {
                                    "provisioningState": "Succeeded",
                                    "resourceGuid": "94a0c0a4-cdba-45b0-b2b1-b6f602d8981e",
                                    "securityRules": [

Parameters

  • name required - string
  • type required - string
  • apiVersion required - string
  • location required - string

    Resource location.

  • tags optional - string

    Resource tags.

  • properties required
      • securityRules optional array
          • properties optional
              • description optional - string

                A description for this rule. Restricted to 140 chars.

              • protocol required - string

                Network protocol this rule applies to.

              • sourcePortRange optional - string

                The source port or range. Integer or range between 0 and 65535. Asterisk '*' can also be used to match all ports.

              • destinationPortRange optional - string

                The destination port or range. Integer or range between 0 and 65535. Asterisk '*' can also be used to match all ports.

              • sourceAddressPrefix optional - string

                The CIDR or source IP range. Asterisk '*' can also be used to match all source IPs. Default tags such as 'VirtualNetwork', 'AzureLoadBalancer' and 'Internet' can also be used. If this is an ingress rule, specifies where network traffic originates from.

              • sourceAddressPrefixes optional - array

                The CIDR or source IP ranges.

              • sourceApplicationSecurityGroups optional array
                  • id required - string

                    Resource ID.

              • destinationAddressPrefix optional - string

                The destination address prefix. CIDR or destination IP range. Asterisk '*' can also be used to match all source IPs. Default tags such as 'VirtualNetwork', 'AzureLoadBalancer' and 'Internet' can also be used.

              • destinationAddressPrefixes optional - array

                The destination address prefixes. CIDR or destination IP ranges.

              • destinationApplicationSecurityGroups optional array
                  • id required - string

                    Resource ID.

              • sourcePortRanges optional - array

                The source port ranges.

              • destinationPortRanges optional - array

                The destination port ranges.

              • access required - string

                The network traffic is allowed or denied.

              • priority required - integer

                The priority of the rule. The value can be between 100 and 4096. The priority number must be unique for each rule in the collection. The lower the priority number, the higher the priority of the rule.

              • direction required - string

                The direction of the rule. The direction specifies if rule will be evaluated on incoming or outgoing traffic.

          • name optional - string

            The name of the resource that is unique within a resource group. This name can be used to access the resource.

Frequently asked questions

What is Azure Network Security Group?

Azure Network Security Group is a resource for Network of Microsoft Azure. Settings can be wrote in Terraform.

Where can I find the example code for the Azure Network Security Group?

For Terraform, the claudior57/azure-iac, hashicorp/boundary-reference-architecture and sorabhm/tf-samples source code examples are useful. See the Terraform Example section for further details.

For Azure Resource Manager, the LRuttenCN/cloud-custodian, LRuttenCN/cloud-custodian and LRuttenCN/cloud-custodian source code examples are useful. See the Azure Resource Manager Example section for further details.