Azure SQL : Service Endpoint vs Private Endpoint Part 2
Tags: azure, security, database
Published: 2021-10-12 07:44:00

In [the previous article](https://dev.to/kenakamu/azure-sql-service-endpoint-vs-private-endpoint-part-1-23oh), I explain about Service Endpoint. In this article, I will use same setup and explain about Private Endpoint.

# Setup

- Azure SQL
- V-NET and subnets
- VM with SSMS (SQL Server Management Service)

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d8cm5i619jszv8ghoo5j.png)

VM has following public/private IP

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zafkyczfrln91zy0ur7r.png)

# Use Private Endpoint

Private endpoint assigns Private IP address to the Azure resource. By having private IP address in particular subnet, any other component can access to the service by using the private IP address rather than Microsoft assigned public IP address.

Unlike Service Endpoint, **Private Endpoint** is Azure resource we need to provision. Let's do it.

## Create Private Endpoint for the Azure SQL.

1\. From Add Resources menu, create private endpoint.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xb1tqu63ydjkclf877k7.png)
 
2\. Give it a name and region where VNET is located.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ag6cu17tsl5z47vplfv9.png)

3\. In the Resource tab, select the Azure SQL.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zq8cdtxv17nabz917svk.png)

4\. Select a VNET and a subnet where you want to locate the private IP of the Azure SQL. I selected subnet2 where no other resource exists. If you don't have private DNS, then create DNS zone at the same time. Private DNS is mandatory to resolve its name to the assigned private IP address.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/555w20qhu79flj9ch4fn.png)

5\. Finally, create the resource.

## Verify the result from VNET

Once the private endpoint is created, we can see it in the VNET resource where we assigned it to. As we can see, it is added to VNET1/subnet2

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gqakmkzztwov0fqq7f41.png)
 
We can also see the address from "Connected devices" menu. The IP address is "10.0.2.4".

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/42x8ds4k2lvsj33zx1io.png)
 
## Verify name resolution

As private DNS zone is also added, we can check name resolution from the VM by using ping. The name is correctly resolved to the private IP address.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vm5oqrpouk5sfslg54mv.png)
 
# Use Private Endpoint to Access to Azure SQL

As we use Private Endpoint, we can change the firewall rule for the Azure SQL.

## Update firewall rule

1\. Go back to Azure SQL resource and "Firewalls and virtual networks" menu. Remove the subnet we added in the previous article.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7m1um65k2s7clvuboqh5.png)

2\. We can also enable "Deny public network access" when we provision Private Endpoint as the Azure SQL now has private IP.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3od60fkttumty1qs745k.png)
 
3\. Go back to VM and re-connect to SQL server. We can successfully connect it without the Azure SQL rule as we connect via Private Endpoint now. Once thing to note here is that VM locates in subnet1, whereas Azure SQL private endpoint locates in subnet2. So cross subnet access is also possible now.

## Use NSG (Network Security Group) to control access

NSG for private endpoint is in public preview now. See [here](https://docs.microsoft.com/en-us/azure/private-link/disable-private-endpoint-network-policy) for more detail.

# Summary

By using Private Endpoint, we can assign private IP address and consider it as local resource as any other resources. This means we can access to the resource from other vnet/subnet even from On-Prem vnet. However, we need private DNS zone to resolve it's name and need to consider security carefully.