Skip to content

Outputs xample

Loading...

Naming Conventions Example


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


good-example.tf
1
output "db_instance_arn" {
2
# ...
3
}

bad-example.tf
1
output "db_instance_arn" {
2
# ...
3
}

Avoid Repeating Resource Type


good-example.tf
1
output "db_instance_arn" {
2
# ...
3
}

bad-example.tf
1
output "db_instance_db_arn" {
2
# ...
3
}

Use Descriptive Names


good-example.tf
1
output "db_instance_arn" {
2
# ...
3
}

good-example.tf
1
output "db_instance_id" {
2
# ...
3
}

bad-example.tf
1
output "db_instance_arn_id" {
2
# ...
3
}

bad-example.tf
1
output "db_instance_id_arn" {
2
# ...
3
}

Do Not Use this for Outputs


bad-example.tf
1
output "this" {
2
# ...
3
}

bad-example.tf
1
output "output" {
2
# ...
3
}

Use {resource name} Prefix


good-example.tf
1
output "db_instance_arn" {
2
# ...
3
}

good-example.tf
1
output "vpc_id" {
2
# ...
3
}

bad-example.tf
1
output "arn" {
2
# ...
3
}

bad-example.tf
1
output "id" {
2
# ...
3
}

Description for Outputs


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

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
}

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
}

Conclusion

In this example, we have demonstrated how to create a naming convention for outputs in a Terraform codebase. By following these conventions, you can ensure that your outputs are well-structured, descriptive, and easy to understand. This will make it easier for you and your team to work with the codebase and maintain consistency across your projects.