terraform-azurerm-naming

repository·master·Indexed 42 days ago

https://github.com/azure/terraform-azurerm-naming

An archived Terraform module for providing consistent, standardized naming for Azure resources. It provides outputs for various resource types including resource groups, storage accounts, and virtual networks, with support for unique name generation. Users are advised to migrate to the Azure Verified Naming Utility (AVM UTL).

Tokens
6.6K
Snippets
5
Records
11
Agent score
53%

What's inside terraform-azurerm-naming

  1. Migrate to Azure Verified Naming Utility

    master

    IMPORTANT: This module is archived and will no longer receive updates.

    You should migrate to the Azure Verified Naming Utility (AVM UTL).

    Migration Tip: To maintain the original naming behavior and legacy named outputs during your migration, set legacy_mode = true in the new module. By default, the new module uses JSON-driven naming rules which may produce different names than this archived module.

  2. Use the Azure Naming module for resource names

    master

    This module provides consistent naming for Azure resources. To use it, initialize the module and then reference the .name property of the specific resource output in your azurerm resources.

    Note: When referencing the module for azurerm resources, you use the resource type name without the azurerm prefix (e.g., resource_group instead of azurerm_resource_group).

    Basic Usage

    module "naming" {
      source  = "Azure/naming/azurerm"
      suffix = [ "test" ]
    }
    
    resource "azurerm_resource_group" "example" {
      name     = module.naming.resource_group.name
      location = "West Europe"
    }

    Ensuring Uniqueness

    If you need a unique name (e.g., to avoid collisions), use the .name_unique property instead of .name. This appends random characters to the generated name.

    resource "azurerm_resource_group" "example" {
      name     = module.naming.resource_group.name_unique
      location = "West Europe"
    }
    module "naming" {
      source  = "Azure/naming/azurerm"
      suffix = [ "test" ]
    }
    
    resource "azurerm_resource_group" "example" {
      name     = module.naming.resource_group.name
      location = "West Europe"
    }
  3. Workaround for CDKTF module output type errors

    master

    When using this module with Cloud Development Kit for Terraform (CDKTF), autogenerated bindings may incorrectly type module outputs as string instead of map.

    For example, calling a method like ResourceGroupOutput() might yield a reference like ${module.resourceNaming.resource_group}. Because the underlying value is actually a map, running tf plan will fail with an error stating the output is an object with multiple attributes (e.g., module.resourceNaming.resource_group is object with 8 attributes).

    To resolve this, you must explicitly access the specific key within the map (such as .name) using cdktf.Fn_Lookup to ensure the synthesized cdktf.json contains the correct path (e.g., ${module.resourceNaming.resource_group.name}).

  4. Configure the Azure Naming module inputs

    master

    The following inputs can be used to customize the generated names:

    NameDescriptionTypeDefaultRequired
    prefixIt is not recommended that you use prefix by azure you should be using a suffix for your resources.list(string)[]no
    suffixIt is recommended that you specify a suffix for consistency. please use only lowercase characters when possiblelist(string)[]no
    unique-include-numbersIf you want to include numbers in the unique generationbooltrueno
    unique-lengthMax length of the uniqueness suffix to be addednumber4no
    unique-seedCustom value for the random characters to be usedstring""no
  5. Use cdktf.Fn_Lookup to access module output map keys

    master

    To fix the type mismatch in CDKTF, wrap the module output in cdktf.Fn_Lookup. This function retrieves a single element from a map given its key. You should also wrap the result in cdktf.Token_AsString to ensure the final synthesized value is treated as a string.

    Pattern: cdktf.Token_AsString(cdktf.Fn_Lookup(moduleOutput, jsii.String("key_name"), nil), nil)

    // Correct way to use ResourceGroupOutput in CDKTF to avoid type errors
    resourcegroup.NewResourceGroup(stack, jsii.String("resource_group_workaround"), &resourcegroup.ResourceGroupConfig{
        Name:     cdktf.Token_AsString(cdktf.Fn_Lookup(n.ResourceGroupOutput(), jsii.String("name"), nil), nil),
        Location: jsii.String("Canada Central"),
    })
  6. Reference Azure resource name outputs

    master

    The terraform-azurerm-naming module provides standardized name outputs for a wide variety of Azure resources. When using a naming module for a specific resource type, you can access the generated name via the corresponding output attribute.

    Commonly supported resource name outputs include:

    • Virtual Desktop: virtual_desktop_application_group, virtual_desktop_host_pool, virtual_desktop_scaling_plan, virtual_desktop_workspace
    • Virtual Machine: virtual_machine, virtual_machine_extension, virtual_machine_restore_point_collection, virtual_machine_scale_set, virtual_machine_scale_set_extension, windows_virtual_machine, windows_virtual_machine_scale_set
    • Networking: virtual_network, virtual_network_gateway, virtual_network_gateway_connection, virtual_network_peering, virtual_wan, vpn_gateway, vpn_gateway_connection, vpn_site
    • Security & Web: web_application_firewall_policy, web_application_firewall_policy_rule_group, static_web_app
    • Other: virtual_hub
    | Resource Type | Output Name |
    | :--- | :--- |
    | Virtual Desktop Application Group | `virtual_desktop_application_group` |
    | Virtual Desktop Host Pool | `virtual_desktop_host_pool` |
    | Virtual Desktop Scaling Plan | `virtual_desktop_scaling_plan` |
    | Virtual Desktop Workspace | `virtual_desktop_workspace` |
    | Virtual Hub | `virtual_hub` |
    | Virtual Machine | `virtual_machine` |
    | Virtual Machine Extension | `virtual_machine_extension` |
    | Virtual Machine Restore Point Collection | `virtual_machine_restore_point_collection` |
    | Virtual Machine Scale Set | `virtual_machine_scale_set` |
    | Virtual Machine Scale Set Extension | `virtual_machine_scale_set_extension` |
    | Virtual Network | `virtual_network` |
    | Virtual Network Gateway | `virtual_network_gateway` |
    | Virtual Network Gateway Connection | `virtual_network_gateway_connection` |
    | Virtual Network Peering | `virtual_network_peering` |
    | Virtual Wan | `virtual_wan` |
    | Vpn Gateway | `vpn_gateway` |
    | Vpn Gateway Connection | `vpn_gateway_connection` |
    | Vpn Site | `vpn_site` |
    | Web Application Firewall Policy | `web_application_firewall_policy` |
    | Web Application Firewall Policy Rule Group | `web_application_firewall_policy_rule_group` |
    | Windows Virtual Machine | `windows_virtual_machine` |
    | Windows Virtual Machine Scale Set | `windows_virtual_machine_scale_set` |
    | Static Web App | `static_web_app` |
  7. List of currently unsupported Azure resources

    master

    The terraform-azurerm-naming module does not currently provide naming logic for the following Azure resources. If you need to name these resources, you must define the naming convention manually in your Terraform configuration:

    • Managed Applications: managed_application_definition, managed_application
    • Compute & Images: shared_image_version, orchestrated_virtual_machine_scale_set (preview)
    • SQL & IoT: sql_virtual_network_rule, iot_time_series_insights_access_policy, iot_time_series_insights_standard_environment, iot_time_series_insights_reference_data_set
    • Log Analytics: log_analytics_datasource_windows_event, log_analytics_datasource_windows_performance_counter, log_analytics_linked_service, log_analytics_solution
    • Logic Apps: logic_app_action_custom, logic_app_action_http, logic_app_integration_account, logic_app_trigger_custom, logic_app_trigger_http_request, logic_app_trigger_recurrence, logic_app_workflow
    • Management & Governance: maintenance_configuration, management_lock, management_group
    • Networking & Security: express_route_circuit_authorization, network_packet_capture, network_profile, packet_capture (deprecated), web_application_firewall_policy
    • Storage & NetApp: netapp_account, netapp_pool, netapp_volume, netapp_snapshot
    • Site Recovery & Recovery Services: recovery_services_vault, site_recovery_fabric, site_recovery_network_mapping, site_recovery_protection_container_mapping, site_recovery_protection_container, site_recovery_replicated_vm, site_recovery_replication_policy
    • Media & Spatial: media_services_account, spatial_anchors_account
    • HPC Cache: hpc_cache, hpc_cache_nfs_target, hpc_cache_blob_target
    • Other: scheduled_query_rule_log
    - managed_application_definition
    - managed_application
    - shared_image_version
    - orchestrated_virtual_machine_scale_set / preview
    - sql_virtual_network_rule
    - iot_time_series_insights_access_policy
    - iot_time_series_insights_standard_environment
    - iot_time_series_insights_reference_data_set
    - log_analytics_datasource_windows_event
    - log_analytics_datasource_windows_performance_counter
    - log_analytics_linked_service
    - log_analytics_solution
    - logic_app_action_custom
    - logic_app_action_http
    - logic_app_integration_account
    - logic_app_trigger_custom
    - logic_app_trigger_http_request
    - logic_app_trigger_recurrence
    - logic_app_workflow
    - maintenance_configuration
    - management_lock
    - management_group
    - media_services_account
    - spatial_anchors_account
    - scheduled_query_rule_log
    - express_route_circuit_authorization
    - network_packet_capture
    - network_profile
    - packet_capture //deprecated
    - web_application_firewall_policy
    - netapp_account
    - netapp_pool
    - netapp_volume
    - netapp_snapshot
    - recovery_services_vault
    - site_recovery_fabric
    - site_recovery_network_mapping
    - site_recovery_protection_container_mapping
    - site_recovery_protection_container
    - site_recovery_replicated_vm
    - site_recovery_replication_policy
    - hpc_cache
    - hpc_cache_nfs_target
    - hpc_cache_blob_target
  8. Reference the module outputs for Azure resources

    master

    The terraform-azurerm-naming module provides a wide range of outputs that correspond to various Azure resource types. These outputs allow you to retrieve the generated names for use in other parts of your Terraform configuration.

    Commonly used resource outputs include:

    • resource_group: The generated name for a Resource Group.
    • storage_account: The generated name for a Storage Account.
    • sql_server: The generated name for a SQL Server.
    • subnet: The generated name for a Subnet.
    • virtual_network: The generated name for a Virtual Network.
    • public_ip: The generated name for a Public IP.

    For a complete list of supported resource outputs, refer to the module's documentation or the specific output definitions in the source code.

  9. Reference the available Terraform module outputs

    master

    The terraform-azurerm-naming module provides a wide range of outputs for various Azure resources. These outputs allow you to retrieve the generated names for use in other parts of your Terraform configuration.

    Commonly used resource outputs include:

    • Event Grid: eventgrid_domain, eventgrid_namespace, eventgrid_topic, etc.
    • Event Hub: eventhub, eventhub_namespace, eventhub_consumer_group, etc.
    • Firewall: firewall, firewall_policy, firewall_ip_configuration, etc.
    • Key Vault: key_vault, key_vault_secret, key_vault_key, etc.
    • Networking: network_security_group, network_interface, nat_gateway, lb, etc.
    • Storage/Databases: mssql_server, mysql_server, mariadb_server, managed_disk, etc.

    Refer to the specific output name in the module documentation to access the generated string for your resource.

    | Output Name | Resource Description |
    | :--- | :--- |
    | `eventgrid_domain` | Eventgrid Domain |
    | `eventgrid_domain_topic` | Eventgrid Domain Topic |
    | `eventgrid_namespace` | Eventgrid Namespace |
    | `eventgrid_event_subscription` | Eventgrid Event Subscription |
    | `eventgrid_system_topic` | Eventgrid System Topic |
    | `eventgrid_topic` | Eventgrid Topic |
    | `eventhub` | Eventhub |
    | `eventhub_authorization_rule` | Eventhub Authorization Rule |
    | `eventhub_cluster` | Eventhub Cluster |
    | `eventhub_consumer_group` | Eventhub Consumer Group |
    | `eventhub_namespace` | Eventhub Namespace |
    | `eventhub_namespace_authorization_rule` | Eventhub Namespace Authorization Rule |
    | `eventhub_namespace_disaster_recovery_config` | Eventhub Namespace Disaster Recovery Config |
    | `express_route_circuit` | Express Route Circuit |
    | `express_route_gateway` | Express Route Gateway |
    | `fabric_capacity` | Fabric Capacity |
    | `firewall` | Firewall |
    | `firewall_application_rule_collection` | Firewall Application Rule Collection |
    | `firewall_ip_configuration` | Firewall Ip Configuration |
    | `firewall_nat_rule_collection` | Firewall Nat Rule Collection |
    | `firewall_network_rule_collection` | Firewall Network Rule Collection |
    | `firewall_policy` | Firewall Policy |
    | `firewall_policy_rule_collection_group` | Firewall Policy Rule Collection Group |
    | `frontdoor` | Frontdoor |
    | `frontdoor_firewall_policy` | Frontdoor Firewall Policy |
    | `function_app` | Function App |
    | `hdinsight_hadoop_cluster` | Hdinsight Hadoop Cluster |
    | `hdinsight_hbase_cluster` | Hdinsight Hbase Cluster |
    | `hdinsight_interactive_query_cluster` | Hdinsight Interactive Query Cluster |
    | `hdinsight_kafka_cluster` | Hdinsight Kafka Cluster |
    | `hdinsight_ml_services_cluster` | Hdinsight Ml Services Cluster |
    | `hdinsight_rserver_cluster` | Hdinsight Rserver Cluster |
    | `hdinsight_spark_cluster` | Hdinsight Spark Cluster |
    | `hdinsight_storm_cluster` | Hdinsight Storm Cluster |
    | `image` | Image |
    | `iotcentral_application` | Iotcentral Application |
    | `iothub` | Iothub |
    | `iothub_consumer_group` | Iothub Consumer Group |
    | `iothub_dps` | Iothub Dps |
    | `iothub_dps_certificate` | Iothub Dps Certificate |
    | `ip_group` | Ip Group |
    | `key_vault` | Key Vault |
    | `key_vault_certificate` | Key Vault Certificate |
    | `key_vault_key` | Key Vault Key |
    | `key_vault_secret` | Key Vault Secret |
    | `kubernetes_cluster` | Kubernetes Cluster |
    | `kusto_cluster` | Kusto Cluster |
    | `kusto_database` | Kusto Database |
    | `kusto_eventhub_data_connection` | Kusto Eventhub Data Connection |
    | `lb` | Lb |
    | `lb_nat_rule` | Lb Nat Rule |
    | `lb_rule` | Lb Rule |
    | `linux_virtual_machine` | Linux Virtual Machine |
    | `linux_virtual_machine_scale_set` | Linux Virtual Machine Scale Set |
    | `local_network_gateway` | Local Network Gateway |
    | `log_anlaytics_query_pack` | Log Analytics Query Pack |
    | `log_analytics_workspace` | Log Analytics Workspace |
    | `logic_app_integration_account` | Logic App Integration Account |
    | `logic_app_workflow` | Logic App Workflow |
    | `machine_learning_workspace` | Machine Learning Workspace |
    | `machine_learning_registry` | Machine Learning Registry |
    | `managed_disk` | Managed Disk |
    | `management_group` | Management Group |
    | `maps_account` | Maps Account |
    | `maintentance_configuration` | Maintenance Configuration |
    | `mariadb_database` | Mariadb Database |
    | `mariadb_firewall_rule` | Mariadb Firewall Rule |
    | `mariadb_server` | Mariadb Server |
    | `mariadb_virtual_network_rule` | Mariadb Virtual Network Rule |
    | `monitor_action_group` | Monitor Action Group |
    | `monitor_alert_processing_rule_action_group` | Monitor Alert Processing Rule Action Group |
    | `monitor_autoscale_setting` | Monitor Autoscale Setting |
    | `monitor_data_collection_endpoint` | Monitor Data Collection Endpoint |
    | `monitor_data_collection_rule` | Monitor Data Collection Rule |
    | `monitor_diagnostic_setting` | Monitor Diagnostic Setting |
    | `monitor_scheduled_query_rules_alert` | Monitor Scheduled Query Rules Alert |
    | `mssql_database` | Mssql Database |
    | `mssql_elasticpool` | Mssql Elasticpool |
    | `mssql_job_agent` | Mssql Job Agent |
    | `mssql_managed_instance` | Mssql Server Managed Instance |
    | `mssql_server` | Mssql Server |
    | `mysql_database` | Mysql Database |
    | `mysql_firewall_rule` | Mysql Firewall Rule |
    | `mysql_server` | Mysql Server |
    | `mysql_virtual_network_rule` | Mysql Virtual Network Rule |
    | `nat_gateway` | NAT Gateway |
    | `network_ddos_protection_plan` | Network Ddos Protection Plan |
    | `network_interface` | Network Interface |
    | `network_manager` | Network Manager |
    | `network_security_group` | Network Security Group |
    | `network_security_group_rule` | Network Security Group Rule |
    | `network_security_rule` | Network Security Rule |
  10. Understand resource naming outputs

    master

    Each resource output in this module provides a set of properties describing the generated name and its constraints:

    PropertyTypeDescription
    namestringname of the resource including respective suffixes and prefixes applied
    name_uniquestringsame as the name but with random chars added for uniqueness
    dashesboolif these resources support dashes
    slugstringletters to identify this resource among others
    min_lengthintegerMinimum length required for this resource name
    max_lengthintegerMaximum length allowed for this resource name
    scopestringscope which this name needs to be unique, such as resourcegroup or global
    regexstringTerraform compatible version of the regex

    Example Output Object

    postgresql_server = {
          name        = "pre-fix-psql-su-fix"
          name_unique = "pre-fix-psql-su-fix-asdfg"
          dashes      = true
          slug        = "psql"
          min_length  = 3
          max_length  = 63
          scope       = "global"
          regex       = "^[a-z0-9][a-zA-Z0-9-]+[a-z0-9]$"
        }
  11. Identify Azure resources with undefined naming limits

    master

    The following Azure resource types do not have predefined naming rules within this module. When using these resources, you must manually manage their names as the module cannot automatically generate them according to specific Azure constraints or patterns.

    Affected Resources:

    • Cosmos DB: cosmosdb_cassandra_keyspace, cosmosdb_gremlin_database, cosmosdb_gremlin_graph, cosmosdb_mongo_collection, cosmosdb_mongo_database, cosmosdb_sql_container, cosmosdb_sql_database, cosmosdb_table
    • IoT Hub: iothub_dps_shared_access_policy, iothub_endpoint_eventhub, iothub_endpoint_servicebus_queue, iothub_endpoint_servicebus_topic, iothub_endpoint_storage_container, iothub_route, iothub_shared_access_policy
    • Load Balancer: lb_backend_address_pool, lb_rule, lb_outbound_rule, lb_nat_pool, lb_probe
    • Monitor/Alerting: monitor_action_group, monitor_action_rule_action_group, monitor_action_rule_suppression, monitor_activity_log_alert, monitor_autoscale_setting, monitor_diagnostic_setting, monitor_log_profile, monitor_metric_alert, monitor_scheduled_query_rules_alert
    • Policy: policy_assignment, policy_definition, policy_remediation, policy_set_definition
    • Sentinel: sentinel_alert_rule_ms_security_incident, sentinel_alert_rule_scheduled
    • Other: kubernetes_cluster_node_pool, cost_management_export_resource_group, mariadb_configuration, mysql_configuration, postgresql_configuration, data_share, data_share_account, devspace_controller, dev_test_policy, dev_test_schedule, dev_test_virtual_network, healthcare_service, traffic_manager_endpoint, virtual_hub, virtual_hub_connection, virtual_network_gateway_connection, vpn_server_configuration, storage_table_entity, search_service