Service endpoint policies enable you to filter virtual network traffic to specific Azure resources, over service endpoints. If you're not familiar with service endpoint policies, see service endpoint policies overview to learn more.
In this tutorial, you learn how to:
Create a virtual network.
Add a subnet and enable service endpoint for Azure Storage.
Create two Azure Storage accounts and allow network access to it from the subnet in the virtual network.
Create a service endpoint policy to allow access only to one of the storage accounts.
Deploy a virtual machine (VM) to the subnet.
Confirm access to the allowed storage account from the subnet.
Confirm access is denied to the nonallowed storage account from the subnet.
Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. You can use either Bash or PowerShell with Cloud Shell to work with Azure services. You can use the Cloud Shell preinstalled commands to run the code in this article, without having to install anything on your local environment.
To start Azure Cloud Shell:
Option
Example/Link
Select Try It in the upper-right corner of a code or command block. Selecting Try It doesn't automatically copy the code or command to Cloud Shell.
Go to https://shell.azure.com, or select the Launch Cloud Shell button to open Cloud Shell in your browser.
Select the Cloud Shell button on the menu bar at the upper right in the Azure portal.
To use Azure Cloud Shell:
Start Cloud Shell.
Select the Copy button on a code block (or command block) to copy the code or command.
Paste the code or command into the Cloud Shell session by selecting Ctrl+Shift+V on Windows and Linux, or by selecting Cmd+Shift+V on macOS.
Select Enter to run the code or command.
If you choose to install and use PowerShell locally, this article requires the Azure PowerShell module version 1.0.0 or later. Run Get-Module -ListAvailable Az to find the installed version. If you need to upgrade, see Install Azure PowerShell module. If you're running PowerShell locally, you also need to run Connect-AzAccount to create a connection with Azure.
If you prefer to run CLI reference commands locally, install the Azure CLI. If you're running on Windows or macOS, consider running Azure CLI in a Docker container. For more information, see How to run the Azure CLI in a Docker container.
If you're using a local installation, sign in to the Azure CLI by using the az login command. To finish the authentication process, follow the steps displayed in your terminal. For other sign-in options, see Sign in with the Azure CLI.
When you're prompted, install the Azure CLI extension on first use. For more information about extensions, see Use extensions with the Azure CLI.
Run az version to find the version and dependent libraries that are installed. To upgrade to the latest version, run az upgrade.
This article requires version 2.0.28 or later of the Azure CLI. If using Azure Cloud Shell, the latest version is already installed.
Create a virtual network and enable service endpoint
Create a virtual network to contain the resources you create in this tutorial.
In the search box in the portal, enter Virtual networks. Select Virtual networks in the search results.
Select + Create to create a new virtual network.
Enter or select the following information in the Basics tab of Create virtual network.
Setting
Value
Project details
Subscription
Select your subscription.
Resource group
Select Create new. Enter test-rg in Name. Select OK.
Name
Enter vnet-1.
Region
Select West US 2.
Select Next.
Select Next.
In the IP addresses tab, in Subnets, select the default subnet.
Enter or select the following information in Edit subnet.
Setting
Value
Name
Enter subnet-1.
Service Endpoints
Services
In the pull-down menu, select Microsoft.Storage.
Select Save.
Select Review + Create.
Select Create.
Before creating a virtual network, you have to create a resource group for the virtual network, and all other resources created in this article. Create a resource group with New-AzResourceGroup. The following example creates a resource group named test-rg:
Create a subnet configuration with New-AzVirtualNetworkSubnetConfig, and then write the subnet configuration to the virtual network with Set-AzVirtualNetwork. The following example adds a subnet named subnet-1 to the virtual network and creates the service endpoint for Microsoft.Storage.
Before creating a virtual network, you have to create a resource group for the virtual network, and all other resources created in this article. Create a resource group with az group create. The following example creates a resource group named test-rg in the westus2 location.
az group create \
--name test-rg \
--location westus2
In the search box in the portal, enter Network security groups. Select Network security groups in the search results.
Select + Create to create a new network security group.
In the Basics tab of Create network security group, enter, or select the following information.
Setting
Value
Project details
Subscription
Select your subscription.
Resource group
Select test-rg.
Name
Enter nsg-1.
Region
Select West US 2.
Select Review + Create.
Select Create.
Create network security group rules
In the search box in the portal, enter Network security groups. Select Network security groups in the search results.
Select nsg-1.
Expand Settings. Select Outbound security rules.
Select + Add to add a new outbound security rule.
In Add outbound security rule, enter or select the following information.
Setting
Value
Source
Select Service Tag.
Source service tag
Select VirtualNetwork.
Source port ranges
Enter *.
Destination
Select Service Tag.
Destination service tag
Select Storage.
Service
Select Custom.
Destination port ranges
Enter *.
Protocol
Select Any.
Action
Select Allow.
Priority
Enter 100.
Name
Enter allow-storage-all.
Select Add.
Select + Add to add another outbound security rule.
In Add outbound security rule, enter or select the following information.
Setting
Value
Source
Select Service Tag.
Source service tag
Select VirtualNetwork.
Source port ranges
Enter *.
Destination
Select Service Tag.
Destination service tag
Select Internet.
Service
Select Custom.
Destination port ranges
Enter *.
Protocol
Select Any.
Action
Select Deny.
Priority
Enter 110.
Name
Enter deny-internet-all.
Select Add.
Expand Settings. Select Subnets.
Select Associate.
In Associate subnet, enter or select the following information.
Setting
Value
Virtual network
Select vnet-1 (test-rg).
Subnet
Select subnet-1.
Select OK.
Create network security group security rules with New-AzNetworkSecurityRuleConfig. The following rule allows outbound access to the public IP addresses assigned to the Azure Storage service:
The following rule denies access to all public IP addresses. The previous rule overrides this rule, due to its higher priority, which allows access to the public IP addresses of Azure Storage.
Associate the network security group to the subnet-1 subnet with Set-AzVirtualNetworkSubnetConfig and then write the subnet configuration to the virtual network. The following example associates the nsg-1 network security group to the subnet-1 subnet:
Create a network security group with az network nsg create. The following example creates a network security group named nsg-1.
az network nsg create \
--resource-group test-rg \
--name nsg-1
Associate the network security group to the subnet-1 subnet with az network vnet subnet update. The following example associates the nsg-1 network security group to the subnet-1 subnet:
Create security rules with az network nsg rule create. The rule that follows allows outbound access to the public IP addresses assigned to the Azure Storage service:
Each network security group contains several default security rules. The rule that follows overrides a default security rule that allows outbound access to all public IP addresses. The destination-address-prefix "Internet" option denies outbound access to all public IP addresses. The previous rule overrides this rule, due to its higher priority, which allows access to the public IP addresses of Azure Storage.
The steps necessary to restrict network access to resources created through Azure services enabled for service endpoints varies across services. See the documentation for individual services for specific steps for each service. The remainder of this article includes steps to restrict network access for an Azure Storage account, as an example.
In the search box in the portal, enter Storage accounts. Select Storage accounts in the search results.
Select allowedaccount(random-number).
Expand the Data storage section and select File shares.
Select + File share.
In New file share, enter or select the following information.
Setting
Value
Name
Enter file-share.
Leave the rest of the settings as default and select Review + create.
Select Create.
Repeat the previous steps to create a file share in deniedaccount(random-number).
Create allowed storage account file share
Use Get-AzStorageAccountKey to get the storage account key for the allowed storage account. You'll use this key in the next step to create a file share in the allowed storage account.
Use Get-AzStorageAccountKey to get the storage account key for the allowed storage account. You'll use this key in the next step to create a file share in the denied storage account.
Retrieve the connection string for the storage accounts into a variable with az storage account show-connection-string. The connection string is used to create a file share in a later step.
Retrieve the connection string for the storage accounts into a variable with az storage account show-connection-string. The connection string is used to create a file share in a later step.
By default, storage accounts accept network connections from clients in any network. To restrict network access to the storage accounts, you can configure the storage account to accept connections only from specific networks. In this example, you configure the storage account to accept connections only from the virtual network subnet you created earlier.
In the search box in the portal, enter Storage accounts. Select Storage accounts in the search results.
Select allowedaccount(random-number).
Expand Security + networking and select Networking.
In Firewalls and virtual networks, in Public network access, select Enabled from selected virtual networks and IP addresses.
In Virtual networks, select + Add existing virtual network.
In Add networks, enter or select the following information.
Setting
Value
Subscription
Select your subscription.
Virtual networks
Select vnet-1.
Subnets
Select subnet-1.
Select Add.
Select Save.
Repeat the previous steps to deny network access to deniedaccount(random-number).
Use Update-AzStorageAccountNetworkRuleSet to deny access to the storage accounts except from the virtual network and subnet you created earlier. Once network access is denied, the storage account isn't accessible from any network.
By default, storage accounts accept network connections from clients in any network. To limit access to selected networks, change the default action to Deny with az storage account update. Once network access is denied, the storage account isn't accessible from any network.
Apply policy to allow access to valid storage account
You can create a service endpoint policy. The policy ensures users in the virtual network can only access safe and allowed Azure Storage accounts. This policy contains a list of allowed storage accounts applied to the virtual network subnet that is connected to storage via service endpoints.
Create a service endpoint policy
This section creates the policy definition with the list of allowed resources for access over service endpoint.
Service endpoint policies are applied over service endpoints. Start by creating a service endpoint policy. Then create the policy definitions under this policy for Azure Storage accounts to be approved for this subnet
Ensure that all the resources accessed from the subnet are added to the policy definition before associating the policy to the given subnet. Once the policy is associated, only access to the allow listed resources will be allowed over service endpoints.
Ensure that no managed Azure services exist in the subnet that is being associated to the service endpoint policy.
Access to Azure Storage resources in all regions will be restricted as per Service Endpoint Policy from this subnet.
Validate access restriction to Azure Storage accounts
To test network access to a storage account, deploy a VM in the subnet.
In the search box in the portal, enter Virtual machines. Select Virtual machines in the search results.
In the Basics tab of Create a virtual machine, enter, or select the following information:
Setting
Value
Project details
Subscription
Select your subscription.
Resource group
Select test-rg.
Instance details
Virtual machine name
Enter vm-1.
Region
Select (US) West US 2.
Availability options
Select No infrastructure redundancy required.
Security type
Select Standard.
Image
Select Windows Server 2022 Datacenter - x64 Gen2.
Size
Select a size.
Administrator account
Username
Enter a username.
Password
Enter a password.
Confirm password
Enter the password again.
Inbound port rules
Select Next: Disks, then select Next: Networking.
In the Networking tab, enter or select the following information.
Setting
Value
Network interface
Virtual network
Select vnet-1.
Subnet
Select subnet-1 (10.0.0.0/24).
Public IP
Select None.
NIC network security group
Select None.
Leave the rest of the settings as default and select Review + Create.
Select Create.
Create a virtual machine in the subnet-1 subnet with New-AzVM. When running the command that follows, you're prompted for credentials. The values that you enter are configured as the user name and password for the VM.
In the search box in the portal, enter Storage accounts. Select Storage accounts in the search results.
Select allowedaccount(random-number).
Expand Security + networking and select Access keys.
Copy the key1 value. You use this key to map a drive to the storage account from the virtual machine you created earlier.
In the search box in the portal, enter Virtual machines. Select Virtual machines in the search results.
Select vm-1.
Expand Operations. Select Run command.
Select RunPowerShellScript.
Paste the following script in Run Command Script.
## Enter the storage account key for the allowed storage account that you recorded earlier.
$storageAcctKey1 = (pasted from procedure above)
$acctKey = ConvertTo-SecureString -String $storageAcctKey1 -AsPlainText -Force
## Replace the login account with the name of the storage account you created.
$credential = New-Object System.Management.Automation.PSCredential -ArgumentList ("Azure\allowedaccount"), $acctKey
## Replace the storage account name with the name of the storage account you created.
New-PSDrive -Name Z -PSProvider FileSystem -Root "\\allowedaccount.file.core.windows.net\file-share" -Credential $credential
Select Run.
If the drive map is successful, the output in the Output box looks similar to the following example:
Name Used (GB) Free (GB) Provider Root
---- --------- --------- -------- ----
Z FileSystem \\allowedaccount.file.core.windows.net\fil..
Confirm access is denied to the denied storage account
In the search box in the portal, enter Storage accounts. Select Storage accounts in the search results.
Select deniedaccount(random-number).
Expand Security + networking and select Access keys.
Copy the key1 value. You use this key to map a drive to the storage account from the virtual machine you created earlier.
In the search box in the portal, enter Virtual machines. Select Virtual machines in the search results.
Select vm-1.
Expand Operations. Select Run command.
Select RunPowerShellScript.
Paste the following script in Run Command Script.
## Enter the storage account key for the denied storage account that you recorded earlier.
$storageAcctKey2 = (pasted from procedure above)
$acctKey = ConvertTo-SecureString -String $storageAcctKey2 -AsPlainText -Force
## Replace the login account with the name of the storage account you created.
$credential = New-Object System.Management.Automation.PSCredential -ArgumentList ("Azure\deniedaccount"), $acctKey
## Replace the storage account name with the name of the storage account you created.
New-PSDrive -Name Z -PSProvider FileSystem -Root "\\deniedaccount.file.core.windows.net\file-share" -Credential $credential
Select Run.
You receive the following error message in the Output box:
New-PSDrive : Access is denied
At line:1 char:1
+ New-PSDrive -Name Z -PSProvider FileSystem -Root "\\deniedaccount8675 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Z:PSDriveInfo) [New-PSDrive], Win32Exception
+ FullyQualifiedErrorId : CouldNotMapNetworkDrive,Microsoft.PowerShell.Commands.NewPSDriveCommand
The drive map is denied because of the service endpoint policy that restricts access to the storage account.
When you finish using the resources that you created, you can delete the resource group and all its resources.
In the Azure portal, search for and select Resource groups.
On the Resource groups page, select the test-rg resource group.
On the test-rg page, select Delete resource group.
Enter test-rg in Enter resource group name to confirm deletion, and then select Delete.
When no longer needed, you can use Remove-AzResourceGroup to remove the resource group and all of the resources it contains:
$params = @{
Name = "test-rg"
Force = $true
}
Remove-AzResourceGroup @params
When no longer needed, use az group delete to remove the resource group and all of the resources it contains.
az group delete \
--name test-rg \
--yes \
--no-wait
Next steps
In this tutorial, you created a service endpoint policy and associated it to a subnet. To learn more about service endpoint policies, see service endpoint policies overview.