Terraform apply 命令不使用来自 azure blob 存储的状态文件

Terraform apply command not using its state file from azure blob storage

我在 azure blob 存储帐户中有 terraform 状态,我在我的 jenkins 管道中指的是这个 terraform 状态。

我正在使用 terraform init 命令,它通过使用如下所示的“-backend-config”参数在 blob 存储中引用此 terraform 状态。

terraform init -input=false \
                  -backend-config="resource_group_name=RESOURCE_GROUP_NAME" \
                  -backend-config="storage_account_name=STORAGE_ACCOUNT_NAME" \
                  -backend-config="container_name=CONTAINER_NAME" \
                  -backend-config="key=STATE_FILE_NAME" 

之后我是 运行 terraform plan 命令,它正在将它的数据写入天蓝色 blob 存储中的 terraform 状态,我可以看到这个文件正在 blob 存储帐户中更新。

但是,当我执行 运行 terraform apply 命令时,它正在本地寻找它的 terraform 状态,而我的 jenkins 管道失败了。

所以,我已经尝试提供相同的 terraform 状态文件名,它位于 azure blob 存储中,但它仍然失败,因为它正在 jenkins 管道中本地查找此文件。 因此,想检查我如何强制执行我的 terraform apply 命令以默认从 azure blob 存储中获取其 terraform 状态文件,就像 terraform plan 命令自动从 azure blob 存储中引用它的 terraform 状态文件一样?

最好在 Terraform 后端提供程序中对这些值进行硬编码,因为您不会经常更改它。更改此配置将对您的基础架构产生巨大影响。这些值不是秘密的,特别是如果您将它们传递到保留历史记录的 shell 中。

建议对 Azure 凭据使用 environment 变量。如果要指定后端,请传递这样的文件。

azurecreds.conf:

ARM_SUBSCRIPTION_ID="123"
ARM_CLIENT_ID = "123"
ARM_CLIENT_SECRET="123"
ARM_TENANT_ID="123"

terraform init -backend-config=azurecreds.conf