0

I need a help with a terraform module that I've created. It works perfectly, but I need to add some automation.

I created a module which creates multiple private endpoints, but I always need to put the variable values in manually.

This is the module:

resource "azurerm_private_endpoint" "endpoint" {
  for_each            = try({ for endpoint in var.endpoints : endpoint.name => endpoint }, toset([]))
  name                = each.key
  location            = var.location
  resource_group_name = var.resource_group_name
  subnet_id           = each.value.subnet_id

  dynamic "private_service_connection" {
    for_each = each.value.private_service_connection
    content {
      name                           = each.key
      private_connection_resource_id = private_service_connection.value.private_connection_resource_id
      is_manual_connection           = false
      subresource_names              = var.subresource_name ### see values on : https://learn.microsoft.com/fr-fr/azure/private-link/private-endpoint-overview#private-link-resource
    }
  }

  lifecycle {
    ignore_changes = [
      private_dns_zone_group
    ]
  }
  tags = var.tags
}

I need to have:

1 - for the private endpoint name : I need it to be automatically provided: "pendp-(the subresource_name value in lower cases- my resource_name =>(mysql server for example))"

2 - for the private connection name: I need the values to be automatically: "connection-(the subresource_name value in lower cases- my ressource_name =>(mysql server for exemple))"

3 - some automation to detect automatically the subresource_name ( if I create a private endpoint for a blob or for a mariadb or for a mysqlserver, the module should detected it.

terraform version:

terraform {
  required_version        = "~> 1"
  required_providers {
    azurerm               = "~> 3.0"
  }
}
Highway of Life
  • 22,803
  • 16
  • 52
  • 80

1 Answers1

0

The easiest way to combine values automatically would be to use the Terraform string join() function to join multiple strings together. For lower case strings, you can use the lower() function.

Some examples:

  name = join("-", ["pandp", lower(var.subresource_name)])
...
  name = join("-", ["connection", lower(var.subresource_name), lower(each.key)])

For your third rule, you want to use a conditional expression to determine if it's a blob, or mariadb, or mysqlserver.

In this example, we set an example_name local with a value some-blob-value if var.subresource_name contains a string that starts with "blob", and set it to something-else if the condition is false:

locals {
  example_name = startswith(lower(var.subresource_name), "blob") ? "some-blob-value" : "something-else"
}

There are many options available for doing a conditional on if a value is passed to what you expect and then determine a result based on that value. What exactly you want isn't clear in the question, but hopefully this will get you pointed in the right direction.

Terraform even has several helper functions that might help you if you only need part of a string, such as startswith(), endswith(), or contains() depending on your needs.

Highway of Life
  • 22,803
  • 16
  • 52
  • 80