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
1output "db_instance_arn" {2 # ...3}1output "db_instance_arn" {2 # ...3}Avoid Repeating Resource Type
1output "db_instance_arn" {2 # ...3}1output "db_instance_db_arn" {2 # ...3}Use Descriptive Names
1output "db_instance_arn" {2 # ...3}1output "db_instance_id" {2 # ...3}1output "db_instance_arn_id" {2 # ...3}1output "db_instance_id_arn" {2 # ...3}Do Not Use this for Outputs
1output "this" {2 # ...3}1output "output" {2 # ...3}Use {resource name} Prefix
1output "db_instance_arn" {2 # ...3}1output "vpc_id" {2 # ...3}1output "arn" {2 # ...3}1output "id" {2 # ...3}Description for Outputs
1output "db_instance_arn" {2 description = "The ARN of the RDS instance. Type: string"3 value = aws_db_instance.example.arn4}1output "db_arn" {2 description = "The ARN of the RDS instance. Type: string"3 value = aws_db_instance.example.arn4}1output "db_instance_arn" {2 description = "The ARN of the RDS instance"3 value = aws_db_instance.example.arn4}Leverage Output Argument Standards
1output "db_instance_arn" {2 description = "The ARN of the RDS instance. Type: string"3 value = aws_db_instance.example.arn4}1output "db_arn" {2 description = "The ARN of the RDS instance. Type: string"3 value = aws_db_instance.example.arn4}1output "db_instance_arn" {2 description = "The ARN of the RDS instance"3 value = aws_db_instance.example.arn4}Use the try, element and concat functions to get the output value
1output "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}1output "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.