Azure Network Virtual Network

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

azurerm_virtual_network (Terraform)

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

Example Usage from GitHub

main.tf#L18
resource "azurerm_virtual_network" "hub" {
  name                = "hub"
  resource_group_name = azurerm_resource_group.test.name
  address_space       = ["10.1.0.0/16"]
  location            = "East US"

main.tf#L31
resource "azurerm_virtual_network" "ungle_vnet" {
  # Manually imported through "terraform import" command
  name = "ungle_vnet"
  resource_group_name = azurerm_resource_group.ungle_rg.name
  location = "uksouth"
  address_space = ["10.2.0.0/16"]
main.tf#L37
resource "azurerm_virtual_network" "vnet1" {
  name                = "CoreServicesVnet"
  location            = azurerm_resource_group.group1.location
  resource_group_name = azurerm_resource_group.group1.name
  address_space       = ["10.20.0.0/16"]

main.tf#L10
resource "azurerm_virtual_network" "example1" {
  name                = "virtualNetwork1"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  address_space       = ["10.0.0.0/16"]
}
network.tf#L2
resource "azurerm_virtual_network" "HUB" {
  name                = "HUB"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  address_space       = ["10.0.0.0/16"]
}
main.tf#L6
resource "azurerm_virtual_network" "vnet1" {
  name                = "virtual-network-1"
  resource_group_name = azurerm_resource_group.rg.name
  location            = azurerm_resource_group.rg.location
  address_space       = ["10.1.0.0/16"]
}
networks.tf#L2
resource "azurerm_virtual_network" "VNet-HUB" {
    resource_group_name = azurerm_resource_group.terraform-testing.name
    location = azurerm_resource_group.terraform-testing.location
    name = "terraform-hubvnet"
    address_space = [ var.iphubvnet ]
}
network.tf#L3
resource "azurerm_virtual_network" "tf_main_vnet" {
  name                = "tf_vnet_web"
  location            = var.tf_sample_location
  address_space       = ["10.0.0.0/16"]
  resource_group_name = var.rg_name
}
use.tf#L7
resource "azurerm_virtual_network" "ondo-p-use-hub-vnet" {
    name                = "ondo-p-use-hub-vnet"
    address_space       = ["172.20.1.0/24"]
    location            = azurerm_resource_group.ondo-p-use-hub-rg.location
    resource_group_name = azurerm_resource_group.ondo-p-use-hub-rg.name
}
vnets.tf#L6
resource "azurerm_virtual_network" "vnet_mgmt" {
  name                = "vnet-mgmt"
  resource_group_name = azurerm_resource_group.rg_vnet.name
  location            = azurerm_resource_group.rg_vnet.location
  address_space       = ["10.255.0.0/16"]
}

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).

Parameters

Explanation in Terraform Registry

Manages a virtual network including any configured subnets. Each subnet can optionally be configured with a security group to be associated with the subnet.

NOTE on Virtual Networks and Subnets: Terraform currently provides both a standalone Subnet resource, and allows for Subnets to be defined in-line within the Virtual Network resource. At this time you cannot use a Virtual Network with in-line Subnets in conjunction with any Subnet resources. Doing so will cause a conflict of Subnet configurations and will overwrite subnets.

NOTE on Virtual Networks and DNS Servers: Terraform currently provides both a standalone virtual network DNS Servers resource, and allows for DNS servers to be defined in-line within the Virtual Network resource. At this time you cannot use a Virtual Network with in-line DNS servers in conjunction with any Virtual Network DNS Servers resources. Doing so will cause a conflict of Virtual Network DNS Servers configurations and will overwrite virtual networks DNS servers.

Tips: Best Practices for The Other Azure Network Resources

In addition to the azurerm_network_security_group, 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_group

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

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/virtualNetworks (Azure Resource Manager)

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

Example Usage from GitHub

template.json#L21
            "type": "Microsoft.Network/virtualNetworks",
            "apiVersion": "2020-11-01",
            "name": "[parameters('virtualNetworks_CoreServicesVnet_name')]",
            "location": "westus",
            "properties": {
                "addressSpace": {
azuredeploy.json#L21
            "type": "Microsoft.Network/virtualNetworks",
            "apiVersion": "2020-11-01",
            "name": "[parameters('virtualNetworks_CoreServicesVnet_name')]",
            "location": "westus",
            "properties": {
                "addressSpace": {
network-subnets.json
{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "moodleCommon": {
network-subnets.json
{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "moodleCommon": {
vNet_Peering_Fed.json
{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "netHubRG": {
HubSpokev4.json#L83
      "type": "Microsoft.Network/virtualNetworks",
      "name": "[variables('hubVnetName')]",
      "apiVersion": "2019-11-01",
      "location": "[parameters('location')]",
      "dependsOn": [
        "[resourceId('Microsoft.Network/networkSecurityGroups', variables('mgmtSubnetNSG'))]"
networks.json#L29
            "type": "Microsoft.Network/virtualNetworks",
            "apiVersion": "2018-04-01",
            "comments": "Deploy hub network",
            "location": "westeurope",
            "properties": {
                "addressSpace": {
template.json#L13
            "type": "Microsoft.Network/virtualNetworks",
            "apiVersion": "2019-11-01",
            "name": "[parameters('virtualNetworks_phc_p_vnet_001_name')]",
            "location": "westeurope",
            "properties": {
                "addressSpace": {
az.02B.deploy.virtual-network.subnet.json
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "vnetName": {
delete.json#L29
            "type": "Microsoft.Network/virtualNetworks",
            "apiVersion": "2018-04-01",
            "comments": "Deploy hub network",
            "location": "westeurope",
            "properties": {
                "addressSpace": {

Parameters

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

    Resource location.

  • tags optional - string

    Resource tags.

  • extendedLocation optional
      • name required - string

        The name of the extended location.

      • type required - string

        The type of the extended location.

  • properties required
      • addressSpace required
          • addressPrefixes required - array

            A list of address blocks reserved for this virtual network in CIDR notation.

      • dhcpOptions optional
          • dnsServers required - array

            The list of DNS servers IP addresses.

      • subnets optional array
          • properties optional
              • addressPrefix required - string

                The address prefix for the subnet.

              • addressPrefixes optional - array

                List of address prefixes for the subnet.

              • networkSecurityGroup optional
                  • id required - string

                    Resource ID.

              • routeTable optional
                  • id required - string

                    Resource ID.

              • natGateway optional
                  • id required - string

                    Resource ID.

              • serviceEndpoints optional array
                  • service optional - string

                    The type of the endpoint service.

                  • locations optional - array

                    A list of locations.

              • serviceEndpointPolicies optional array
                  • id required - string

                    Resource ID.

              • ipAllocations optional array
                  • id required - string

                    Resource ID.

              • delegations optional array
                  • properties optional
                      • serviceName optional - string

                        The name of the service to whom the subnet should be delegated (e.g. Microsoft.Sql/servers).

                  • name required - string

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

              • privateEndpointNetworkPolicies optional - string

                Enable or Disable apply network policies on private end point in the subnet.

              • privateLinkServiceNetworkPolicies optional - string

                Enable or Disable apply network policies on private link service in the subnet.

          • name required - string

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

      • virtualNetworkPeerings optional array
          • properties optional
              • allowVirtualNetworkAccess optional - boolean

                Whether the VMs in the local virtual network space would be able to access the VMs in remote virtual network space.

              • allowForwardedTraffic optional - boolean

                Whether the forwarded traffic from the VMs in the local virtual network will be allowed/disallowed in remote virtual network.

              • allowGatewayTransit optional - boolean

                If gateway links can be used in remote virtual networking to link to this virtual network.

              • useRemoteGateways optional - boolean

                If remote gateways can be used on this virtual network. If the flag is set to true, and allowGatewayTransit on remote peering is also true, virtual network will use gateways of remote virtual network for transit. Only one peering can have this flag set to true. This flag cannot be set if virtual network already has a gateway.

              • remoteVirtualNetwork required
                  • id required - string

                    Resource ID.

              • remoteAddressSpace optional
                  • addressPrefixes required - array

                    A list of address blocks reserved for this virtual network in CIDR notation.

              • remoteBgpCommunities optional
                  • virtualNetworkCommunity required - string

                    The BGP community associated with the virtual network.

              • peeringState optional - string

                The status of the virtual network peering.

          • name required - string

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

      • enableDdosProtection optional - boolean

        Indicates if DDoS protection is enabled for all the protected resources in the virtual network. It requires a DDoS protection plan associated with the resource.

      • enableVmProtection optional - boolean

        Indicates if VM protection is enabled for all the subnets in the virtual network.

      • ddosProtectionPlan optional
          • id required - string

            Resource ID.

      • bgpCommunities optional
          • virtualNetworkCommunity required - string

            The BGP community associated with the virtual network.

      • ipAllocations optional array
          • id required - string

            Resource ID.

Frequently asked questions

What is Azure Network Virtual Network?

Azure Network Virtual Network 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 Virtual Network?

For Terraform, the larryclaman/vpnscaffolds, mibrahimmalik/terraform_cloud and scottohalloran/Certifications source code examples are useful. See the Terraform Example section for further details.

For Azure Resource Manager, the MicrosoftLearning/AZ-700-Designing-and-Implementing-Microsoft-Azure-Networking-Solutions.ja-jp, MicrosoftLearning/AZ-700-Designing-and-Implementing-Microsoft-Azure-Networking-Solutions.ja-jp and oyakim/baskentmoodle source code examples are useful. See the Azure Resource Manager Example section for further details.