terraformterraform-provider-azureterraform0.12+acr

AKS integration with ACR


trying to create ACR and integrate the same with existing AKS cluster

below is the resource block where will be doing role assignment {User Assigned Managed Identity} to aks nodepool and trying datablock to fetch existing aks details

#Create Resource Group 
resource "azurerm_resource_group" "acr_rg" {
  location = var.location
  name     = "${var.global-prefix}-${var.repo-id}-rg"
}

#Create ACR Registry for Powerme
resource "azurerm_container_registry" "acr" {
  name                = var.repo-id
  resource_group_name = azurerm_resource_group.acr_rg.name
  location            = azurerm_resource_group.acr_rg.location
  sku                 = "Premium"
  admin_enabled       = true
}

#Featching AKS details for Integration with ACR
data "azurerm_kubernetes_cluster" "aks_cluster" {
  resource_group_name = var.aks_rg
  name                = var.k8s_cluster
}

#Role assignment of vmss to acr
resource "azurerm_role_assignment" "acrpull_role" {
  scope                = azurerm_container_registry.acr.id
  role_definition_name = "AcrPull"
  principal_id         = data.azurerm_kubernetes_cluster.aks_cluster.kubelet_identity[0].object_id
}

error

$ terraform plan
╷
│ Error: Error: Managed Kubernetes Cluster "mycluster" was not found in Resource Group "myresource"
│ 
│   with data.azurerm_kubernetes_cluster.aks_cluster,
│   on acr.tf line 17, in data "azurerm_kubernetes_cluster" "aks_cluster":
│   17: data "azurerm_kubernetes_cluster" "aks_cluster" {
╵

Solution

  • If you have multiple subscriptions access, Please ensure you are setting the subscription that has the aks cluster and the resource group by using the below command:

    az account set --subscription "your subscription you want to use"
    

    After the Subscription is set , You will be successfully able to get the AKS cluster and resource group using the same code .