尝试创建 azure 存储帐户并使用它来存储 terraform 状态文件

Trying to create azure storage account & use the same to store terraform state file

在通过 terraform 创建 aks 的过程中,这里我想创建 azure 存储帐户并希望使用相同的帐户来存储 terraform 状态文件。

然而低于错误

│ 错误:加载状态错误:检索存储帐户“azurerm_resource_group.aks_rg.name”的密钥时出错:storage.AccountsClient#ListKeys:输入无效:autorest/validation:验证失败:参数=accountName 约束=MaxLength value="azurerm_resource_group.aks_rg.name" 详情:值长度必须小于等于24 │

#Create Resource Group
resource "azurerm_resource_group" "aks_rg" {
  location = "${var.location}"
  name     = "${var.global-prefix}-${var.cluster-id}-${var.environment}-azwe-aks-rg"
}

#Create Storage Account & Container
resource "azurerm_storage_account" "storage_acc" {
  name                     = "${var.cluster-id}-storage-account"
  resource_group_name      = azurerm_resource_group.aks_rg.name
  location                 = azurerm_resource_group.aks_rg.location
  account_tier             = "Standard"
  account_replication_type = "LRS" 
}
resource "azurerm_storage_container" "storage_container" {
  name                  = "${var.cluster-id}-storage-account-container"
  storage_account_name  = azurerm_storage_account.storage_acc.name
  container_access_type = "private"
}

#store terraform state in remote container
terraform {
  # Configure Terraform State Storage
  backend "azurerm" {
    resource_group_name  = "azurerm_resource_group.aks_rg.name"
    storage_account_name = "azurerm_storage_container.storage_acc.name"
    container_name       = "azurerm_storage_container.storage_container.name"
    key                  = "terraform.tfstate"
  }
}

您需要先创建存储帐户和容器,然后在创建 aks 集群时需要提供以下内容:

terraform {
  # Configure Terraform State Storage
  backend "azurerm" {
    resource_group_name  = "azurerm_resource_group.aks_rg.name"
    storage_account_name = "azurerm_resource_group.aks_rg.name"
    container_name       = "powermeprodtfstate"
    key                  = "terraform.tfstate"
  }
}

而不是在存储 terraform tfstate 时在同一个文件中创建存储帐户和容器。

示例:

创建存储帐户和容器:

provider "azurerm" { 
  features {}
}

data "azurerm_resource_group" "example" {
  name     = "resourcegroupname"
}

resource "azurerm_storage_account" "example" {
  name                     = "yourstorageaccountname"
  resource_group_name      = data.azurerm_resource_group.example.name
  location                 = data.azurerm_resource_group.example.location
  account_tier             = "Standard"
  account_replication_type = "LRS" 
}
resource "azurerm_storage_container" "example" {
  name                  = "terraform"
  storage_account_name  = azurerm_storage_account.example.name
  container_access_type = "private"
}

然后创建aks资源组并将tfstate存储在容器中。

provider "azurerm" { 
  features {}
}
terraform {
  # Configure Terraform State Storage
  backend "azurerm" {
    resource_group_name  = "resourcegroup"
    storage_account_name = "storageaccountnameearliercreated"
    container_name       = "terraform"
    key                  = "terraform.tfstate"
  }
}

resource "azurerm_resource_group" "aks_rg" {
 name = "aks-rg"
 location = "west us"
}

参考:

How to store the Terraform state file in Azure Storage. » Jorge Bernhardt