Vincenzo
Vincenzo

Reputation: 6358

Terraform provider's resources not available in other tf files

I'm just starting with terraform and I just don't grasp some of the basics. I have two problems

I have a main.tf file where I initialise Terraform, declare 2 providers for Azure and Kubernetes, and define two modules.

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 3.0.2"
    }
  }

  required_version = ">= 1.1.0"
}
provider "kubernetes" {
    host                   =  module.cluster.host
    client_certificate     =  module.cluster.client_certificate
    client_key             =  module.cluster.client_key
    cluster_ca_certificate =  module.cluster.cluster_ca_certificate
}

provider "azurerm" {
  features {}
}
module "cluster" {
  source = "./modules/cluster"
  location = var.location
  kubernetes_version = var.kubernetes_version
  ssh_key = var.ssh_key
  
}
module "network" {
  source = "./modules/network"
  
}

then in modules/cluster/cluster.tf I setup the resource group and the cluster. when I define a new resource all azure and kubernetes providers modules are available.

I'm trying to add two new resources azurem_public_ip and an azurem_lb in modules/network/network.tf but the azurerm modules are not available to select enter image description here

If I try and create those two resources in `modules/cluster/cluster.tf they are available

enter image description here

and I can write the so:

resource "azurerm_resource_group" "resource_group" {
  name     = var.resource_group_name
  location = var.location
    tags = {
    Environment = "Production"
    Team = "DevOps"
  }
}

resource "azurerm_kubernetes_cluster" "server_cluster" {
...
}

resource "azurerm_public_ip" "public-ip" {
  name                = "PublicIPForLB"
  location            = azurerm_resource_group.resource_group.location
  resource_group_name = azurerm_resource_group.resource_group.name
  allocation_method   = "Static"
}

resource "azurerm_lb" "load-balancer" {
  name                = "TestLoadBalancer"
  location            = azurerm_resource_group.resource_group.location
  resource_group_name = azurerm_resource_group.resource_group.name

  frontend_ip_configuration {
    name                 = "PublicIPAddress"
    public_ip_address_id = azurerm_public_ip.public-ip.id
  }
}

Why aren't the providers available in network.tf? Shouldn't they be available in the network module as they are in the cluster module? From the docs network module should inherit the azurerm provider from main.tf

If not specified, the child >module inherits all of the >default (un-aliased) provider >configurations from the calling >module.

Many thanks for your help.

Upvotes: 0

Views: 577

Answers (1)

Swarna Anipindi
Swarna Anipindi

Reputation: 954

Tried to replicate the same scenario using below mentioned code. Everything is working as expected. Hope issue may cause because of module declaration.

Main tf code as follows:

provider "azurerm" {
    features {}
}

provider "kubernetes" {
    host                   =  module.cluster.host
    client_certificate     =  module.cluster.client_certificate
    client_key             =  module.cluster.client_key
    cluster_ca_certificate =  module.cluster.cluster_ca_certificate
}

module "cluster" {
  source = "./modules/cluster"
}
module "network" {
  source = "./modules/network"
  
}

NOTE: No more parameters require on provider tf file. Just declaration of module is sufficient.

Provider tf file as follows:

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 3.0.2"
    }
  }

  required_version = ">= 1.1.0"
}

Cluster file as follows:

resource "azurerm_kubernetes_cluster" "example" {
  name                = "swarnaexample-aks1"
  location            = "East Us"
  resource_group_name =  "*********"
  dns_prefix          = "exampleaks1"

  default_node_pool {
    name       = "default"
    node_count = 1
    vm_size    = "Standard_D2_v2"
  }

  identity {
    type = "SystemAssigned"
  }

  tags = {
    Environment = "Production"
  }
}

output "client_certificate" {
  value     = azurerm_kubernetes_cluster.example.kube_config.0.client_certificate
  sensitive = true
}

output "kube_config" {
  value = azurerm_kubernetes_cluster.example.kube_config_raw

  sensitive = true
}

network tf file as follows:

resource "azurerm_resource_group" "example" {
  name     = "***********"
  location = "East Us"
}
resource "azurerm_public_ip" "example" {
  name                = "swarnapIPForLB"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  allocation_method   = "Static"
}

resource "azurerm_lb" "example" {
  name                = "swarnaLB"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

  frontend_ip_configuration {
    name                 = "PublicIPadd"
    public_ip_address_id = azurerm_public_ip.example.id
  }
}

from the code enter image description here

Upon running of plan and apply

enter image description here

enter image description here

enter image description here

Upvotes: 1

Related Questions