hashicorp / terraform-provider-azurerm

Terraform provider for Azure Resource Manager
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
Mozilla Public License 2.0
4.61k stars 4.65k forks source link

azurerm_container_app_environment reference to his managed resource group name #28017

Open Andrei-IVD opened 6 days ago

Andrei-IVD commented 6 days ago

Is there an existing issue for this?

Community Note

Terraform Version

1.10.0

AzureRM Provider Version

4.9.0

Affected Resource(s)/Data Source(s)

azurerm_container_app_environment

Terraform Configuration Files

I am trying to get the name of the managed resource group created by azurerm_container_app_environment. I want to get it without using the property [infrastructure_resource_group_name](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/container_app_environment#infrastructure_resource_group_name-1) because I don't want to use the workload_profile to be able to create a private link service between the internal load balancer (which normally is kubernetes-internal by default the name) with my virtual network for a cdn frontfoor.

resource "azurerm_virtual_network" "this" {
  name                = "vnet-${local.virtual_network_name}-${var.environment}"
  location            = var.location
  resource_group_name = azurerm_resource_group.this.name
  address_space       = ["10.0.0.0/19"]

  tags = {}
}

resource "azurerm_subnet" "container_app" {
  name                 = "snet-${local.subnet_name}-${var.environment}"
  resource_group_name  = azurerm_resource_group.this.name
  virtual_network_name = azurerm_virtual_network.this.name
  address_prefixes     = ["10.0.0.0/21"]
}

resource "azurerm_container_app_environment" "this" {
  name                       = "cae-${local.container_app_env_name}-${var.environment}"
  location                   = var.location
  resource_group_name        = azurerm_resource_group.this.name
  log_analytics_workspace_id = azurerm_log_analytics_workspace.this.id

  infrastructure_subnet_id       = azurerm_subnet.container_app.id
  internal_load_balancer_enabled = true
}
resource "azurerm_container_app" "this" {
  name                         = "ca-${local.container_app_name}-${var.environment}"
  container_app_environment_id = azurerm_container_app_environment.this.id
  resource_group_name          = azurerm_resource_group.this.name
  revision_mode                = "Multiple"

  ingress {
    external_enabled = true
    target_port      = 80
    traffic_weight {
      percentage      = 100
      label           = "my-container-app"
      latest_revision = true
    }
  }

  template {
    container {
      name   = "examplecontainerapp"
      image  = "mcr.microsoft.com/k8se/quickstart:latest"
      cpu    = 0.25
      memory = "0.5Gi"
    }
    min_replicas = 1
    max_replicas = 1
  }
}

resource "azurerm_cdn_frontdoor_profile" "this" {
  name                = "afd-${local.cdn_profile_name}-${var.environment}"
  resource_group_name = azurerm_resource_group.this.name
  sku_name            = "Premium_AzureFrontDoor"
}

resource "azurerm_cdn_frontdoor_origin_group" "this" {
  name                     = "afd-${local.cdn_origin_group_name}-${var.environment}"
  cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.this.id

  load_balancing {
    additional_latency_in_milliseconds = 50
    sample_size                        = 4
    successful_samples_required        = 3
  }

}

resource "azurerm_cdn_frontdoor_origin" "this" {
  name                           = "afd-${local.cdn_origin_name}-${var.environment}"
  cdn_frontdoor_origin_group_id  = azurerm_cdn_frontdoor_origin_group.this.id
  enabled                        = true
  certificate_name_check_enabled = true

  host_name          = azurerm_container_app.this.ingress[0].fqdn
  origin_host_header = azurerm_container_app.this.ingress[0].fqdn
  http_port          = 80
  https_port         = 443
  priority           = 1
  weight             = 1

  //Make sure to approve all the ptivate link services
  //You need to retry if you get an error during the approval
  private_link {
    request_message        = "Add me"
    location               = azurerm_private_link_service.this.location
    private_link_target_id = azurerm_private_link_service.this.id
  }
}

resource "azurerm_private_link_service" "this" {
  name                = "privatelinkcontainerapp"
  resource_group_name = azurerm_resource_group.this.name
  location            = azurerm_resource_group.this.location

  nat_ip_configuration {
    name      = "primary"
    subnet_id = azurerm_subnet.container_app.id
    primary   = true
  }

  load_balancer_frontend_ip_configuration_ids = [data.azurerm_lb.this.frontend_ip_configuration[0].id] //here is the problem
}

resource "azurerm_cdn_frontdoor_endpoint" "this" {
  name                     = "fde-${local.cdn_endpoint_name}-${var.environment}"
  cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.this.id
}

resource "azurerm_cdn_frontdoor_rule_set" "this" {
  name                     = "afd${local.cdn_rule_set_name}${var.environment}"
  cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.this.id
}

resource "azurerm_cdn_frontdoor_route" "this" {
  name                          = "afd-${local.cdn_route_name}-${var.environment}"
  cdn_frontdoor_endpoint_id     = azurerm_cdn_frontdoor_endpoint.this.id
  cdn_frontdoor_origin_group_id = azurerm_cdn_frontdoor_origin_group.this.id
  cdn_frontdoor_origin_ids      = [azurerm_cdn_frontdoor_origin.this.id]
  cdn_frontdoor_rule_set_ids    = [azurerm_cdn_frontdoor_rule_set.this.id]
  enabled                       = true

  patterns_to_match      = ["/*"]
  supported_protocols    = ["Http", "Https"]
  link_to_default_domain = true

  cache {
    query_string_caching_behavior = "IgnoreSpecifiedQueryStrings"
    query_strings                 = ["account", "settings"]
  }
}

When I try to get the load balancer throurg a data point for the origin of the cdn I can't reference any way the managed resource group. `data "azurerm_lb" "this" { name = "kubernetes-internal" resource_group_name = azurerm_container_app_environment.this.infrastructure_resource_group_name

depends_on = [azurerm_container_app_environment.this] }`


### Debug Output/Panic Output

```shell
╷
│ Error: "resource_group_name" cannot be blank
│
│   with module.downloadarea.data.azurerm_lb.this,
│   on ..\..\modules\downloadarea\data.tf line 3, in data "azurerm_lb" "this":
│    3:   resource_group_name = azurerm_container_app_environment.this.infrastructure_resource_group_name
│

Expected Behaviour

Successful deployment

Actual Behaviour

Planning failed. Terraform encountered an error while generating this plan.

╷ │ Error: "resource_group_name" cannot be blank │ │ with module.downloadarea.data.azurerm_lb.this, │ on ....\modules\downloadarea\data.tf line 3, in data "azurerm_lb" "this": │ 3: resource_group_name = azurerm_container_app_environment.this.infrastructure_resource_group_name │ ╵

Steps to Reproduce

After running terraform apply will appear the output.

Important Factoids

No response

References

https://learn.microsoft.com/en-us/azure/templates/microsoft.app/2024-03-01/managedenvironments?pivots=deployment-language-bicep#managedenvironmentproperties