terraform - 尝试创建 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"
}
}
解决方案
您需要首先创建存储帐户和容器,然后在创建 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"
}
参考:
推荐阅读
- python - Python Pandas 对数据点之间的平均值进行上采样(15 分钟到 1 分钟)
- python-3.x - 用单个字符替换字符串中的所有字母
- python - 如何仅使用numpy获取具有给定坐标的ndarray的成对距离矩阵
- python - 对于列表值的字典,找到列表中每个索引的最大值的键
- php - 如何在 PHP 中更改格式编号
- excel - 在不同工作表的列中查找匹配项,然后将特定单元格复制粘贴到 VBA 中的匹配行
- php - 参数 1 传递给 App\Mail\OrderPlaced::__construct()
- python - python为每年创建具有特定结束日期的多年日期范围
- ruby-on-rails - Rails 中的 CSRF - 复制真实性令牌
- python - 没有名为“websockets.legacy”的模块