Securing Azure Applications with Terraform: Subnets, Private Endpoints & NSGs

  • Thread Author
Securing Your Azure Application with Terraform: A Deep Dive into Subnets, Endpoints, DNS, and NSGs
Azure’s cloud-first mindset demands that applications not only scale but also remain secure. With the proliferation of microservices and distributed architectures, proper network segmentation becomes a cornerstone of any robust security model. Today, we explore how to configure network security in Azure using Terraform, focusing on subnets, private endpoints, DNS, and network security groups (NSGs).
────────────────────────────
Introduction
Imagine launching your shiny new application on Azure only to realize that its doors are wide open for the entire internet. That’s where network security steps in. By isolating services in dedicated subnets, enforcing delegated networking policies, and establishing private links between services, you can protect your applications from unnecessary exposure while ensuring seamless communication between trusted components.
In this piece, we break down an architecture built using Terraform. It demonstrates how to map out a secure environment by constructing the ideal Azure Virtual Network (VNet) and segregating services into multiple subnets. We’ll also cover the complexities of subnet delegation and how private endpoints demand a dedicated subnet—ensuring your application’s internal communications remain safe.
────────────────────────────
Network Segregation: The Foundation of Azure Security
Following Microsoft’s recommendations, a secure Azure blueprint starts with segregation. The idea is simple: isolate each service into its own environment. In our illustrative example, two applications (in this case Azure Functions) are deployed within the same VNet but placed into separate subnets. This design not only limits potential blast radius in the event of an attack but also simplifies communication channels by only exposing what is necessary.
For instance, Terraform code snippets create a storage account, a service plan, and the function apps themselves. Each function app ties to a specific subnet, as shown below:
 • A storage account ("dnsexamplesa") for app data
 • A service plan ("dns-asp") for hosting the Windows-based functions
 • Two function apps ("dns-app1" and "dns-app2") each linked to a different subnet
This segregation paves the way for controlled network boundaries, offering a first line of defense by ensuring that only designated services can communicate with one another.
────────────────────────────
Subnet Delegation: How It Strengthens Networking Rules
Subnet delegation is a critical Azure feature that lets you apply service-specific networking configurations automatically. In our Terraform deployment, attempting to deploy the function apps in the VNet without delegation triggers an error. Azure requires that subnets, designed to host specific services (in this case, Azure App Service related resources), be explicitly delegated.
The Terraform code reveals the solution:
 • Each subnet (e.g., subnet1) is updated with a delegation block
 • The ‘Microsoft.Web/serverFarms’ delegation specifies actions such as joining the subnet and preparing network policies
By delegating the subnet, Azure:
 – Automatically manages IP allocation
 – Enforces dedicated routing rules
 – Prevents conflicts and applies service-specific security policies
This delegation reduces manual overhead and ensures that your configuration complies with Azure’s standards—a surefire way to maintain order in a sprawling cloud environment.
────────────────────────────
Private Endpoints & Private Link: Securing Inbound Connectivity
Once your services reside securely in their delegated subnets, the question arises: How do they connect privately? The answer lies in the use of private endpoints and private links.
A private endpoint is essentially a private IP address allocated from your virtual network. It acts as a network interface (NIC) through which a service (for instance, Azure Functions or SQL Server) is accessed. When deploying private endpoints in a subnet that has been delegated, however, you encounter a stumbling block: Azure enforces a rigid separation.
Azure prevents the deployment of a private endpoint in a subnet that is already delegated to a specific service type (Microsoft.Web/serverFarms, in our case). Doing so results in an error message like:
 “PrivateEndpointCreationNotAllowedAsSubnetIsDelegated”
The resolution is straightforward but crucial: allocate a dedicated subnet just for private endpoints. For example, adding a new subnet (subnet3) solely for private endpoints ensures that the communication pathway to your services stays both secure and compliant with Azure’s restrictions.
By creating a private endpoint in this dedicated subnet, you effectively forge a private link between your internal services. The private link works behind the scenes, routing traffic over Azure’s secure backbone rather than the public internet. This arrangement is especially beneficial for sensitive applications that require both isolation and seamless dependency intercommunication.
────────────────────────────
DNS, A Records, and NSGs: Fine-Tuning Your Network Security
While subnets and endpoints establish the physical and logical separations, DNS configurations and NSGs add a layer of granularity to control who can access what on your network.
Modern network security relies on:
 • Private DNS zones to resolve names without exposing them externally
 • A records ensuring that service names map correctly to private IPs
 • Network Security Groups (NSGs) to regulate traffic flow, specifying allowed protocols, ports, and source/destination constraints
Although our Terraform guide focuses on subnets and endpoints, it hints at additional considerations such as NSGs. Implementing NSGs allows you to filter and monitor network traffic per subnet or even individual virtual network interfaces, offering another safeguard against potential vulnerabilities.
For Windows administrators transitioning to a cloud-first strategy, leveraging NSGs is an indispensable step. They add granular control, similar to a robust Windows Firewall in traditional environments, but at the network layer.
────────────────────────────
Troubleshooting & Best Practices
Designing a secure application network architecture is as much about the planning as it is about handling the occasional hiccup. Consider this common scenario: attempting to attach a private endpoint to a delegated subnet results in a 400 Bad Request error. When you see messages like:
 “Private endpoint cannot be created as subnet is delegated,”
it's a signal to reassess your subnet configurations. Here’s a quick troubleshooting checklist:
  1. Confirm that the subnet containing your application resources is correctly delegated (e.g., to Microsoft.Web/serverFarms).
  2. Allocate a separate subnet for private endpoints.
  3. Ensure that NSG rules are appropriately configured to allow internal traffic between subnets.
  4. Validate your private DNS settings (A records and zones) so that network resolution occurs seamlessly.
By following these steps, not only do you align with best practices, but you also set up your network for both operational excellence and long-term security.
────────────────────────────
Conclusion
As organizations increasingly pivot to cloud-native environments, securing application networks with precision is critical. By harnessing Terraform to automate resource provisioning in Azure, you gain a dual advantage: streamlined deployments and robust security. Key takeaways include:
• Segregate applications across dedicated subnets to minimize risk.
• Use subnet delegation to ensure service-specific rules and policies are automatically enforced.
• Separate private endpoints into their own subnet to comply with Azure’s restrictions and establish private links.
• Enhance security further with private DNS configurations and granular NSG rules.
For Windows administrators and IT engineers, these techniques highlight how cloud security mirrors traditional methodologies while embracing innovation. Integrating these practices into your Azure strategy is an essential step towards a more secure, efficient, and resilient network infrastructure.
At WindowsForum.com, you’ll find further discussions on best practices in Azure resource management, advanced NSG configurations, and Terraform strategies for cloud deployments. Remember, the key to robust security is not just in configuring the tools but also in constantly evolving with the cloud landscape.

Source: Medium
 

Back
Top