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
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"
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"]
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"]
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"]
}
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"]
}
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"]
}
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 ]
}
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
}
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
}
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"]
}
Parameters
-
address_space
required - list of string -
bgp_community
optional - string -
dns_servers
optional - list of string -
guid
optional computed - string -
id
optional computed - string -
location
required - string -
name
required - string -
resource_group_name
required - string -
subnet
optional computed - set of object-
address_prefix
- string -
id
- string -
name
- string -
security_group
- string
-
-
tags
optional - map from string to string -
vm_protection_enabled
optional - bool -
ddos_protection_plan
list block -
timeouts
single block
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.
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.
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/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
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "2020-11-01",
"name": "[parameters('virtualNetworks_CoreServicesVnet_name')]",
"location": "westus",
"properties": {
"addressSpace": {
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "2020-11-01",
"name": "[parameters('virtualNetworks_CoreServicesVnet_name')]",
"location": "westus",
"properties": {
"addressSpace": {
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"moodleCommon": {
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"moodleCommon": {
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"netHubRG": {
"type": "Microsoft.Network/virtualNetworks",
"name": "[variables('hubVnetName')]",
"apiVersion": "2019-11-01",
"location": "[parameters('location')]",
"dependsOn": [
"[resourceId('Microsoft.Network/networkSecurityGroups', variables('mgmtSubnetNSG'))]"
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "2018-04-01",
"comments": "Deploy hub network",
"location": "westeurope",
"properties": {
"addressSpace": {
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "2019-11-01",
"name": "[parameters('virtualNetworks_phc_p_vnet_001_name')]",
"location": "westeurope",
"properties": {
"addressSpace": {
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"vnetName": {
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "2018-04-01",
"comments": "Deploy hub network",
"location": "westeurope",
"properties": {
"addressSpace": {
Parameters
name
required - stringtype
required - stringapiVersion
required - stringlocation
required - stringResource location.
tags
optional - stringResource tags.
extendedLocation
optionalname
required - stringThe name of the extended location.
type
required - stringThe type of the extended location.
properties
requiredaddressSpace
requiredaddressPrefixes
required - arrayA list of address blocks reserved for this virtual network in CIDR notation.
dhcpOptions
optionaldnsServers
required - arrayThe list of DNS servers IP addresses.
subnets
optional arrayproperties
optionaladdressPrefix
required - stringThe address prefix for the subnet.
addressPrefixes
optional - arrayList of address prefixes for the subnet.
networkSecurityGroup
optionalid
required - stringResource ID.
routeTable
optionalid
required - stringResource ID.
natGateway
optionalid
required - stringResource ID.
serviceEndpoints
optional arrayservice
optional - stringThe type of the endpoint service.
locations
optional - arrayA list of locations.
serviceEndpointPolicies
optional arrayid
required - stringResource ID.
ipAllocations
optional arrayid
required - stringResource ID.
delegations
optional arrayproperties
optionalserviceName
optional - stringThe name of the service to whom the subnet should be delegated (e.g. Microsoft.Sql/servers).
name
required - stringThe name of the resource that is unique within a subnet. This name can be used to access the resource.
privateEndpointNetworkPolicies
optional - stringEnable or Disable apply network policies on private end point in the subnet.
privateLinkServiceNetworkPolicies
optional - stringEnable or Disable apply network policies on private link service in the subnet.
name
required - stringThe name of the resource that is unique within a resource group. This name can be used to access the resource.
virtualNetworkPeerings
optional arrayproperties
optionalallowVirtualNetworkAccess
optional - booleanWhether the VMs in the local virtual network space would be able to access the VMs in remote virtual network space.
allowForwardedTraffic
optional - booleanWhether the forwarded traffic from the VMs in the local virtual network will be allowed/disallowed in remote virtual network.
allowGatewayTransit
optional - booleanIf gateway links can be used in remote virtual networking to link to this virtual network.
useRemoteGateways
optional - booleanIf 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
requiredid
required - stringResource ID.
remoteAddressSpace
optionaladdressPrefixes
required - arrayA list of address blocks reserved for this virtual network in CIDR notation.
remoteBgpCommunities
optionalvirtualNetworkCommunity
required - stringThe BGP community associated with the virtual network.
peeringState
optional - stringThe status of the virtual network peering.
name
required - stringThe name of the resource that is unique within a resource group. This name can be used to access the resource.
enableDdosProtection
optional - booleanIndicates 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 - booleanIndicates if VM protection is enabled for all the subnets in the virtual network.
ddosProtectionPlan
optionalid
required - stringResource ID.
bgpCommunities
optionalvirtualNetworkCommunity
required - stringThe BGP community associated with the virtual network.
ipAllocations
optional arrayid
required - stringResource 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.