- OpenTofu 语言
- OpenTofu 设置
- 后端
- azurerm
后端类型:azurerm
将状态存储为 Blob,该 Blob 具有给定的 Key,位于 Blob 容器内 Blob 存储帐户 中。
此后端支持使用 Azure Blob 存储的原生功能进行状态锁定和一致性检查。
示例配置
使用 Azure CLI 或服务主体进行身份验证时(使用客户端证书或客户端密钥)
terraform {
backend "azurerm" {
resource_group_name = "StorageAccount-ResourceGroup"
storage_account_name = "abcd1234"
container_name = "tfstate"
key = "prod.terraform.tfstate"
}
}
使用托管服务标识 (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"
}
}
使用 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"
}
}
使用 Azure AD 身份验证进行身份验证时
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"
}
}
当使用 AzureAD 进行存储身份验证时,还需要确保已分配“存储 Blob 数据所有者”角色。
使用与存储帐户关联的访问密钥进行身份验证时
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..."
}
}
使用与存储帐户关联的 SAS 令牌进行身份验证时
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..."
}
}
使用服务主体或访问密钥时 - 我们建议使用 部分配置 来配置凭据。
数据源配置
使用服务主体进行身份验证时(使用客户端证书或客户端密钥)
data "terraform_remote_state" "foo" {
backend = "azurerm"
config = {
storage_account_name = "tofu123abc"
container_name = "tofu-state"
key = "prod.terraform.tfstate"
}
}
使用托管服务标识 (MSI) 进行身份验证时
data "terraform_remote_state" "foo" {
backend = "azurerm"
config = {
resource_group_name = "StorageAccount-ResourceGroup"
storage_account_name = "tofu123abc"
container_name = "tofu-state"
key = "prod.terraform.tfstate"
use_msi = true
subscription_id = "00000000-0000-0000-0000-000000000000"
tenant_id = "00000000-0000-0000-0000-000000000000"
}
}
使用 OpenID Connect (OIDC) 进行身份验证时
data "terraform_remote_state" "foo" {
backend = "azurerm"
config = {
resource_group_name = "StorageAccount-ResourceGroup"
storage_account_name = "tofu123abc"
container_name = "tofu-state"
key = "prod.terraform.tfstate"
use_oidc = true
subscription_id = "00000000-0000-0000-0000-000000000000"
tenant_id = "00000000-0000-0000-0000-000000000000"
}
}
使用 AzureAD 身份验证进行身份验证时
data "terraform_remote_state" "foo" {
backend = "azurerm"
config = {
storage_account_name = "tofu123abc"
container_name = "tofu-state"
key = "prod.terraform.tfstate"
use_azuread_auth = true
subscription_id = "00000000-0000-0000-0000-000000000000"
tenant_id = "00000000-0000-0000-0000-000000000000"
}
}
当使用 AzureAD 进行存储身份验证时,还需要确保已分配“存储 Blob 数据所有者”角色。
使用与存储帐户关联的访问密钥进行身份验证时
data "terraform_remote_state" "foo" {
backend = "azurerm"
config = {
storage_account_name = "tofu123abc"
container_name = "tofu-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..."
}
}
使用与存储帐户关联的 SAS 令牌进行身份验证时
data "terraform_remote_state" "foo" {
backend = "azurerm"
config = {
storage_account_name = "tofu123abc"
container_name = "tofu-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..."
}
}
配置变量
我们建议使用环境变量来提供凭据和其他敏感数据。如果使用 -backend-config
或在配置中直接硬编码这些值,OpenTofu 将在 .terraform
子目录和计划文件中包含这些值。有关详细信息,请参阅 凭据和敏感数据。
支持以下配置选项
-
storage_account_name
- (必需)存储帐户 的名称。 -
container_name
- (必需)存储帐户中 存储容器 的名称。 -
key
- (必需)用于在存储容器内检索/存储 OpenTofu 状态文件的 Blob 的名称。 -
environment
- (可选)应使用的 Azure 环境。这也可以从ARM_ENVIRONMENT
环境变量中获取。可能的值包括public
、china
、german
、stack
和usgovernment
。默认为public
。 -
endpoint
- (可选)Azure 资源管理器的自定义终结点。这也可以从ARM_ENDPOINT
环境变量中获取。注意仅在使用 Azure Stack 时才应配置
endpoint
。 -
metadata_host
- (可选)Azure 元数据服务的主机名(例如management.azure.com
),用于在使用自定义 Azure 环境时获取云环境。这也可以从ARM_METADATA_HOSTNAME
环境变量中获取。 -
snapshot
- (可选)在使用前是否应对用于存储 OpenTofu 状态文件的 Blob 进行快照?默认为false
。此值也可以从ARM_SNAPSHOT
环境变量中获取。
使用托管服务标识 (MSI) 进行身份验证时,还支持以下字段
-
resource_group_name
- (必需)存储帐户所在的资源组的名称。 -
msi_endpoint
- (可选)自定义托管服务标识终结点的路径,如果未指定则自动确定。这也可以从ARM_MSI_ENDPOINT
环境变量中获取。 -
subscription_id
- (可选)存储帐户所在的订阅 ID。这也可以从ARM_SUBSCRIPTION_ID
环境变量中获取。 -
tenant_id
- (可选)订阅所在的租户 ID。也可以从ARM_TENANT_ID
环境变量中获取。 -
use_msi
- (可选)是否使用托管服务标识身份验证?也可以从ARM_USE_MSI
环境变量中获取。
使用 OpenID Connect (OIDC) 和服务主体进行身份验证时,也支持以下字段
-
oidc_request_url
- (可选)用于请求 ID 令牌的 OIDC 提供程序的 URL。也可以从ARM_OIDC_REQUEST_URL
或ACTIONS_ID_TOKEN_REQUEST_URL
环境变量中获取。 -
oidc_request_token
- (可选)对 OIDC 提供程序的请求的承载令牌。也可以从ARM_OIDC_REQUEST_TOKEN
或ACTIONS_ID_TOKEN_REQUEST_TOKEN
环境变量中获取。 -
oidc_token
- (可选)使用 OpenID Connect (OIDC) 进行身份验证时的 ID 令牌。也可以从ARM_OIDC_TOKEN
环境变量中获取。 -
oidc_token_file_path
- (可选)使用 OpenID Connect (OIDC) 进行身份验证时,包含 ID 令牌的文件的路径。也可以从ARM_OIDC_TOKEN_FILE_PATH
环境变量中获取。 -
use_oidc
- (可选)是否使用 OIDC 身份验证?也可以从ARM_USE_OIDC
环境变量中获取。
使用与存储帐户关联的 SAS 令牌进行身份验证时,也支持以下字段
sas_token
- (可选)用于访问 Blob 存储帐户的 SAS 令牌。也可以从ARM_SAS_TOKEN
环境变量中获取。
使用存储帐户的访问密钥进行身份验证时,也支持以下字段
access_key
- (可选)用于访问 Blob 存储帐户的访问密钥。也可以从ARM_ACCESS_KEY
环境变量中获取。
使用 AzureAD 身份验证进行身份验证时,也支持以下字段
-
use_azuread_auth
- (可选)是否应使用 AzureAD 身份验证访问 Blob 存储帐户。也可以从ARM_USE_AZUREAD
环境变量中获取。注意当使用 AzureAD 进行存储身份验证时,还需要确保已分配“存储 Blob 数据所有者”角色。
使用客户端证书和服务主体进行身份验证时,也支持以下字段
-
resource_group_name
- (必需)存储帐户所在的资源组的名称。 -
client_id
- (可选)服务主体的客户端 ID。也可以从ARM_CLIENT_ID
环境变量中获取。 -
client_certificate_password
- (可选)与client_certificate_path
中指定的客户端证书关联的密码。也可以从ARM_CLIENT_CERTIFICATE_PASSWORD
环境变量中获取。 -
client_certificate_path
- (可选)作为服务主体进行身份验证时用作客户端证书的 PFX 文件的路径。也可以从ARM_CLIENT_CERTIFICATE_PATH
环境变量中获取。 -
subscription_id
- (可选)存储帐户所在的订阅 ID。这也可以从ARM_SUBSCRIPTION_ID
环境变量中获取。 -
tenant_id
- (可选)订阅所在的租户 ID。也可以从ARM_TENANT_ID
环境变量中获取。
使用客户端密钥和服务主体进行身份验证时,也支持以下字段
-
resource_group_name
- (必需)存储帐户所在的资源组的名称。 -
client_id
- (可选)服务主体的客户端 ID。也可以从ARM_CLIENT_ID
环境变量中获取。 -
client_secret
- (可选)服务主体的客户端密钥。也可以从ARM_CLIENT_SECRET
环境变量中获取。 -
subscription_id
- (可选)存储帐户所在的订阅 ID。这也可以从ARM_SUBSCRIPTION_ID
环境变量中获取。 -
tenant_id
- (可选)订阅所在的租户 ID。也可以从ARM_TENANT_ID
环境变量中获取。