首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试创建蔚蓝存储帐户&使用该帐户存储terraform状态文件

尝试创建蔚蓝存储帐户&使用该帐户存储terraform状态文件
EN

Stack Overflow用户
提问于 2021-08-30 13:40:09
回答 1查看 1.3K关注 0票数 1

在通过terraform创建ak的过程中,这里我想创建一个天蓝色的存储帐户&希望使用相同的帐户来存储terraform状态文件。

然而,越来越少的错误

错误加载状态:错误检索存储帐户“azurerm_resource_group.aks_rg.name”的键: storage.AccountsClient#ListKeys:无效输入:自动休息/验证:验证失败: parameter=accountName constraint=MaxLength value="azurerm_resource_group.aks_rg.name“详细信息:值长度必须小于或等于24│

代码语言:javascript
复制
#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"
  }
}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-30 18:36:55

您需要首先创建存储帐户和容器,然后在创建ak集群时,您需要给出以下内容:

代码语言:javascript
复制
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时在同一个文件中创建存储帐户和容器。

示例:

创建存储帐户和容器:

代码语言:javascript
复制
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"
}

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

代码语言:javascript
复制
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状态文件。豪尔赫·伯恩哈特

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68985139

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档