【发布时间】:2019-11-03 16:37:40
【问题描述】:
我正在尝试从 terraform 创建一个存储帐户,并使用它的一些访问密钥来创建一个 blob 容器。 我的 terraform 配置是从 bash 文件中给出的,因此,这里最重要的步骤之一如下:
customer_prefix=4pd
customer_environment=dev
RESOURCE_GROUP_NAME=$customer_prefix
az group create --name $RESOURCE_GROUP_NAME --location westeurope
# Creating Storage account
STORAGE_ACCOUNT_NAME=4pdterraformstates
az storage account create --resource-group $RESOURCE_GROUP_NAME --name $STORAGE_ACCOUNT_NAME --sku Standard_LRS --encryption-services blob
# We are getting the storage account key to access to it when we need to store the terraform .tf production state file
ACCOUNT_KEY=$(az storage account keys list --resource-group $RESOURCE_GROUP_NAME --account-name $STORAGE_ACCOUNT_NAME --query [0].value -o tsv)
# Creating a blob container
CONTAINER_NAME=4pd-tfstate
az storage container create --name $CONTAINER_NAME --account-name $STORAGE_ACCOUNT_NAME --account-key $ACCOUNT_KEY
source ../terraform_apply_template.sh
我正在从.bash 文件中发送那些az cli 命令,因此,我正在那里发送其他TF_VAR_azurerm ... 变量
最后,当我执行 bash 的第一个 bash 文件时,此调用会调用创建计划的 terraform_apply_template.sh,并应用它。是否如下:
#!/bin/bash
#Terminates script execution after first failed (returned non-zero exit code) command and treat unset variables as errors.
set -ue
terraform init -backend-config=$statefile -backend-config=$storage_access_key -backend-config=$storage_account_name
#TF:
function tf_plan {
terraform plan -out=$outfile
}
case "${1-}" in
apply)
tf_plan && terraform apply $outfile
;;
apply-saved)
if [ ! -f $outfile ]; then tf_plan; fi
terraform apply $outfile
;;
*)
tf_plan
echo ""
echo "Not applying changes. Call one of the following to apply changes:"
echo " - '$0 apply': prepares and applies new plan"
echo " - '$0 apply-saved': applies saved plan ($outfile)"
;;
esac
但我的输出如下,azurerm 后端已初始化,名为 4pdterraformstates 的存储帐户已创建,还有 4pd-tfstate blob 容器,
但在实践中此操作无效,我得到以下输出:
Initializing the backend...
Successfully configured the backend "azurerm"! Terraform will automatically
use this backend unless the backend configuration changes.
Error: Failed to get existing workspaces: storage: service returned error: StatusCode=404, ErrorCode=ContainerNotFound, ErrorMessage=The specified container does not exist.
RequestId:2db5df4e-f01e-014c-369d-272246000000
Time:2019-06-20T19:21:01.6931098Z, RequestInitiated=Thu, 20 Jun 2019 19:21:01 GMT, RequestId=2db5df4e-f01e-014c-369d-272246000000, API Version=2016-05-31, QueryParameterName=, QueryParameterValue=
寻找类似行为,I have found this issue in the azurerm provider terraform repository
而且还根据this issue直接在terraform存储库中创建它看起来像一个网络操作错误...... 但奇怪的是它是固定的..
我使用的是terraform v0.12.1版本
⟩ terraform version
Terraform v0.12.2
根据@Gaurav-Mantri 的回答,有必要等到要配置的存储帐户才能继续执行与存储帐户本身相关的其他任务。
存储帐户的创建是一个异步过程。当您执行 az storage account create 创建存储帐户时,请求会发送到 Azure,并且您会收到一个接受的响应(如果一切顺利的话)。
创建(预配)存储帐户的整个过程需要一些时间(根据我的经验,最多需要 1 分钟),并且在预配存储帐户之前,不允许对该存储帐户进行任何操作。
如何在创建存储帐户后包含等待过程?
似乎仅使用 bash sleep 命令或使用 read 命令暂停 bash shell 一段时间是不够的..
【问题讨论】:
标签: bash azure terraform azure-rm