- OpenTF 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 Configuration
When authenticating using the Azure CLI or a Service Principal (either with a Client Certificate or a Client Secret):
terraform {
backend "azurerm" {
resource_group_name = "StorageAccount-ResourceGroup"
storage_account_name = "abcd1234"
container_name = "tfstate"
key = "prod.terraform.tfstate"
}
}
When authenticating using Managed Service Identity (MSI):
terraform {
backend "azurerm" {
resource_group_name = "StorageAccount-ResourceGroup"
storage_account_name = "abcd1234"
container_name = "tfstate"
key = "prod.terraform.tfstate"
use_msi = true
subscription_id = "00000000-0000-0000-0000-000000000000"
tenant_id = "00000000-0000-0000-0000-000000000000"
}
}
When authenticating using OpenID Connect (OIDC):
terraform {
backend "azurerm" {
resource_group_name = "StorageAccount-ResourceGroup"
storage_account_name = "abcd1234"
container_name = "tfstate"
key = "prod.terraform.tfstate"
use_oidc = true
subscription_id = "00000000-0000-0000-0000-000000000000"
tenant_id = "00000000-0000-0000-0000-000000000000"
}
}
When authenticating using Azure AD Authentication:
terraform {
backend "azurerm" {
storage_account_name = "abcd1234"
container_name = "tfstate"
key = "prod.terraform.tfstate"
use_azuread_auth = true
subscription_id = "00000000-0000-0000-0000-000000000000"
tenant_id = "00000000-0000-0000-0000-000000000000"
}
}
When using AzureAD for Authentication to Storage you also need to ensure the Storage Blob Data Owner role is assigned.
When authenticating using the Access Key associated with the Storage Account:
terraform {
backend "azurerm" {
storage_account_name = "abcd1234"
container_name = "tfstate"
key = "prod.terraform.tfstate"
# rather than defining this inline, the Access Key can also be sourced
# from an Environment Variable - more information is available below.
access_key = "abcdefghijklmnopqrstuvwxyz0123456789..."
}
}
When authenticating using a SAS Token associated with the Storage Account:
terraform {
backend "azurerm" {
storage_account_name = "abcd1234"
container_name = "tfstate"
key = "prod.terraform.tfstate"
# rather than defining this inline, the SAS Token can also be sourced
# from an Environment Variable - more information is available below.
sas_token = "abcdefghijklmnopqrstuvwxyz0123456789..."
}
}
When using a Service Principal or an Access Key - we recommend using a Partial Configuration for the credentials.
Data Source Configuration
When authenticating using a Service Principal (either with a Client Certificate or a Client Secret):
data "terraform_remote_state" "foo" {
backend = "azurerm"
config = {
storage_account_name = "opentf123abc"
container_name = "opentf-state"
key = "prod.terraform.tfstate"
}
}
When authenticating using Managed Service Identity (MSI):
data "terraform_remote_state" "foo" {
backend = "azurerm"
config = {
resource_group_name = "StorageAccount-ResourceGroup"
storage_account_name = "opentf123abc"
container_name = "opentf-state"
key = "prod.terraform.tfstate"
use_msi = true
subscription_id = "00000000-0000-0000-0000-000000000000"
tenant_id = "00000000-0000-0000-0000-000000000000"
}
}
When authenticating using OpenID Connect (OIDC):
data "terraform_remote_state" "foo" {
backend = "azurerm"
config = {
resource_group_name = "StorageAccount-ResourceGroup"
storage_account_name = "opentf123abc"
container_name = "opentf-state"
key = "prod.terraform.tfstate"
use_oidc = true
subscription_id = "00000000-0000-0000-0000-000000000000"
tenant_id = "00000000-0000-0000-0000-000000000000"
}
}
When authenticating using AzureAD Authentication:
data "terraform_remote_state" "foo" {
backend = "azurerm"
config = {
storage_account_name = "opentf123abc"
container_name = "opentf-state"
key = "prod.terraform.tfstate"
use_azuread_auth = true
subscription_id = "00000000-0000-0000-0000-000000000000"
tenant_id = "00000000-0000-0000-0000-000000000000"
}
}
When using AzureAD for Authentication to Storage you also need to ensure the Storage Blob Data Owner role is assigned.
When authenticating using the Access Key associated with the Storage Account:
data "terraform_remote_state" "foo" {
backend = "azurerm"
config = {
storage_account_name = "opentf123abc"
container_name = "opentf-state"
key = "prod.terraform.tfstate"
# rather than defining this inline, the Access Key can also be sourced
# from an Environment Variable - more information is available below.
access_key = "abcdefghijklmnopqrstuvwxyz0123456789..."
}
}
When authenticating using a SAS Token associated with the Storage Account:
data "terraform_remote_state" "foo" {
backend = "azurerm"
config = {
storage_account_name = "opentf123abc"
container_name = "opentf-state"
key = "prod.terraform.tfstate"
# rather than defining this inline, the SAS Token can also be sourced
# from an Environment Variable - more information is available below.
sas_token = "abcdefghijklmnopqrstuvwxyz0123456789..."
}
}
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, OpenTF 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 OpenTF's State file inside the Storage Container.environment- (Optional) The Azure Environment which should be used. This can also be sourced from theARM_ENVIRONMENTenvironment variable. Possible values arepublic,china,german,stackandusgovernment. Defaults topublic.endpoint- (Optional) 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.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.snapshot- (Optional) Should the Blob used to store the OpenTF Statefile be snapshotted before use? Defaults tofalse. This value can also be sourced from theARM_SNAPSHOTenvironment variable.
When authenticating using the Managed Service Identity (MSI) - the following fields are also supported:
resource_group_name- (Required) The Name of the Resource Group in which the Storage Account exists.msi_endpoint- (Optional) 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.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 a Service Principal with OpenID Connect (OIDC) - the following fields are also supported:
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 AzureAD Authentication - the following fields are also supported:
use_azuread_auth- (Optional) Should AzureAD Authentication be used to access the Blob Storage Account. This can also be sourced from theARM_USE_AZUREADenvironment variable.NoteWhen using AzureAD for Authentication to Storage you also need to ensure the
Storage Blob Data Ownerrole is assigned.
When authenticating using a Service Principal with a Client Certificate - the following fields are also supported:
resource_group_name- (Required) The Name of the Resource Group in which the Storage Account exists.client_id- (Optional) The Client ID of the Service Principal. This can also be sourced from theARM_CLIENT_IDenvironment variable.client_certificate_password- (Optional) The password associated with the Client Certificate specified inclient_certificate_path. This can also be sourced from theARM_CLIENT_CERTIFICATE_PASSWORDenvironment variable.client_certificate_path- (Optional) The path to the PFX file used as the Client Certificate when authenticating as a Service Principal. 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:
resource_group_name- (Required) The Name of the Resource Group in which the Storage Account exists.client_id- (Optional) The Client ID of the Service Principal. This can also be sourced from theARM_CLIENT_IDenvironment variable.client_secret- (Optional) The Client Secret of the Service Principal. This can also be sourced from theARM_CLIENT_SECRETenvironment 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.