Search code examples
azuremoduleterraformrbacterraform-modules

Terraform modules rbac assignements


It might be a silly error but I don't see it so I am asking for help and clarification. I can't seem to be able to call my values from my outputs.tf for my RBAC which has 2 "dependencies" one from my azurerm folder and one from my azuread folder. I am basically trying to grand an azure RBAC to an azure security group.

Having the following structure:

IaC/
├─ deployments/
├─ modules/
│  ├─ aws/
│  ├─ azuread/
│  │  ├─ security-groups/
│  │  |  ├─ cns/
|  |  |  |  ├─ main.tf
|  |  |  |  ├─ outputs.tf
|  |  |  |  ├─ variables.tf
│  ├─ azurerm/
│  │  ├─ akv/
│  │  │  ├─ main.tf
|  |  |  ├─ outputs.tf
│  │  │  ├─ variables.tf
│  │  ├─ rbac/
│  │  |  ├─ rbac-rg-operator/
│  │  │  |  ├─ main.tf
|  |  |  |  ├─ outputs.tf
│  │  │  |  ├─ variables.tf
│  │  ├─ rg/
│  │  │  ├─ main.tf
|  |  |  ├─ outputs.tf
│  │  │  ├─ variables.tf
├─ project-templates/
│  ├─ azure/
│  │  ├─ project-template-solution-1/
│  │  │  ├─ akv.tf
│  │  │  ├─ main.tf
│  │  │  ├─ rg.tf
│  │  │  ├─ rbac-rg-operator.tf
│  │  │  ├─ sg-cns.tf
│  │  │  ├─ variables.tf
│  │  │  ├─ terragrunt.hcl
├─ terragrunt.hcl

I get the following error message when running terragrunt plan.

error-msg

Here's my concerned configuration files:

IaC/modules/azuread/security-groups/cns/

main.tf

terraform {
  
  required_providers {
    azuread = {
      source  = "hashicorp/azuread"
      version = "2.31.0"
    }
  }
}
provider "azuread" {
  tenant_id = var.azure_tenant_id
}

data "azuread_client_config" "current" {}

#create azure active directory group cns
resource "azuread_group" "azure_sg_cns" {
  display_name     = var.azure_sg_cns
  owners           = [data.azuread_client_config.current.object_id]
  security_enabled = true
}

outputs.tf

output "azure_sg_cns_object_id_out" {
    value = azuread_group.azure_sg_cns.object_id
}

variables.tf

variable "azure_sg_cns" {
  type        = string
  description = "Azure AD Security Group Name CNS"
}

variable "azure_tenant_id" {
  type        = string
  description = "Azure Tenant Id"
}

IaC/modules/azurerm/rbac/rbac-rg-operator/

main.tf

terraform {
  
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "3.42.0"
    }
    azuread = {
      source  = "hashicorp/azuread"
      version = "2.31.0"
    }
  }
}

resource  "azurerm_role_assignment"  "rbac-rg-operator" {
scope =  var.azure_rg_name
role_definition_name =  "RG Operator"
principal_id =  var.azure_sg_cns.object_id
}

variables.tf

variable "azure_sg_cns" {
  type        = string
  description = "Azure AD Security Group Name CNS"
}

variable "azure_rg_name" {
  type        = string
  description = "Azure Resource Group Name"
}

variable "azure_tenant_id" {
  type        = string
  description = "Azure Tenant Id"
}

IaC/modules/project-templates/azure/project-template-solution-1/

rbac-rg-operator.tf

module "rbac-rg-operator" {
    source                          ="../../..//modules/azurerm/rbac/rbac-rg-operator/"
    azure_sg_cns                    = module.azure_sg_cns.azure_sg_cns_object_id_out
    azure_rg_name                   = module.rg.rg_id_out
    azure_tenant_id                 = var.azure_tenant_id
    
}

Solution

  • Changed the following to make it work:

    rbac-rg-operator.tf

    module "rbac-rg-operator" {
        source                          ="../../..//modules/azurerm/rbac/rbac-rg-operator/"
        azure_sg_cns                    = module.azure_sg_cns.azure_sg_cns_object_id_out
        azure_rg_name                   = module.rg.rg_id_out
        azure_tenant_id                 = var.azure_tenant_id
        
    }
    

    to

    module "rbac-rg-operator" {
        source                          ="../../..//modules/azurerm/rbac/rbac-rg-operator/"
        azure_sg_cns                    = module.sg-cns.azure_sg_cns_object_id_out
        azure_rg_name                   = module.rg.rg_id_out
        azure_tenant_id                 = var.azure_tenant_id
        
    }
    

    Line 3: azure_sg_cns = module.azure_sg_cns.azure_sg_cns_object_id_out became azure_sg_cns = module.sg-cns.azure_sg_cns_object_id_out

    apply