This document provides shared guidelines for AI agents working on OpenTofu modules to ensure consistency, clarity, and alignment with best practices.
When working on OpenTofu modules, AI agents should optimize modules, outputs, variables, and file structures according to the specifications outlined in this document. The goal is to maintain consistency across all OpenTofu configurations while following established best practices.
- Consistency: Maintain uniform naming conventions and structures across all modules
- Clarity: Ensure code is readable and well-documented
- Best Practices: Follow OpenTofu and Terraform community standards
- Retrocompatibility: Preserve backward compatibility when making changes
Replace module and resource output names consistently:
- Use
idinstead ofapp_service_id - Use
nameinstead ofapp_service_name
Add a global resource output for the main resource in every module:
output "resource" {
value = [RESOURCE_TYPE].main
}Ensure outputs are added for all resources and modules referred within each module:
output "module_service_plan" {
value = module.service_plan
}
output "resource_application_insights" {
value = azurerm_application_insights.main
}Rename the main resource of each module to "main" for consistent reference:
resource "azurerm_service_plan" "main" {}Add the moved block for retrocompatibility:
moved {
from = azurerm_service_plan.old_resource_name
to = azurerm_service_plan.main
}- Update variable descriptions to include clear punctuation (add dots at the end of sentences)
- Use Markdown links consistently within descriptions
[text](http://example_url) - Update variable naming for clarity and consistency (e.g., change
custom_diagnostic_settings_nametodiagnostic_settings_custom_name) - Ensure
lookup()includes a default value:lookup("parameter_name", var.parameter, "default_value")
- Avoid using
lookupfor typed variables
Use standardized naming conventions for files:
- Data sources:
d-naming.tffor specific naming datasources anddata-sources.tffor grouped datasources - Resources: For example,
r-app-service.tffor specific resources likeazurerm_app_service - Module calls:
m-logs.tffor example for logs diagnostic settings module,m-prefix for others module calls - Split providers constrains into a dedicated
providers.tffile - Split inputs in
variables-xx.tffiles and outputs inoutputs-xx.tffiles, wherexxis a specific category (e.g.,variables-logs.tf,outputs-resources.tf)
- Add a table for OpenTofu versions, including version 8.x.x
- Include a warning about modules not being verified for Terraform versions >= 1.3
- Add notes to indicate optimization for OpenTofu versions >= 1.8
- Light rework of examples:
- Provide examples in
base.tf(used for initialization/validation/plan) but exclude detailed examples from README
- Provide examples in
- Align README content with updated module names, outputs, and conventions
- Use the
terraform-docstool for generating documentation and ensure it is up-to-date with the latest module structure and outputs
Ensure IDs in keys are avoided; use fixed strings or clearly defined keys.
Use count for conditional/boolean operations:
resource "my_resource" {
count = var.resource_enabled ? 1 : 0
...
}Avoid the use of generated values (like resource's id) in count or for_each. Replace with an object-wrapping approach:
variable "resource" { type = object({ id = string }) }
resource "my_other_resource" {
count = var.resource == null ? 1 : 0
attr_id = var.resource.id
}- Enforce Terraform version constraints >= 1.3 in
versions.tf - Require OpenTofu >= 1.8 in CI/CD configurations
- Update both
.gitlab-ci.ymlandproviders.tfto reflect AzureRM provider version constraints
- Fork and use Claranet's "azurecaf naming" provider for naming conventions
- Avoid "unknown values" comparison issues using non-null wrappers for input-generated values
Do not use:
variable "resource_id" {}
resource "my_other_resource" {
count = var.resource_id == null ? 1 : 0
...
}Instead, wrap into objects:
variable "resource" { type = object({ id = string }) }
resource "my_other_resource" {
count = var.resource == null ? 1 : 0
attr_id = var.resource.id
}Before (Input):
output "app_service_id" {
value = azurerm_app_service.main.id
}
output "app_service_name" {
value = azurerm_app_service.main.name
}After (Transformed Output):
output "id" {
value = azurerm_app_service.main.id
}
output "name" {
value = azurerm_app_service.main.name
}
output "resource" {
value = azurerm_app_service.main
}
output "module_service_plan" {
value = module.service_plan
}
output "resource_application_insights" {
value = azurerm_application_insights.main
}All AI agents must follow these git contribution standards when working on OpenTofu modules:
- Create a new branch for each contribution
- Use prefixed branch names based on the type of change:
feat/add_new_param- for new features or parametersfix/change_attribute- for bug fixes or correctionsdocs/update_readme- for documentation updatesrefactor/rename_variables- for code refactoringchore/update_dependencies- for maintenance tasks
- Follow conventional commits structure:
type(scope): description - Optional unicode emojis are allowed for better readability
- Examples:
feat(outputs): ✨ add global resource outputfix(variables): 🐛 correct lookup default valuedocs(readme): 📝 update version compatibility tablerefactor(resources): ♻️ rename main resource with moved block
- Install and update tools using mise-en-place:
mise install - Keep tools up-to-date before starting work
- Verify tool versions match project requirements in
.tool-versions
- Install pre-commit hooks:
pre-commit install - Pre-commit must trigger on each commit to ensure validity of changes
- All pre-commit checks must pass before pushing changes
- Address any pre-commit failures immediately
- Open a merge request when changes are ready for review
- Provide clear description of changes and their impact
- Reference related issues or requirements
- Ensure all CI/CD checks pass before requesting review
- Address review feedback promptly and thoroughly
When working on OpenTofu modules, AI agents should:
- Analyze existing code structure and identify areas for improvement
- Apply the standardized naming conventions and file structures
- Update outputs, variables, and documentation according to guidelines
- Validate configurations against best practices
- Ensure retrocompatibility through appropriate migration strategies
- Follow git contribution guidelines for all changes
- Document changes and maintain clear commit messages
- Validate all examples and configurations against updated specifications
- Ensure consistency across all documentation files
- Test variable naming conventions and OpenTofu configurations
- Verify that all changes maintain backward compatibility
- Confirm all pre-commit checks pass
- Ensure proper git workflow is followed
- Focus on alignment with best practices for readability
- Avoid introducing unnecessary complexity
- Carefully validate examples against specifications
- Maintain consistency across all files and configurations
- Always include retrocompatibility measures when making breaking changes
- Follow git contribution guidelines for proper version control