【问题标题】:Trying to create azure storage account & use the same to store terraform state file尝试创建 azure 存储帐户并使用它来存储 terraform 状态文件
【发布时间】:2021-10-29 06:23:37
【问题描述】:

在通过 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"
  }
}

【问题讨论】:

    标签: terraform azure-aks terraform-provider-azure


    【解决方案1】:

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

    【讨论】:

    • 我想与容器一起创建存储帐户并想存储状态文件,我已经更新了代码,似乎有些问题,我认为它无法使用变量后端“azurerm”传递值{}
    • 您好@SatyamPandey,不幸的是,这是不可能的,因为 terraform 后端将需要存储帐户的凭据来存储 tfstate。但是当您同时创建它时,它会出错告诉找不到密钥
    • 只有在你已经创建了存储账户的情况下才能传递后端,以便terraform可以引用它并进行初始化。
    • 要存储 tfstate,您需要预先配置存储帐户和容器,以便 terraform 能够使用它。
    • 谢谢,现在我不需要,我必须硬编码这些值。
    猜你喜欢
    • 1970-01-01
    • 2017-01-24
    • 2021-08-01
    • 1970-01-01
    • 2021-10-29
    • 2020-04-29
    • 2017-11-23
    • 2021-05-20
    • 2021-11-17
    相关资源
    最近更新 更多