How to set consistent naming in cloud with Azure Naming Tool and Terraform
Consistent naming across cloud resources is crucial for maintainability, clarity, and compliance. The Azure Naming Tool is a powerful tool that helps you define and enforce naming conventions for Azure resources. In this post, we will explore how to use the Azure Naming Tool in conjunction with Terraform to ensure consistent naming across your cloud infrastructure.
What is the Azure Naming Tool?
The Azure Naming Tool is an open-source project that provides a framework for defining and enforcing naming conventions for Azure resources. It allows you to create a set of rules that can be applied to various resource types, ensuring that all resources follow a consistent naming pattern. This is particularly useful in large organizations where multiple teams may be creating resources independently.
Really, the Azure Naming Tool is not only for Azure, it can be used for any cloud provider, as it is a generic tool that can be adapted to different environments if you define the rules correctly. You have the option Resource Type Editing documentation
that allows you to define the rules for each resource type or create your own custom resource types, for AWS, GCP, or any other cloud provider.
Why Use Consistent Naming?
Consistent naming is essential for several reasons:
- Clarity: Clear and consistent names make it easier to understand the purpose of each resource.
- Maintainability: When resources are named consistently, it simplifies management and reduces the risk of errors.
- Compliance: Many organizations have compliance requirements that mandate specific naming conventions for resources.
- Automation: Consistent naming allows for easier automation and scripting, as you can predict resource names based on their types and roles.
What is the problem?
The problem with naming is the interfaces that we use to create resources, Azure Naming Tool portal is a great tool to define the rules, but it is not integrated with Terraform, so we need another way to apply the rules defined in the Azure Naming Tool to our Terraform code.
Solution
To solve the problem of integrating the Azure Naming Tool with Terraform, I created a Terraform module that allows you to use the naming rules defined in the Azure Naming Tool directly in your Terraform code. This module post a request to the Azure Naming Tool API to get the name of the resource based on the rules defined in the Azure Naming Tool.
This example shows how to use the module to create a resource group with a name generated by the Azure Naming Tool based on the rules defined in the Azure Naming Tool portal. The module uses the data.external
and data.http
resources to call the Azure Naming Tool API and get the name of the resource.
terraform apply -auto-approve
module.azurenamingtool.data.external.aad_token: Reading...
module.azurenamingtool.data.external.aad_token: Read complete after 0s [id=-]
module.azurenamingtool.data.http.name_generation_post: Reading...
module.azurenamingtool.data.http.name_generation_post: Read complete after 1s [id=https://azurenamingtool.azurewebsites.net/api/ResourceNamingRequests/RequestName]
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# azurerm_resource_group.example will be created
+ resource "azurerm_resource_group" "example" {
+ id = (known after apply)
+ location = "westeurope"
+ name = "rg-spa-dev-auc-025"
+ tags = {
+ "environment" = "example"
+ "project" = "azurenamingtool"
}
}
Plan: 1 to add, 0 to change, 0 to destroy.
Changes to Outputs:
+ generated_name = "rg-spa-dev-auc-025"
azurerm_resource_group.example: Creating...
azurerm_resource_group.example: Still creating... [00m10s elapsed]
azurerm_resource_group.example: Creation complete after 16s [id=/subscriptions/000000-0000-0000-0000-000000000000/resourceGroups/rg-spa-dev-auc-025]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Outputs:
generated_name = "rg-spa-dev-auc-025"
You can check the module in the Terraform Registry.
Conclusion
Using the Azure Naming Tool in conjunction with Terraform allows you to enforce consistent naming conventions across your cloud resources. By integrating the Azure Naming Tool API into your Terraform workflows, you can automate the naming process and ensure that all resources follow the defined rules. This not only improves clarity and maintainability but also helps meet compliance requirements.
Enjoy the benefits of consistent naming in your cloud infrastructure with the Azure Naming Tool and Terraform!