Azure Storage Account
This page shows how to write Terraform and Azure Resource Manager for Storage Account and write them securely.
azurerm_storage_account (Terraform)
The Account in Storage can be configured in Terraform with the resource name azurerm_storage_account. The following sections describe 6 examples of how to use the resource and its parameters.
Example Usage from GitHub
resource "azurerm_storage_account" "bb_Premium_ZRS" {
name = "storageaccountname"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
account_kind = "BlockBlobStorage"
account_tier = "Premium"
resource "azurerm_storage_account" "allowed" {
account_kind = "BlobStorage"
}
resource "azurerm_storage_account" "allowed_2" {
account_kind = "BlockBlobStorage"
resource "azurerm_storage_account" "default" {
name = "staspcmpgitops"
resource_group_name = azurerm_resource_group.default.name
location = azurerm_resource_group.default.location
account_tier = "Standard"
resource "azurerm_storage_account" "good_example" {
name = "storageaccountname"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
min_tls_version = "TLS1_2"
}
resource "azurerm_storage_account" "tamops-sa" {
name = "tamopssa"
resource_group_name = azurerm_resource_group.tamops-rg.name
location = azurerm_resource_group.tamops-rg.location
account_tier = "Standard"
resource "azurerm_storage_account" "dev" {
name = var.storage_dev
resource_group_name = azurerm_resource_group.dev.name
location = azurerm_resource_group.dev.location
account_tier = "Standard"
account_replication_type = "LRS"
Security Best Practices for azurerm_storage_account
There are 6 settings in azurerm_storage_account that should be taken care of for security reasons. The following section explain an overview and example code.
Ensure to use HTTPS connections
It is better to use HTTPS instead of HTTP, which could be vulnerable to person-in-the-middle attacks.
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.
Ensure to enable the latest TLS/SSL policy for the load balancer
It is better to enable the latest TLS/SSL policy for the load balancer. Three versions of the TLS protocol, 1.0, 1.1, and 1.2 are available at the moment. TLS 1.2 should be selected if you do not have special reasons.
Ensure to set the default action on the network rules to deny
It is better to set the "Deny" as the default action on the network rules. The default_action for network rules will be executed when any rules are not matched.
Ensure to set the default action on the network rules to deny
It is better to set the "Deny" as the default action on the network rules. The default_action for network rules will be executed when any rules are not matched.
Ensure to enable logging for queue services
It is better to enable logging for queue services indicating the details of successful and failed requests to storage services.
Parameters
-
access_tieroptional computed - string -
account_kindoptional - string -
account_replication_typerequired - string -
account_tierrequired - string -
allow_blob_public_accessoptional - bool -
enable_https_traffic_onlyoptional - bool -
idoptional computed - string -
is_hns_enabledoptional - bool -
large_file_share_enabledoptional computed - bool -
locationrequired - string -
min_tls_versionoptional - string -
namerequired - string -
primary_access_keyoptional computed - string -
primary_blob_connection_stringoptional computed - string -
primary_blob_endpointoptional computed - string -
primary_blob_hostoptional computed - string -
primary_connection_stringoptional computed - string -
primary_dfs_endpointoptional computed - string -
primary_dfs_hostoptional computed - string -
primary_file_endpointoptional computed - string -
primary_file_hostoptional computed - string -
primary_locationoptional computed - string -
primary_queue_endpointoptional computed - string -
primary_queue_hostoptional computed - string -
primary_table_endpointoptional computed - string -
primary_table_hostoptional computed - string -
primary_web_endpointoptional computed - string -
primary_web_hostoptional computed - string -
resource_group_namerequired - string -
secondary_access_keyoptional computed - string -
secondary_blob_connection_stringoptional computed - string -
secondary_blob_endpointoptional computed - string -
secondary_blob_hostoptional computed - string -
secondary_connection_stringoptional computed - string -
secondary_dfs_endpointoptional computed - string -
secondary_dfs_hostoptional computed - string -
secondary_file_endpointoptional computed - string -
secondary_file_hostoptional computed - string -
secondary_locationoptional computed - string -
secondary_queue_endpointoptional computed - string -
secondary_queue_hostoptional computed - string -
secondary_table_endpointoptional computed - string -
secondary_table_hostoptional computed - string -
secondary_web_endpointoptional computed - string -
secondary_web_hostoptional computed - string -
tagsoptional - map from string to string -
blob_propertieslist block-
container_delete_retention_policylist block-
daysoptional - number
-
-
cors_rulelist block-
allowed_headersrequired - list of string -
allowed_methodsrequired - list of string -
allowed_originsrequired - list of string -
exposed_headersrequired - list of string -
max_age_in_secondsrequired - number
-
-
delete_retention_policylist block-
daysoptional - number
-
-
-
custom_domainlist block-
namerequired - string -
use_subdomainoptional - bool
-
-
identitylist block-
principal_idoptional computed - string -
tenant_idoptional computed - string -
typerequired - string
-
-
network_ruleslist block-
bypassoptional computed - set of string -
default_actionrequired - string -
ip_rulesoptional computed - set of string -
virtual_network_subnet_idsoptional computed - set of string
-
-
queue_propertieslist block-
cors_rulelist block-
allowed_headersrequired - list of string -
allowed_methodsrequired - list of string -
allowed_originsrequired - list of string -
exposed_headersrequired - list of string -
max_age_in_secondsrequired - number
-
-
hour_metricslist block-
enabledrequired - bool -
include_apisoptional - bool -
retention_policy_daysoptional - number -
versionrequired - string
-
-
logginglist block-
deleterequired - bool -
readrequired - bool -
retention_policy_daysoptional - number -
versionrequired - string -
writerequired - bool
-
-
minute_metricslist block-
enabledrequired - bool -
include_apisoptional - bool -
retention_policy_daysoptional - number -
versionrequired - string
-
-
-
static_websitelist block-
error_404_documentoptional - string -
index_documentoptional - string
-
-
timeoutssingle block
Explanation in Terraform Registry
Manages an Azure Storage Account.
Tips: Best Practices for The Other Azure Storage Resources
In addition to the azurerm_storage_account_network_rules, 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_network_rules
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.
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?
Azure Storage Account 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?
For Terraform, the gilyas/infracost, snyk-labs/infrastructure-as-code-goof and CMPGitOpsInnovation/logic-app-ase 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.