Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terraform Naming Convention #482

Open
1 task done
stevewallone opened this issue Dec 20, 2024 · 0 comments
Open
1 task done

Terraform Naming Convention #482

stevewallone opened this issue Dec 20, 2024 · 0 comments
Labels
documentation Documentation
Milestone

Comments

@stevewallone
Copy link

Code of Conduct

  • I have read and agree to the project's Code of Conduct.

Provider Version

latest

Motivation

The examples do not follow the common terraform naming standards. There is use of an imperative style, as opposed to declarative. The specific naming I found unusual was the use of "create_" as the start of a resource name. I asked chatGPT what it thought of that convention.

Question
Reviewing terraform code, I noticed the resources are named starting with "create_". ex. resource "tanzu-mission-control_git_repository" "create_cluster_git_repository" {

Is this a common practice? I would have thought the name would just be "cluster_git_repository"

Answer
Naming conventions in Terraform are critical for readability, maintainability, and collaboration, but there is no strict requirement on how resource names should be structured. Using create_ as a prefix in resource names is not a common practice and might lead to confusion, as Terraform resources typically represent a declarative state rather than an imperative action.

Common Practices in Terraform Resource Naming:
Descriptive and Contextual Names:

Resource names should describe the entity being managed and its context in the infrastructure.
Example:

resource "tanzu-mission-control_git_repository" "cluster_git_repository" {
  # Configuration here
}

Avoiding Action-Oriented Prefixes:

Prefixes like create_ or delete_ imply imperative operations, which can be misleading. Terraform's declarative model means it reconciles the desired state, not directly issuing "create" or "delete" commands.
Using Resource Purpose:

Include the purpose or function in the name for clarity.
Example for multiple repositories:

resource "tanzu-mission-control_git_repository" "prod_git_repository" {
  # Configuration here
}
resource "tanzu-mission-control_git_repository" "dev_git_repository" {
  # Configuration here
}

Provider-Specific Guidance:

Some providers may suggest naming patterns. For example, AWS-related resources often follow names like vpc_main, s3_bucket_logs, etc.
Potential Issues with create_ Prefix:
Redundancy: The action (create) is implicit in Terraform since all resources are created unless explicitly destroyed.
Misleading Semantics: It suggests an imperative process rather than describing the resource's role.
Consistency: May lead to inconsistent naming if not applied across all resources.
Recommended Naming Pattern:
Instead of create_cluster_git_repository, prefer:

resource "tanzu-mission-control_git_repository" "cluster_git_repository" {
  # Configuration
}

Sources and References:
Terraform Documentation on Resources
Best Practices for Naming in Infrastructure as Code
Let me know if you'd like help updating the resource names in your codebase!

Suggestion

Refactor the terraform to use naming best practices.

@stevewallone stevewallone added documentation Documentation needs-review Needs Review labels Dec 20, 2024
@github-actions github-actions bot added the pending-review Pending Review label Dec 20, 2024
@tenthirtyam tenthirtyam removed pending-review Pending Review needs-review Needs Review labels Dec 20, 2024
@tenthirtyam tenthirtyam added this to the Backlog milestone Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Documentation
Projects
None yet
Development

No branches or pull requests

2 participants