Azure Storage Account Network Rules
This page shows how to write Terraform and Azure Resource Manager for Storage Account Network Rules and write them securely.
azurerm_storage_account_network_rules (Terraform)
The Account Network Rules in Storage can be configured in Terraform with the resource name azurerm_storage_account_network_rules. The following sections describe 10 examples of how to use the resource and its parameters.
Example Usage from GitHub
resource "azurerm_storage_account_network_rules" "sa2_rules" {
resource_group_name = azurerm_resource_group.rg1.name
storage_account_name = azurerm_storage_account.sa2.name
default_action = "Deny"
ip_rules = [data.azurerm_key_vault_secret.davids_home_ip.value, data.azurerm_key_vault_secret.shanikas_home_ip.value]
resource "azurerm_storage_account_network_rules" "good_example" {
default_action = "Deny"
ip_rules = ["127.0.0.1"]
virtual_network_subnet_ids = [azurerm_subnet.test.id]
bypass = ["Metrics"]
}
resource "azurerm_storage_account_network_rules" "SANetRule" {
resource_group_name = var.resource_group_name
storage_account_name = var.storage_account_name
default_action = var.default_action
ip_rules = var.ip_rules
resource "azurerm_storage_account_network_rules" "test" {
resource_group_name = azurerm_resource_group.test.name
storage_account_name = azurerm_storage_account.test.name
default_action = "Allow"
ip_rules = ["127.0.0.1"]
resource "azurerm_storage_account_network_rules" "network_rules" {
resource_group_name = var.resource_group_name
storage_account_name = var.storage_account_name
default_action = var.default_action
ip_rules = var.ip_rules
resource "azurerm_storage_account_network_rules" "positive3" {
resource_group_name = azurerm_resource_group.test.name
storage_account_name = azurerm_storage_account.test.name
default_action = "Allow"
ip_rules = ["0.0.0.0/0"]
resource "azurerm_storage_account_network_rules" "storageaccountnetrules" {
resource_group_name = azurerm_resource_group.rg-br-infra-prod.name
storage_account_name = azurerm_storage_account.storageaccountproddl.name
default_action = "Allow"
#ip_rules = ["172.25.0.0/16"]
resource "azurerm_storage_account_network_rules" "storage_fw" {
resource_group_name = var.rg_name
storage_account_name = azurerm_storage_account.storage_account.name
default_action = "Deny"
ip_rules = var.ip_rules
resource "azurerm_storage_account_network_rules" "module" {
storage_account_name = var.storage_name
resource_group_name = var.rg_name
default_action = var.storage_net_default_action
ip_rules = var.storage_net_ip_rules
resource "azurerm_storage_account_network_rules" "positive3" {
resource_group_name = azurerm_resource_group.test.name
storage_account_name = azurerm_storage_account.test.name
default_action = "Allow"
ip_rules = ["0.0.0.0/0"]
Security Best Practices for azurerm_storage_account_network_rules
There is 1 setting in azurerm_storage_account_network_rules that should be taken care of for security reasons. The following section explain an overview and example code.
Ensure to allow Trusted Microsoft Services to bypass
It is better to allow Trusted Microsoft Services to bypass. They are not able to access storage account unless rules are set to allow them explicitly.
Parameters
-
bypassoptional computed - set of string -
default_actionrequired - string -
idoptional computed - string -
ip_rulesoptional computed - set of string -
resource_group_namerequired - string -
storage_account_namerequired - string -
virtual_network_subnet_idsoptional computed - set of string -
timeoutssingle block
Explanation in Terraform Registry
Manages network rules inside of a Azure Storage Account.
NOTE: Network Rules can be defined either directly on the
azurerm_storage_accountresource, or using theazurerm_storage_account_network_rulesresource - but the two cannot be used together. Spurious changes will occur if both are used against the same Storage Account.NOTE: Only one
azurerm_storage_account_network_rulescan be tied to anazurerm_storage_account. Spurious changes will occur if more thanazurerm_storage_account_network_rulesis tied to the sameazurerm_storage_account.NOTE: Deleting this resource updates the storage account back to the default values it had when the storage account was created.
Tips: Best Practices for The Other Azure Storage Resources
In addition to the azurerm_storage_account, Azure Storage has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.
azurerm_storage_account
Ensure to use HTTPS connections
It is better to use HTTPS instead of HTTP, which could be vulnerable to person-in-the-middle attacks.
Microsoft.Storage/storageAccounts (Azure Resource Manager)
The storageAccounts in Microsoft.Storage can be configured in Azure Resource Manager with the resource name Microsoft.Storage/storageAccounts. The following sections describe how to use the resource and its parameters.
Example Usage from GitHub
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2019-06-01",
"location": "[parameters('location')]",
"kind": "StorageV2",
"sku": {
"name": "[variables('skuName')]",
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2019-06-01",
"location": "[parameters('location')]",
"kind": "StorageV2",
"sku": {
"name": "[variables('skuName')]",
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2021-01-01",
"name": "[parameters('storageAccounts_kohithdiagstrg_name')]",
"location": "centralus",
"sku": {
"name": "Standard_LRS",
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2021-01-01",
"name": "[parameters('storageAccounts_cs1100320011af67746_name')]",
"location": "southeastasia",
"tags": {
"ms-resource-usage": "azure-cloud-shell"
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2020-08-01-preview",
"name": "[parameters('storageAccountName')]",
"location": "[parameters('location')]",
"dependsOn": [
],
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2019-06-01",
"name": "veeraprathap465",
"location": "eastus",
"sku": {
"name": "Standard_LRS"
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2021-04-01",
"name": "[parameters('storageAccounts_1sinkstorageaccountmgs_name')]",
"location": "eastus",
"sku": {
"name": "Standard_LRS",
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2020-08-01-preview",
"name": "[parameters('storageAccountName')]",
"location": "eastus",
"sku": {
"name": "Standard_LRS",
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2019-06-01",
"name": "[parameters('storageAccounts_sardniceaccountname_name')]",
"location": "westindia",
"sku": {
"name": "Standard_RAGRS",
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2021-04-01",
"name": "[parameters('storageAccounts_tpisprod_name')]",
"location": "eastus",
"tags": {
"System Owner": "IA-TPIS",
Parameters
apiVersionrequired - stringextendedLocationoptionalnameoptional - stringThe name of the extended location.
typeoptional - stringThe type of the extended location.
identityoptionaltyperequired - stringThe identity type.
userAssignedIdentitiesoptional - undefinedGets or sets a list of key value pairs that describe the set of User Assigned identities that will be used with this storage account. The key is the ARM resource identifier of the identity. Only 1 User Assigned identity is permitted here.
kindrequired - stringRequired. Indicates the type of storage account.
locationrequired - stringRequired. Gets or sets the location of the resource. This will be one of the supported and registered Azure Geo Regions (e.g. West US, East US, Southeast Asia, etc.). The geo region of a resource cannot be changed once it is created, but if an identical geo region is specified on update, the request will succeed.
namerequired - stringThe name of the storage account within the specified resource group. Storage account names must be between 3 and 24 characters in length and use numbers and lower-case letters only.
propertiesoptionalaccessTieroptional - stringRequired for storage accounts where kind = BlobStorage. The access tier used for billing.
allowBlobPublicAccessoptional - booleanAllow or disallow public access to all blobs or containers in the storage account. The default interpretation is true for this property.
allowCrossTenantReplicationoptional - booleanAllow or disallow cross AAD tenant object replication. The default interpretation is true for this property.
allowSharedKeyAccessoptional - booleanIndicates whether the storage account permits requests to be authorized with the account access key via Shared Key. If false, then all requests, including shared access signatures, must be authorized with Azure Active Directory (Azure AD). The default value is null, which is equivalent to true.
azureFilesIdentityBasedAuthenticationoptionalactiveDirectoryPropertiesoptionalazureStorageSidrequired - stringSpecifies the security identifier (SID) for Azure Storage.
domainGuidrequired - stringSpecifies the domain GUID.
domainNamerequired - stringSpecifies the primary domain that the AD DNS server is authoritative for.
domainSidrequired - stringSpecifies the security identifier (SID).
forestNamerequired - stringSpecifies the Active Directory forest to get.
netBiosDomainNamerequired - stringSpecifies the NetBIOS domain name.
defaultSharePermissionoptional - stringDefault share permission for users using Kerberos authentication if RBAC role is not assigned.
directoryServiceOptionsrequired - stringIndicates the directory service used.
customDomainoptionalnamerequired - stringGets or sets the custom domain name assigned to the storage account. Name is the CNAME source.
useSubDomainNameoptional - booleanIndicates whether indirect CName validation is enabled. Default value is false. This should only be set on updates.
defaultToOAuthAuthenticationoptional - booleanA boolean flag which indicates whether the default authentication is OAuth or not. The default interpretation is false for this property.
encryptionoptionalidentityoptionaluserAssignedIdentityoptional - stringResource identifier of the UserAssigned identity to be associated with server-side encryption on the storage account.
keySourcerequired - stringThe encryption keySource (provider). Possible values (case-insensitive): Microsoft.Storage, Microsoft.Keyvault.
keyvaultpropertiesoptionalkeynameoptional - stringThe name of KeyVault key.
keyvaulturioptional - stringThe Uri of KeyVault.
keyversionoptional - stringThe version of KeyVault key.
requireInfrastructureEncryptionoptional - booleanA boolean indicating whether or not the service applies a secondary layer of encryption with platform managed keys for data at rest.
servicesoptionalbloboptionalenabledoptional - booleanA boolean indicating whether or not the service encrypts the data as it is stored.
keyTypeoptional - stringEncryption key type to be used for the encryption service. 'Account' key type implies that an account-scoped encryption key will be used. 'Service' key type implies that a default service key is used.
fileoptionalenabledoptional - booleanA boolean indicating whether or not the service encrypts the data as it is stored.
keyTypeoptional - stringEncryption key type to be used for the encryption service. 'Account' key type implies that an account-scoped encryption key will be used. 'Service' key type implies that a default service key is used.
queueoptionalenabledoptional - booleanA boolean indicating whether or not the service encrypts the data as it is stored.
keyTypeoptional - stringEncryption key type to be used for the encryption service. 'Account' key type implies that an account-scoped encryption key will be used. 'Service' key type implies that a default service key is used.
tableoptionalenabledoptional - booleanA boolean indicating whether or not the service encrypts the data as it is stored.
keyTypeoptional - stringEncryption key type to be used for the encryption service. 'Account' key type implies that an account-scoped encryption key will be used. 'Service' key type implies that a default service key is used.
immutableStorageWithVersioningoptionalenabledoptional - booleanA boolean flag which enables account-level immutability. All the containers under such an account have object-level immutability enabled by default.
immutabilityPolicyoptionalallowProtectedAppendWritesoptional - booleanThis property can only be changed for disabled and unlocked time-based retention policies. When enabled, new blocks can be written to an append blob while maintaining immutability protection and compliance. Only new blocks can be added and any existing blocks cannot be modified or deleted.
immutabilityPeriodSinceCreationInDaysoptional - integerThe immutability period for the blobs in the container since the policy creation, in days.
stateoptional - stringThe ImmutabilityPolicy state defines the mode of the policy. Disabled state disables the policy, Unlocked state allows increase and decrease of immutability retention time and also allows toggling allowProtectedAppendWrites property, Locked state only allows the increase of the immutability retention time. A policy can only be created in a Disabled or Unlocked state and can be toggled between the two states. Only a policy in an Unlocked state can transition to a Locked state which cannot be reverted.
isHnsEnabledoptional - booleanAccount HierarchicalNamespace enabled if sets to true.
isNfsV3Enabledoptional - booleanNFS 3.0 protocol support enabled if set to true.
keyPolicyoptionalkeyExpirationPeriodInDaysrequired - integerThe key expiration period in days.
largeFileSharesStateoptional - stringAllow large file shares if sets to Enabled. It cannot be disabled once it is enabled.
minimumTlsVersionoptional - stringSet the minimum TLS version to be permitted on requests to storage. The default interpretation is TLS 1.0 for this property.
networkAclsoptionalbypassoptional - stringSpecifies whether traffic is bypassed for Logging/Metrics/AzureServices. Possible values are any combination of Logging|Metrics|AzureServices (For example, "Logging, Metrics"), or None to bypass none of those traffics.
defaultActionrequired - stringSpecifies the default action of allow or deny when no other rules match.
ipRulesoptional arrayactionoptional - stringThe action of IP ACL rule.
valuerequired - stringSpecifies the IP or IP range in CIDR format. Only IPV4 address is allowed.
resourceAccessRulesoptional arrayresourceIdoptional - stringResource Id
tenantIdoptional - stringTenant Id
virtualNetworkRulesoptional arrayactionoptional - stringThe action of virtual network rule.
idrequired - stringResource ID of a subnet, for example: /subscriptions/{subscriptionId}/resourceGroups/{groupName}/providers/Microsoft.Network/virtualNetworks/{vnetName}/subnets/{subnetName}.
stateoptional - stringGets the state of virtual network rule.
publicNetworkAccessoptional - stringAllow or disallow public network access to Storage Account. Value is optional but if passed in, must be 'Enabled' or 'Disabled'.
routingPreferenceoptionalpublishInternetEndpointsoptional - booleanA boolean flag which indicates whether internet routing storage endpoints are to be published
publishMicrosoftEndpointsoptional - booleanA boolean flag which indicates whether microsoft routing storage endpoints are to be published
routingChoiceoptional - stringRouting Choice defines the kind of network routing opted by the user.
sasPolicyoptionalexpirationActionrequired - stringThe SAS expiration action. Can only be Log.
sasExpirationPeriodrequired - stringThe SAS expiration period, DD.HH:MM:SS.
supportsHttpsTrafficOnlyoptional - booleanAllows https traffic only to storage service if sets to true. The default value is true since API version 2019-04-01.
skurequirednamerequired - stringtieroptional - string
tagsoptional - stringGets or sets a list of key value pairs that describe the resource. These tags can be used for viewing and grouping this resource (across resource groups). A maximum of 15 tags can be provided for a resource. Each tag must have a key with a length no greater than 128 characters and a value with a length no greater than 256 characters.
typerequired - string
Frequently asked questions
What is Azure Storage Account Network Rules?
Azure Storage Account Network Rules is a resource for Storage of Microsoft Azure. Settings can be wrote in Terraform.
Where can I find the example code for the Azure Storage Account Network Rules?
For Terraform, the drhbigdave/azure_python_func_apps, returntocorp/semgrep-rules and prancer-io/prancer-terramerra source code examples are useful. See the Terraform Example section for further details.
For Azure Resource Manager, the prash280887/GDTools, prashantakhouri/GDTools and kohithreddy/Samples source code examples are useful. See the Azure Resource Manager Example section for further details.