Skip to content

Outputs Naming Conventions

Loading...

Naming Conventions


In this example, we will create a naming convention for outputs in a Terraform codebase. We will use the following naming conventions:


Well-structured output

When naming outputs, it is important to use a well-structured name that is descriptive of the output’s purpose. you can simply name it {resource name}_{attribute reference}. This will help you to easily identify the output when it is used in other modules.

  • ✅ db_instance_arn
  • ❌ db_arn
  • ❌ id_db_instance

Avoid Repeating Resource Type

When naming outputs, avoid repeating the resource type in the name.

  • ✅ db_instance_arn
  • ❌ db_instance_db_arn
  • ❌ db_instance_arn_db

Use Descriptive Names

Use descriptive names for outputs to make it clear what the output is used for.

  • ✅ db_instance_arn
  • ✅ db_instance_id
  • ❌ db_instance_arn_id
  • ❌ db_instance_id_arn

Do Not Use this for Outputs

When naming outputs, avoid using this as it can be confusing and make it difficult to understand the purpose of the output.

  • ❌ this
  • ❌ output

Use {resource name} Prefix

When naming outputs, use the {resource name} prefix to make it clear that the variable is an output.

  • ✅ db_instance_arn
  • ✅ vpc_id
  • ❌ arn
  • ❌ id

Description for Outputs

Do not forget to add a description, use the same description at the terraform registry for the output. and make sure to add a type for the output.

  • good-example.tf
    1
    output "db_instance_arn" {
    2
    description = "The ARN of the RDS instance. Type: string"
    3
    value = aws_db_instance.example.arn
    4
    }
  • bad-example.tf
    1
    output "db_arn" {
    2
    description = "The ARN of the RDS instance. Type: string"
    3
    value = aws_db_instance.example.arn
    4
    }
  • bad-example.tf
    1
    output "db_instance_arn" {
    2
    description = "The ARN of the RDS instance"
    3
    value = aws_db_instance.example.arn
    4
    }

Leverage Output Argument Standards

When defining outputs, make sure to follow the same standards as you would for variables and resources. This includes using descriptive names, avoiding repeating resource types, and using well-structured names.

  • good-example.tf
    1
    output "db_instance_arn" {
    2
    description = "The ARN of the RDS instance. Type: string"
    3
    value = aws_db_instance.example.arn
    4
    }
  • bad-example.tf
    1
    output "db_arn" {
    2
    description = "The ARN of the RDS instance. Type: string"
    3
    value = aws_db_instance.example.arn
    4
    }

Use the try, element and concat functions to get the output value


  • good-example.tf
    1
    output "db_instance_arn" {
    2
    description = "The ARN of the RDS instance. Type: string"
    3
    value = try(element(concat(aws_db_instance.example.*.arn, list("")), 0), "")
    4
    }
  • custom-wrapper-good-example.tf
    1
    output "db_instance_custom_wrapper_arn" {
    2
    description = "The ARN of the RDS instance. Type: string"
    3
    value = try(module.example.*.db_instance_arn, "")
    4
    }
  • bad-example.tf
    1
    output "db_instance_arn" {
    2
    description = "The ARN of the RDS instance. Type: string"
    3
    value = aws_db_instance.example[0].arn
    4
    }