Satyam Pandey
Satyam Pandey

Reputation: 743

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

on the way to create aks via terraform, here i want to create azure storage account & want to use the same same account to store the terraform state file.

however getting below error

│ Error: Error loading state: Error retrieving keys for Storage Account "azurerm_resource_group.aks_rg.name": storage.AccountsClient#ListKeys: Invalid input: autorest/validation: validation failed: parameter=accountName constraint=MaxLength value="azurerm_resource_group.aks_rg.name" details: value length must be less than or equal to 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"
  }
}

enter image description here

[1]: https://i.sstatic.net/78tG6.png

Upvotes: 3

Views: 2781

Answers (1)

Ansuman Bal
Ansuman Bal

Reputation: 11421

You need to first create the storage account and container then while creating the aks cluster you need to give the below:

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

Instead of creating the storage account and container ins the same file while storing the terraform tfstate.

Example:

Create storage account and container:

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

enter image description here

Then create the aks resource group and store the tfstate in container.

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

enter image description here

enter image description here

Reference:

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

Upvotes: 1

Related Questions