Azure SQL : Service Endpoint vs Private Endpoint Part 1
Tags: azure, security, database
Published: 2021-10-12 06:59:47

Azure Service Endpoint and Private Endpoint are little bit confusing, so I will explain them step by step by using SQL Azure and VM as client.

In this article, I will explain how Service Endpoint works.

# 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)
  
# How VM access Azure SQL

When I access Azure SQL from VM now, I see VM tries to use its **Public IP address** to access Azure SQL and it's blocked by SQL Server firewall.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lgntolx9mv9j93mh1hin.png)
 
I can configure SQL Server firewall to allow the specific IP.

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

# Use Service Endpoint

When we use service endpoint, VM uses its private IP rather than public IP to access Azure resources. 

## Add Service Endpoint

There are several ways to create service endpoint, and I explain one of them.

1\. Go to VNET resource and select "Service endpoints". Click "Add".

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

2\. Select service and subnet. By doing this, the connection from this particular subnet to the service will use private IP.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u0kvxgzqsmxdwtslr87i.png)
 
3\. Once configuration is done, try to connect to SQL again. The error message has been changed.

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

## Add Subnet to firewall rule

 1\. Go to Azure SQL Server and select "Firewalls and virtual networks". Click "Add existing virtual network".

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8veotjksuieo3mw9i0b1.png)
 
2\. Add the subnet which we configured for service endpoint. If we don't have service endpoint enabled for the subnet, the blade will add it for us. Click "OK".

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/puxkpezxgmx3a84i2vgb.png)
 
3\. Try connect to SQL again. Now we can connect to the Azure SQL. Run following query shows it uses private IP.

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

## Move VM to another subnet

When I move the VM to subnet2, then I cannot access to Azure SQL anymore as expected. To enable this access, we need to 

- Create Service Endpoint
- Add firewall rule

If we have too many subnets to access to the SQL Azure, it maybe a bit troublesome to manage them in this way.
 
# Summary 

It's obvious that SQL Server needs to know client's private IP address to understand which subnet the client belongs to. That's why we need both Service Endpoint entry and firewall rule. 

Another important thing to note is that, even though VM uses its **private IP address** to access to the Azure SQL, the VM still uses **Public IP address** of the Azure SQL to connecting to.

I will explain Private Endpoint in the [next article](https://dev.to/kenakamu/azure-sql-service-endpoint-vs-private-endpoint-part-2-kcl).