- The OpenTofu Language
- OpenTofu Settings
- Backends
- azurerm
Backend Type: azurerm
Stores the state as a Blob with the given Key within the Blob Container within the Blob Storage Account.
This backend supports state locking and consistency checking with Azure Blob Storage native capabilities.
Example Configurations​
Access Key​
This examples authenticates using the Access Key associated with the Storage Account:
terraform {
backend "azurerm" {
storage_account_name = "abcd1234"
container_name = "tfstate"
key = "prod.terraform.tfstate"
access_key = "abcdefghijklmnopqrstuvwxyz0123456789..."
}
}
We recommend using the environment variable ARM_ACCESS_KEY instead of storing sensitive information in your source code.
Shared Access Signature​
This example authenticates using the SAS Token, which is a set of query parameters signed with the Access Key associated with the Storage Account:
terraform {
backend "azurerm" {
storage_account_name = "tofu123abc"
container_name = "tofu-state"
key = "prod.terraform.tfstate"
sas_token = "sv=2024-11-04&ss=b&srt=sco&sp=rwdlaciytfx&se=2025-08-05T23:46:49Z&st=2025-08-05T15:31:49Z&spr=https&sig=abcdefg..."
}
}
We recommend using the environment variable ARM_SAS_TOKEN instead of storing sensitive information in your source code.
Using Entra ID (Previously AzureAD)​
The previous two authentication methods are specific to the storage account, and are not tied to any Service Principal. Every authentication method after those two can be modified to use Entra ID authentication rather than storage account-based authentication using the following parameter:
...
use_azuread_auth = true
...
Using Entra ID, you can authenticate directly to the storage account or storage container using the service principal, which only requires the Storage Blob Data Contributor role. Without this parameter, OpenTofu will authenticate and try to obtain the Access Key for the Storage Account.
The Owner role does not automatically grant access to the storage container through Entra ID; you need to explicitly provide a role for the storage account such as Storage Blob Data Contributor. However, having an Owner role does allow the principal to obtain the Access Key to the Storage Account.
If you do not set use_azuread_auth = true, you must set the resource_group_name. A resource_group_name is not required when using Entra ID authentication.
CLI Authentication​
This example authenticates the user with the azure profile set up by the az command.
terraform {
backend "azurerm" {
resource_group_name = "tofu-rg"
storage_account_name = "tofu123abc"
container_name = "tofu-state"
key = "prod.terraform.tfstate"
}
}
You can disable CLI Authentication by setting use_cli = false.
Certificate Authentication​
This example authenticates the principal using a certificate authentication.
terraform {
backend "azurerm" {
resource_group_name = "tofu-rg"
storage_account_name = "tofu123abc"
container_name = "tofu-state"
key = "prod.terraform.tfstate"
subscription_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
tenant_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
client_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
client_certificate_path = "cert.pfx"
}
}
Basic Authentication​
This example authenticates the principal using a basic secret.
terraform {
backend "azurerm" {
resource_group_name = "tofu-rg"
storage_account_name = "tofu123abc"
container_name = "tofu-state"
key = "prod.terraform.tfstate"
subscription_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
tenant_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
client_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
client_secret = "hunter2"
}
}
We recommend using the environment variable ARM_CLIENT_SECRET instead of storing sensitive information in your source code.
OpenID Connect Token​
This example authenticates the principal using an OpenID Connect token for Federated Credentials.
terraform {
backend "azurerm" {
resource_group_name = "tofu-rg"
storage_account_name = "tofu123abc"
container_name = "tofu-state"
key = "prod.terraform.tfstate"
subscription_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
tenant_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
client_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
use_oidc = true
oidc_token_file_path = "token.env"
}
}
GitHub Token Request​
If you're using GitHub Actions, the environment variables ACTIONS_ID_TOKEN_REQUEST_URL and ACTIONS_ID_TOKEN_REQUEST_TOKEN will provide you with a way to obtain the OIDC request token. OpenTofu is programmed to accept these environment variables; a workload with OpenTofu on GitHub Actions using OIDC will have this backend configuration to authenticate.
terraform {
backend "azurerm" {
resource_group_name = "tofu-rg"
storage_account_name = "tofu123abc"
container_name = "tofu-state"
key = "prod.terraform.tfstate"
subscription_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
tenant_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
client_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
use_oidc = true
}
}
Managed Service Identity​
This example authenticates a managed service identity attached to the Azure VM or other compute.
terraform {
backend "azurerm" {
resource_group_name = "tofu-rg"
storage_account_name = "tofu123abc"
container_name = "tofu-state"
key = "prod.terraform.tfstate"
subscription_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
tenant_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
use_msi = true
}
}
AKS Workload Identity​
This example authenticates using workload identity on Azure Kubernetes.
terraform {
backend "azurerm" {
resource_group_name = "tofu-rg"
storage_account_name = "tofu123abc"
container_name = "tofu-state"
key = "prod.terraform.tfstate"
subscription_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
tenant_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
use_aks_workload_identity = true
}
}
Data Source Configuration​
Authentication for a data source works equivalently to the remote state authentication shown above, though with slightly different syntax. For example, this is how to obtain the remote state using CLI Authentication:
data "terraform_remote_state" "foo" {
backend = "azurerm"
config = {
resource_group_name = "tofu-rg"
storage_account_name = "tofu123abc"
container_name = "tofu-state"
key = "prod.terraform.tfstate"
}
}
Configuration Variables​
We recommend using environment variables to supply credentials and other sensitive data. If you use -backend-config or hardcode these values directly in your configuration, OpenTofu will include these values in both the .terraform subdirectory and in plan files. Refer to Credentials and Sensitive Data for details.
The following configuration options are supported:
-
storage_account_name- (Required) The Name of the Storage Account. -
container_name- (Required) The Name of the Storage Container within the Storage Account. -
key- (Required) The name of the Blob used to retrieve/store OpenTofu's State file inside the Storage Container. -
resource_group_name- (Required when not using Entra ID auth) The name of the Resource Group in which the Storage Account exists. -
environment- (Optional) The Azure Cloud Configuration which should be used; set this for special compliance environments like Azure China or Azure US Government. This can also be sourced from theARM_ENVIRONMENTenvironment variable. Possible values arepublic,china, andusgovernment. Defaults topublic. -
endpoint- (Deprecated in 1.1x.xx) The Custom Endpoint for Azure Resource Manager. This can also be sourced from theARM_ENDPOINTenvironment variable.NoteAn
endpointshould only be configured when using Azure Stack. This variable has no effect on operation from OpenTofu version 1.1x.xx onward. -
timeout_seconds- (Optional) The number of seconds before a timeout is reached when attempting to initialize a client, retrieve a Blob or a Metadata from Azure. This can also be sourced from theARM_TIMEOUT_SECONDSenvironment variable. Defaults to300(5 minutes). To disable the timeout, set this to0.NoteSetting
timeout_secondsto0or a large value only disables/extends timeouts originating from OpenTofu. Requests will still time out based on your system's network configuration. -
metadata_host- (Optional) The Hostname of the Azure Metadata Service (for examplemanagement.azure.com), used to obtain the Cloud Environment when using a Custom Azure Environment. This can also be sourced from theARM_METADATA_HOSTNAMEEnvironment Variable.NoteWe recommend
metadata_hostonly be configured when using Azure Stack. -
snapshot- (Optional) Should the Blob used to store the OpenTofu Statefile be snapshotted before use? Defaults tofalse. This value can also be sourced from theARM_SNAPSHOTenvironment variable.NoteRather than using snapshots, we recommend enabling versioning and soft deletion on your Azure storage container.
When authenticating using the Managed Service Identity (MSI) - the following fields are also supported:
-
msi_endpoint- (Deprecated in 1.1x.xx) The path to a custom Managed Service Identity endpoint which is automatically determined if not specified. This can also be sourced from theARM_MSI_ENDPOINTenvironment variable.NoteWe recommend using the MSI_ENDPOINT environment variable instead.
-
subscription_id- (Optional) The Subscription ID in which the Storage Account exists. This can also be sourced from theARM_SUBSCRIPTION_IDenvironment variable. -
tenant_id- (Optional) The Tenant ID in which the Subscription exists. This can also be sourced from theARM_TENANT_IDenvironment variable. -
use_msi- (Optional) Should Managed Service Identity authentication be used? This can also be sourced from theARM_USE_MSIenvironment variable.
When authenticating using the AKS Workload Identity - the following fields are also supported:
-
subscription_id- (Optional) The Subscription ID in which the Storage Account exists. This can also be sourced from theARM_SUBSCRIPTION_IDenvironment variable. -
tenant_id- (Optional) The Tenant ID in which the Subscription exists. This can also be sourced from theARM_TENANT_IDenvironment variable. -
use_aks_workload_identity- (Optional) Set to true to if you want to use Azure's AKS Workload Identity to authenticate to Azure. This will only work if Workload Identity is set up on your Kubernetes workload. This can also be sourced from theARM_USE_AKS_WORKLOAD_IDENTITYenvironment variable.
When authenticating using a Service Principal with OpenID Connect (OIDC) - the following fields are also supported:
-
client_id- (Optional) The Client ID of the Service Principal. This can also be sourced from theARM_CLIENT_IDenvironment variable. -
client_id_file_path- (Optional) The path to a file containing the Client ID of the Service Principal. This can also be sourced from theARM_CLIENT_ID_FILE_PATHenvironment variable. -
subscription_id- (Optional) The Subscription ID in which the Storage Account exists. This can also be sourced from theARM_SUBSCRIPTION_IDenvironment variable. -
tenant_id- (Optional) The Tenant ID in which the Subscription exists. This can also be sourced from theARM_TENANT_IDenvironment variable. -
oidc_request_url- (Optional) The URL for the OIDC provider from which to request an ID token. This can also be sourced from theARM_OIDC_REQUEST_URLorACTIONS_ID_TOKEN_REQUEST_URLenvironment variables. -
oidc_request_token- (Optional) The bearer token for the request to the OIDC provider. This can also be sourced from theARM_OIDC_REQUEST_TOKENorACTIONS_ID_TOKEN_REQUEST_TOKENenvironment variables. -
oidc_token- (Optional) The ID token when authenticating using OpenID Connect (OIDC). This can also be sourced from theARM_OIDC_TOKENenvironment variable. -
oidc_token_file_path- (Optional) The path to a file containing an ID token when authenticating using OpenID Connect (OIDC). This can also be sourced from theARM_OIDC_TOKEN_FILE_PATHenvironment variable. -
use_oidc- (Optional) Should OIDC authentication be used? This can also be sourced from theARM_USE_OIDCenvironment variable.
When authenticating using a SAS Token associated with the Storage Account - the following fields are also supported:
sas_token- (Optional) The SAS Token used to access the Blob Storage Account. This can also be sourced from theARM_SAS_TOKENenvironment variable.
When authenticating using the Storage Account's Access Key - the following fields are also supported:
access_key- (Optional) The Access Key used to access the Blob Storage Account. This can also be sourced from theARM_ACCESS_KEYenvironment variable.
When authenticating using Entra ID Authentication - the following fields are also supported:
-
use_azuread_auth- (Optional) Whether to use Entra ID Authentication to access the Storage Account or Container. This can also be sourced from theARM_USE_AZUREADenvironment variable.NoteWhen using Entra ID for Authentication to Storage you also need to ensure the
Storage Blob Data Contributorrole is assigned.
When authenticating using the command line - the following fields are also supported:
use_cli- (Optional) Whether to authenticate with the CLI. Defaults totrue. This can also be sourced from theARM_USE_CLIenvironment variable.
When authenticating using a Service Principal with a Client Certificate - the following fields are also supported:
-
client_id- (Optional) The Client ID of the Service Principal. This can also be sourced from theARM_CLIENT_IDenvironment variable. -
client_id_file_path- (Optional) The path to a file containing the Client ID of the Service Principal. This can also be sourced from theARM_CLIENT_ID_FILE_PATHenvironment variable. -
client_certificate- (Optional) A Base64-encoded PKCS#12 (PFX, not PEM) certificate used as the Client Certificate when authenticating as a Service Principal. The provided data string must encode both the public certificate and its private key. This can also be sourced from theARM_CLIENT_CERTIFICATEenvironment variable. -
client_certificate_password- (Optional) The password associated with the Client Certificate specified inclient_certificateorclient_certificate_path. This can also be sourced from theARM_CLIENT_CERTIFICATE_PASSWORDenvironment variable. -
client_certificate_path- (Optional) The path to the PKCS#12 PFX file used as the Client Certificate when authenticating as a Service Principal. The file must encode both the public certificate and its private key. This can also be sourced from theARM_CLIENT_CERTIFICATE_PATHenvironment variable. -
subscription_id- (Optional) The Subscription ID in which the Storage Account exists. This can also be sourced from theARM_SUBSCRIPTION_IDenvironment variable. -
tenant_id- (Optional) The Tenant ID in which the Subscription exists. This can also be sourced from theARM_TENANT_IDenvironment variable.
When authenticating using a Service Principal with a Client Secret - the following fields are also supported:
-
client_id- (Optional) The Client ID of the Service Principal. This can also be sourced from theARM_CLIENT_IDenvironment variable. -
client_id_file_path- (Optional) The path to a file containing the Client ID of the Service Principal. This can also be sourced from theARM_CLIENT_ID_FILE_PATHenvironment variable. -
client_secret- (Optional) The Client Secret of the Service Principal. This can also be sourced from theARM_CLIENT_SECRETenvironment variable. -
client_secret_file_path- (Optional) The path to a file containing the Client Secret of the Service Principal. This can also be sourced from theARM_CLIENT_SECRET_FILE_PATHenvironment variable. -
subscription_id- (Optional) The Subscription ID in which the Storage Account exists. This can also be sourced from theARM_SUBSCRIPTION_IDenvironment variable. -
tenant_id- (Optional) The Tenant ID in which the Subscription exists. This can also be sourced from theARM_TENANT_IDenvironment variable.