Azure Network Gateway

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

azurerm_vpn_gateway (Terraform)

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

Example Usage from GitHub

vwan.tf#L16
  resource "azurerm_vpn_gateway" "microhack-we-hub-vng" {
    name                = "microhack-we-hub-vng"
    location            = var.location-vwan-we-hub
    resource_group_name = azurerm_resource_group.vwan-microhack-hub-rg.name
    virtual_hub_id      = azurerm_virtual_hub.microhack-we-hub.id
site_to_site_gateway.tf#L2
resource "azurerm_vpn_gateway" "s2s_gateway" {
  depends_on = [azurerm_virtual_hub.vwan_hub]
  count      = var.virtual_hub_config.deploy_s2s ? 1 : 0

  name                = azurecaf_naming_convention.s2s_gateway.0.result
  location            = var.location
main.tf#L7
resource "azurerm_vpn_gateway" "this" {
  location            = var.location
  name                = var.name
  resource_group_name = var.resource_group_name
  scale_unit          = var.scale_unit
  tags                = var.tags
main.tf#L20
resource "azurerm_vpn_gateway" "test" {
  name                = "cwan-vpngateway"
  location            = azurerm_resource_group.test.location
  resource_group_name = azurerm_resource_group.test.name
  virtual_hub_id      = azurerm_virtual_hub.test.id
}
site_to_site_gateway.tf#L2
resource "azurerm_vpn_gateway" "s2s_gateway" {
  depends_on = [azurerm_virtual_hub.vwan_hub]
  count      = var.virtual_hub_config.deploy_s2s ? 1 : 0

  name                = azurecaf_naming_convention.s2s_gateway.0.result
  location            = var.location
vwan.tf#L29
resource "azurerm_vpn_gateway" "VPNGW-HUB-WestEurope" {
  location            = "WestEurope"
  name                = "VPNGW-HUB-WestEurope"
  resource_group_name = var.lab-rg
  virtual_hub_id      = azurerm_virtual_hub.HUB-WestEurope.id
  tags                = var.tags
site_to_site_gateway.tf#L15
resource "azurerm_vpn_gateway" "s2s_gateway" {
  depends_on = [azurerm_virtual_hub.vwan_hub]
  count      = var.virtual_hub_config.deploy_s2s ? 1 : 0

  name                = azurecaf_name.s2s_gateway.0.result
  location            = var.location
azurerm-vpn-gateway.tf#L9
resource "azurerm_vpn_gateway" "canadacentral" {
  name                = "canadacentral-vpngw"
  location            = "canadacentral"
  resource_group_name = azurerm_resource_group.global.name
  virtual_hub_id      = azurerm_virtual_hub.all["canadacentral"].id
}
main.tf#L7
resource "azurerm_vpn_gateway" "this" {
  location            = var.location
  name                = var.name
  resource_group_name = var.resource_group_name
  scale_unit          = var.scale_unit
  tags                = var.tags
site_to_site_gateway.tf#L15
resource "azurerm_vpn_gateway" "s2s_gateway" {
  depends_on = [azurerm_virtual_hub.vwan_hub]
  count      = try(var.virtual_hub_config.deploy_s2s, false) ? 1 : 0

  name                = azurecaf_name.s2s_gateway.0.result
  location            = var.location

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 VPN Gateway within a Virtual Hub, which enables Site-to-Site communication.

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

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

Example Usage from GitHub

VpnGatewayList.json#L16
            "type": "Microsoft.Network/vpnGateways",
            "properties": {
              "provisioningState": "Succeeded",
              "virtualHub": {
                "id": "/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.Network/virtualHubs/virtualHub1"
              },
VpnGatewayListByResourceGroup.json#L16
            "type": "Microsoft.Network/vpnGateways",
            "properties": {
              "provisioningState": "Succeeded",
              "virtualHub": {
                "id": "/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.Network/virtualHubs/virtualHub1"
              },
VpnGatewayListByResourceGroup.json#L16
            "type": "Microsoft.Network/vpnGateways",
            "properties": {
              "provisioningState": "Succeeded",
              "virtualHub": {
                "id": "/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.Network/virtualHubs/virtualHub1"
              },
VpnGatewayList.json#L16
            "type": "Microsoft.Network/vpnGateways",
            "properties": {
              "provisioningState": "Succeeded",
              "virtualHub": {
                "id": "/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.Network/virtualHubs/virtualHub1"
              },
VpnGatewayList.json#L16
            "type": "Microsoft.Network/vpnGateways",
            "properties": {
              "provisioningState": "Succeeded",
              "virtualHub": {
                "id": "/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.Network/virtualHubs/virtualHub1"
              },
VpnGatewayListByResourceGroup.json#L16
            "type": "Microsoft.Network/vpnGateways",
            "properties": {
              "provisioningState": "Succeeded",
              "virtualHub": {
                "id": "/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.Network/virtualHubs/virtualHub1"
              },
VpnGatewayListByResourceGroup.json#L16
            "type": "Microsoft.Network/vpnGateways",
            "properties": {
              "provisioningState": "Succeeded",
              "virtualHub": {
                "id": "/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.Network/virtualHubs/virtualHub1"
              },
VpnGatewayList.json#L16
            "type": "Microsoft.Network/vpnGateways",
            "properties": {
              "provisioningState": "Succeeded",
              "virtualHub": {
                "id": "/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.Network/virtualHubs/virtualHub1"
              },
VpnGatewayList.json#L16
            "type": "Microsoft.Network/vpnGateways",
            "properties": {
              "provisioningState": "Succeeded",
              "virtualHub": {
                "id": "/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.Network/virtualHubs/virtualHub1"
              },
VpnGatewayListByResourceGroup.json#L16
            "type": "Microsoft.Network/vpnGateways",
            "properties": {
              "provisioningState": "Succeeded",
              "virtualHub": {
                "id": "/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.Network/virtualHubs/virtualHub1"
              },

Parameters

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

    Resource location.

  • tags optional - string

    Resource tags.

  • properties required
      • virtualHub optional
          • id required - string

            Resource ID.

      • connections optional array
          • properties optional
              • remoteVpnSite optional
                  • id required - string

                    Resource ID.

              • routingWeight optional - integer

                Routing weight for vpn connection.

              • dpdTimeoutSeconds optional - integer

                DPD timeout in seconds for vpn connection.

              • connectionStatus optional - string

                The connection status.

              • vpnConnectionProtocolType optional - string

                Connection protocol used for this connection.

              • connectionBandwidth optional - integer

                Expected bandwidth in MBPS.

              • sharedKey optional - string

                SharedKey for the vpn connection.

              • enableBgp optional - boolean

                EnableBgp flag.

              • usePolicyBasedTrafficSelectors optional - boolean

                Enable policy-based traffic selectors.

              • ipsecPolicies optional array
                  • saLifeTimeSeconds required - integer

                    The IPSec Security Association (also called Quick Mode or Phase 2 SA) lifetime in seconds for a site to site VPN tunnel.

                  • saDataSizeKilobytes required - integer

                    The IPSec Security Association (also called Quick Mode or Phase 2 SA) payload size in KB for a site to site VPN tunnel.

                  • ipsecEncryption required - string

                    The IPSec encryption algorithm (IKE phase 1).

                  • ipsecIntegrity required - string

                    The IPSec integrity algorithm (IKE phase 1).

                  • ikeEncryption required - string

                    The IKE encryption algorithm (IKE phase 2).

                  • ikeIntegrity required - string

                    The IKE integrity algorithm (IKE phase 2).

                  • dhGroup required - string

                    The DH Group used in IKE Phase 1 for initial SA.

                  • pfsGroup required - string

                    The Pfs Group used in IKE Phase 2 for new child SA.

              • trafficSelectorPolicies optional array
                  • localAddressRanges required - array

                    A collection of local address spaces in CIDR format.

                  • remoteAddressRanges required - array

                    A collection of remote address spaces in CIDR format.

              • enableRateLimiting optional - boolean

                EnableBgp flag.

              • enableInternetSecurity optional - boolean

                Enable internet security.

              • useLocalAzureIpAddress optional - boolean

                Use local azure ip to initiate connection.

              • vpnLinkConnections optional array
                  • properties optional
                      • vpnSiteLink optional
                          • id required - string

                            Resource ID.

                      • routingWeight optional - integer

                        Routing weight for vpn connection.

                      • vpnLinkConnectionMode optional - string

                        Vpn link connection mode.

                      • connectionStatus optional - string

                        The connection status.

                      • vpnConnectionProtocolType optional - string

                        Connection protocol used for this connection.

                      • connectionBandwidth optional - integer

                        Expected bandwidth in MBPS.

                      • sharedKey optional - string

                        SharedKey for the vpn connection.

                      • enableBgp optional - boolean

                        EnableBgp flag.

                      • usePolicyBasedTrafficSelectors optional - boolean

                        Enable policy-based traffic selectors.

                      • ipsecPolicies optional array
                          • saLifeTimeSeconds required - integer

                            The IPSec Security Association (also called Quick Mode or Phase 2 SA) lifetime in seconds for a site to site VPN tunnel.

                          • saDataSizeKilobytes required - integer

                            The IPSec Security Association (also called Quick Mode or Phase 2 SA) payload size in KB for a site to site VPN tunnel.

                          • ipsecEncryption required - string

                            The IPSec encryption algorithm (IKE phase 1).

                          • ipsecIntegrity required - string

                            The IPSec integrity algorithm (IKE phase 1).

                          • ikeEncryption required - string

                            The IKE encryption algorithm (IKE phase 2).

                          • ikeIntegrity required - string

                            The IKE integrity algorithm (IKE phase 2).

                          • dhGroup required - string

                            The DH Group used in IKE Phase 1 for initial SA.

                          • pfsGroup required - string

                            The Pfs Group used in IKE Phase 2 for new child SA.

                      • enableRateLimiting optional - boolean

                        EnableBgp flag.

                      • useLocalAzureIpAddress optional - boolean

                        Use local azure ip to initiate connection.

                      • ingressNatRules optional array
                          • id required - string

                            Resource ID.

                      • egressNatRules optional array
                          • id required - string

                            Resource ID.

                  • name optional - string

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

              • routingConfiguration optional
                  • associatedRouteTable optional
                      • id required - string

                        Resource ID.

                  • propagatedRouteTables optional
                      • labels optional - array

                        The list of labels.

                      • ids optional array
                          • id required - string

                            Resource ID.

                  • vnetRoutes optional
                      • staticRoutes optional array
                          • name optional - string

                            The name of the StaticRoute that is unique within a VnetRoute.

                          • addressPrefixes optional - array

                            List of all address prefixes.

                          • nextHopIpAddress optional - string

                            The ip address of the next hop.

          • name optional - string

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

      • bgpSettings optional
          • asn optional - integer

            The BGP speaker's ASN.

          • bgpPeeringAddress optional - string

            The BGP peering address and BGP identifier of this BGP speaker.

          • peerWeight optional - integer

            The weight added to routes learned from this BGP speaker.

          • bgpPeeringAddresses optional array
              • ipconfigurationId optional - string

                The ID of IP configuration which belongs to gateway.

              • customBgpIpAddresses optional - array

                The list of custom BGP peering addresses which belong to IP configuration.

      • vpnGatewayScaleUnit optional - integer

        The scale unit for this vpn gateway.

      • isRoutingPreferenceInternet optional - boolean

        Enable Routing Preference property for the Public IP Interface of the VpnGateway.

      • natRules optional array
          • properties optional
              • type optional - string

                The type of NAT rule for VPN NAT.

              • mode optional - string

                The Source NAT direction of a VPN NAT.

              • internalMappings optional array
                  • addressSpace optional - string

                    Address space for Vpn NatRule mapping.

              • externalMappings optional array
                  • addressSpace optional - string

                    Address space for Vpn NatRule mapping.

              • ipConfigurationId optional - string

                The IP Configuration ID this NAT rule applies to.

          • 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 Gateway?

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

For Terraform, the mddazure/azure-vwan-microhack, tushar7653/testcaf and kevinhead/azurerm source code examples are useful. See the Terraform Example section for further details.

For Azure Resource Manager, the debhol/azuredocs, debhol/azuredocs and debhol/azuredocs source code examples are useful. See the Azure Resource Manager Example section for further details.