创建存储帐户后无法立即创建天蓝色的容器

时间:2019-06-20 19:58:32

标签: bash azure terraform azure-rm

我正在尝试从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

我从az cli文件发送那些.bash命令,因此,我在那发送其他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容器

enter image description here

但是在实践中此操作无效,我得到以下输出:

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

而且根据直接在terraform信息库中创建的this issue,这看起来像是网络操作错误... 但是奇怪的是它是固定的..

我正在使用terraform v0.12.1版本

⟩ terraform version
Terraform v0.12.2

根据@ Gaurav-Mantri的回答,有必要等到要配置存储帐户后,才能继续执行与存储帐户本身相关的其他任务。

  

创建存储帐户是一个异步过程。当您执行z存储帐户创建以创建存储帐户时,请求将发送到Azure,并且您会收到可接受的响应(如果一切顺利)。

     

创建(设置)存储帐户的整个过程需要一些时间(根据我的经验,最多需要1分钟),并且在配置该存储帐户之前,不允许对该存储帐户进行任何操作。

创建存储帐户后如何包括等待过程? 似乎仅使用bash sleep命令或使用read命令暂停bash shell一段时间是不够的。

1 个答案:

答案 0 :(得分:1)

创建存储帐户是一个异步过程。当您执行az storage account create创建存储帐户时,请求将发送到Azure,并且您会收到accepted的响应(如果一切顺利的话)。

创建(设置)存储帐户的整个过程需要一些时间(根据我的经验,最多需要1分钟),并且在配置该存储帐户之前,不允许对该存储帐户进行任何操作。

这是您收到错误消息的原因,因为您在发送创建请求后立即尝试执行操作。

不确定如何执行,但是需要做的就是等待配置存储帐户。您可以定期(例如每5秒一次)获取存储帐户的属性,并检查其配置状态。供应状态成功后,您可以尝试创建一个容器。那时您的请求应该会成功。