Terraform 提供程序,用于 Azure 的 Terraform 平台工程
The azurecaf provider implements a set of methodologies for naming convention implementation, including the default Microsoft Cloud Adoption Framework for Azure recommendations as per the Azure naming and tagging best practices. Please note this project is not part of Azure Cloud Adoption Framework and is supported by the community, not by Microsoft Support.
The Azure CAF provider allows you to:
Add the provider to your Terraform configuration:
terraform {
required_providers {
azurecaf = {
source = "aztfmod/azurecaf"
version = "~> 1.2.32" # Use the latest version
}
}
}Generate an Azure-compliant resource group name:
# Using data source (recommended - evaluated at plan time)
data "azurecaf_name" "rg_example" {
name = "myproject"
resource_type = "azurerm_resource_group"
prefixes = ["demo"]
suffixes = ["001"]
random_length = 5
clean_input = true
}
resource "azurerm_resource_group" "example" {
name = data.azurecaf_name.rg_example.result
location = "East US"
}
output "resource_group_name" {
value = data.azurecaf_name.rg_example.result
# Output: rg-demo-myproject-001-a1b2c
}Generate names with automatic resource type prefixes:
data "azurecaf_name" "storage_account" {
name = "mydata"
resource_type = "azurerm_storage_account"
random_length = 3
}
resource "azurerm_storage_account" "example" {
name = data.azurecaf_name.storage_account.result
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
account_tier = "Standard"
account_replication_type = "LRS"
}
# Result: "stmydata123" (st = storage account slug, mydata = name, 123 = random)Generate names for multiple related resources:
…For cases where you need to generate multiple resource names:
resource "azurecaf_name" "multiple_resources" {
name = "myapp"
resource_type = "azurerm_app_service"
resource_types = [
"azurerm_app_service_plan",
"azurerm_application_insights"
]
prefixes = ["prod"]
suffixes = ["web"]
random_length = 3
clean_input = true
}
# Access individual names:
# Primary: azurecaf_name.multiple_resources.result
# All: azurecaf_name.multiple_resources.resultsValidate existing resource names without modification:
data "azurecaf_name" "existing_storage" {
name = "mystorageaccount123"
resource_type = "azurerm_storage_account"
passthrough = true
}
# Validates that "mystorageaccount123" is compliant with storage account naming rulesComplex naming with custom separators and no resource slugs:
data "azurecaf_name" "custom_vm" {
name = "database-server"
resource_type = "azurerm_linux_virtual_machine"
prefixes = ["corp", "prod"]
suffixes = ["db", "001"]
separator = "_"
use_slug = false
random_length = 4
clean_input = true
}
# Result: "corp_prod_database_server_db_001_a1b2"| Parameter | Type | Description | Default |
|---|---|---|---|
name |
string | Base name for the resource | "" |
resource_type |
string | Azure resource type (e.g., azurerm_storage_account) |
Required |
resource_types |
list(string) | Additional resource types for multi-resource naming | [] |
prefixes |
list(string) | List of prefixes to prepend | [] |
suffixes |
list(string) | List of suffixes to append | [] |
random_length |
number | Number of random characters to add | 0 |
random_seed |
number | Seed for random generation (0 = time-based) | 0 |
separator |
string | Character to separate name components | "-" |
clean_input |
bool | Remove non-compliant characters from inputs | true |
passthrough |
bool | Validate without modification | false |
use_slug |
bool | Include resource type abbreviation | true |
error_when_exceeding_max_length |
bool | Fail when generated name exceeds the resource's max length | false |
| Attribute | Description |
|---|---|
id |
Unique identifier for the naming configuration |
result |
Generated name for the primary resource type |
results |
Map of all generated names (when using resource_types) |
This provider supports 405 Azure resource types with accurate naming validation rules. Each resource type has specific constraints for:
| Resource Type | Slug | Min Length | Max Length | Example Generated Name |
|---|---|---|---|---|
azurerm_resource_group |
rg |
1 | 90 | rg-prod-myapp-001 |
azurerm_storage_account |
st |
3 | 24 | stprodmyapp001 |
azurerm_key_vault |
kv |
3 | 24 | kv-prod-myapp-001 |
azurerm_app_service |
app |
2 | 60 | app-prod-myapp-001 |
azurerm_kubernetes_cluster |
aks |
1 | 63 | aks-prod-myapp-001 |
azurerm_virtual_machine |
vm |
1 | 15 | vm-prod-001 |
azurerm_sql_server |
sql |
1 | 63 | sql-prod-myapp-001 |
View Full Resource Type List
For the complete list of 405 supported resource types, see the Resource Types Documentation.
locals {
environment_config = {
dev = {
prefix = "dev"
random_length = 3
}
prod = {
prefix = "prod"
random_length = 5
}
}
current_env = local.environment_config[var.environment]
}
data "azurecaf_name" "app_service" {
name = var.application_name
resource_type = "azurerm_app_service"
prefixes = [local.current_env.prefix]
random_length = local.current_env.random_length
}# In your module
variable "project_name" {
description = "Name of the project"
type = string
}
variable "environment" {
description = "Environment (dev, test, prod)"
type = string
}
data "azurecaf_name" "resources" {
for_each = toset([
"azurerm_resource_group",
"azurerm_storage_account",
"azurerm_key_vault"
])
name = var.project_name
resource_type = each.key
prefixes = [var.environment]
random_length = 3
}
output "resource_names" {
value = { for k, v in data.azurecaf_name.resources : k => v.result }
}Issue: Names too long for resource type
# Problem: Generated name exceeds Azure limits
data "azurecaf_name" "long_name" {
name = "very-long-application-name-that-exceeds-limits"
resource_type = "azurerm_storage_account" # max 24 chars
}
# Solution: Use shorter base name or disable slug
data "azurecaf_name" "fixed_name" {
name = "shortname"
resource_type = "azurerm_storage_account"
use_slug = false # Removes 'st' prefix
}Issue: Invalid characters in resource names
# Problem: Special characters not allowed
data "azurecaf_name" "invalid_chars" {
name = "[email protected]"
resource_type = "azurerm_app_service"
clean_input = false # Keeps invalid chars
}
# Solution: Enable input cleaning (default)
data "azurecaf_name" "clean_name" {
name = "[email protected]"
resource_type = "azurerm_app_service"
clean_input = true # Removes invalid chars
}The provider validates names against Azure requirements and will show specific error messages:
Error: Invalid resource name "my--invalid--name"
│ The generated name contains consecutive separators, which is not allowed for azurerm_storage_accountThe Azure CAF terraform provider includes comprehensive testing to ensure reliability and correctness.
To run the standard unit tests:
go test ./azurecaf/...To run tests with coverage information:
go test -cover ./azurecaf/...For a detailed coverage report:
go test -coverprofile=coverage.out ./azurecaf/...
go tool cover -html=coverage.out -o coverage.htmlIntegration tests verify the provider works correctly with Terraform itself. These tests require setting the TF_ACC environment variable:
# Run all integration tests
TF_ACC=1 go test -v ./azurecaf/... -run="TestAcc"
# Run specific integration tests
TF_ACC=1 go test -v ./azurecaf/... -run="TestAccDataSourcesIntegration"
TF_ACC=1 go test -v ./azurecaf/... -run="TestAccErrorHandling"Note: Integration tests take longer to run than unit tests.
The project maintains high test coverage (99.0% of statements) to ensure code quality and reliability. Tests are designed to cover:
The test files are organized as follows:
resource_name_test.goenhanced_tests_test.goTestAcc that interact with Terraform directlyThe provider includes a comprehensive testing framework that validates all 405 Azure resource types:
# Run comprehensive tests for all resource types
make test_all_resources
# Run resource matrix validation (category-based testing and constraint validation)
make test_resource_matrix
# Test resource definition completeness
make test_resource_definitionsThis framework ensures:
When writing new tests, consider the following guidelines:
暂无开放 Issues,或尚未同步最近议题。