首页 > 解决方案 > 尝试创建 azure 存储帐户并使用它来存储 terraform 状态文件

问题描述

在通过 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 │</p>

#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"
  }
}

在此处输入图像描述

[1]:https://i.stack.imgur.com/78tG6.png

标签: terraformazure-aksterraform-provider-azure

解决方案


您需要首先创建存储帐户和容器,然后在创建 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"
}

在此处输入图像描述

在此处输入图像描述

参考:

如何在 Azure 存储中存储 Terraform 状态文件。» 豪尔赫·伯恩哈特


推荐阅读