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
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
}
resource "azurerm_network_security_group" "controller_net" {
name = local.controller_net_nsg
location = var.location
resource_group_name = azurerm_resource_group.boundary.name
}
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
}
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
}
resource "azurerm_network_security_group" "controller_net" {
name = local.controller_net_nsg
location = var.location
resource_group_name = azurerm_resource_group.boundary.name
}
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
resource "azurerm_network_security_group" "mySecGroup" {
name = "sshtraffic"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
security_rule {
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 {
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 {
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 {
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.
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.
Ensure to disable RDP port from 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.
Parameters
-
id
optional computed - string -
location
required - string -
name
required - string -
resource_group_name
required - string -
security_rule
optional computed - set of object-
access
- string -
description
- string -
destination_address_prefix
- string -
destination_address_prefixes
- set of string -
destination_application_security_group_ids
- set of string -
destination_port_range
- string -
destination_port_ranges
- set of string -
direction
- string -
name
- string -
priority
- number -
protocol
- string -
source_address_prefix
- string -
source_address_prefixes
- set of string -
source_application_security_group_ids
- set of string -
source_port_range
- string -
source_port_ranges
- set of string
-
-
tags
optional - map from string to string -
timeouts
single block
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.
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.
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.
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
"type": "Microsoft.Network/networkSecurityGroups",
"location": "eastus",
"properties": {
"provisioningState": "Succeeded",
"resourceGuid": "94a0c0a4-cdba-45b0-b2b1-b6f602d8981e",
"securityRules": [
"type": "Microsoft.Network/networkSecurityGroups",
"location": "eastus",
"properties": {
"provisioningState": "Succeeded",
"resourceGuid": "94a0c0a4-cdba-45b0-b2b1-b6f602d8981e",
"securityRules": [
"type": "Microsoft.Network/networkSecurityGroups",
"location": "eastus",
"properties": {
"provisioningState": "Succeeded",
"resourceGuid": "94a0c0a4-cdba-45b0-b2b1-b6f602d8981e",
"securityRules": [
"type": "Microsoft.Network/networkSecurityGroups",
"location": "eastus",
"properties": {
"provisioningState": "Succeeded",
"resourceGuid": "94a0c0a4-cdba-45b0-b2b1-b6f602d8981e",
"securityRules": [
"type": "Microsoft.Network/networkSecurityGroups",
"location": "eastus",
"properties": {
"provisioningState": "Succeeded",
"resourceGuid": "94a0c0a4-cdba-45b0-b2b1-b6f602d8981e",
"securityRules": [
"type": "Microsoft.Network/networkSecurityGroups",
"location": "eastus",
"properties": {
"provisioningState": "Succeeded",
"resourceGuid": "94a0c0a4-cdba-45b0-b2b1-b6f602d8981e",
"securityRules": [
"type": "Microsoft.Network/networkSecurityGroups",
"location": "eastus",
"properties": {
"provisioningState": "Succeeded",
"resourceGuid": "94a0c0a4-cdba-45b0-b2b1-b6f602d8981e",
"securityRules": [
"type": "Microsoft.Network/networkSecurityGroups",
"location": "eastus",
"properties": {
"provisioningState": "Succeeded",
"resourceGuid": "94a0c0a4-cdba-45b0-b2b1-b6f602d8981e",
"securityRules": [
"type": "Microsoft.Network/networkSecurityGroups",
"apiVersion": "2020-11-01",
"location": "[resourceGroup().location]",
"properties": {
"securityRules": [
{
"type": "Microsoft.Network/networkSecurityGroups",
"location": "eastus",
"properties": {
"provisioningState": "Succeeded",
"resourceGuid": "94a0c0a4-cdba-45b0-b2b1-b6f602d8981e",
"securityRules": [
Parameters
name
required - stringtype
required - stringapiVersion
required - stringlocation
required - stringResource location.
tags
optional - stringResource tags.
properties
requiredsecurityRules
optional arrayproperties
optionaldescription
optional - stringA description for this rule. Restricted to 140 chars.
protocol
required - stringNetwork protocol this rule applies to.
sourcePortRange
optional - stringThe source port or range. Integer or range between 0 and 65535. Asterisk '*' can also be used to match all ports.
destinationPortRange
optional - stringThe destination port or range. Integer or range between 0 and 65535. Asterisk '*' can also be used to match all ports.
sourceAddressPrefix
optional - stringThe 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 - arrayThe CIDR or source IP ranges.
sourceApplicationSecurityGroups
optional arrayid
required - stringResource ID.
destinationAddressPrefix
optional - stringThe 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 - arrayThe destination address prefixes. CIDR or destination IP ranges.
destinationApplicationSecurityGroups
optional arrayid
required - stringResource ID.
sourcePortRanges
optional - arrayThe source port ranges.
destinationPortRanges
optional - arrayThe destination port ranges.
access
required - stringThe network traffic is allowed or denied.
priority
required - integerThe 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 - stringThe direction of the rule. The direction specifies if rule will be evaluated on incoming or outgoing traffic.
name
optional - stringThe 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.