Terraform Module Inputs and Outputs: A Complete Guide (with examples)

Updated: February 3, 2024 By: Guest Contributor Post a comment

Introduction

Terraform by HashiCorp is a potent tool for building, changing, and versioning infrastructure efficiently. It uses configuration files to manage and provision the infrastructure. One of its core concepts is the use of modules to manage and encapsulate blocks of resources in reusable components. This guide will cover everything you need to know about Terraform module inputs and outputs, with practical examples to help you understand how to use them effectively in your Terraform configurations.

Understanding Modules

Before diving into the specifics of inputs and outputs, it’s important to understand what modules are in Terraform. A module is a container for multiple resources that are used together. Modules can be used to create lightweight abstractions, so that you can describe your infrastructure in terms of its architecture, rather than directly in terms of physical objects. This abstraction allows for easily reusable and maintainable code.

Module Inputs

Inputs are parameters that allow you to customize modules for different environments or configurations without altering the module’s main code. Defining input variables can make your modules flexible and reusable across different projects and configurations.

Example 1: Basic Input Variable

variable "instance_type" {
  description = "The instance type of the AWS EC2"
  type        = string
  default     = "t2.micro"
}

In this example, we define an input variable for the AWS EC2 instance type. You can then use this input variable in your resource configurations within the module.

Example 2: Advanced Input Variable

variable "backend_config" {
  description = "Configuration map for the backend"
  type        = map(string)
  default     = {
    size = "large",
    colour = "red"
  }
}

This example takes it a step further by defining a map variable. This allows for more complex configurations to be passed to modules.

Module Outputs

Outputs are a way to extract information from a module. This can be useful when you want to use information from one module in another module, or to output important information from your infrastructure to the command line.

Example 3: Basic Output Variable

output "instance_id" {
  value = aws_instance.web.id
  description = "ID of the instance"
}

This output variable will display the instance ID of the AWS instance created within the module. It’s a simple way to output relevant information about resources managed by a module.

Example 4: Advanced Output Usage

output "frontend_ip" {
  value = aws_elb.frontend.dns_name
  description = "The DNS name for the frontend load balancer"
}

This example demonstrates how to use an output to share information between modules or output certain information to the user or other systems.

Passing Inputs and Outputs Between Modules

Inputs and outputs can be passed between modules, allowing for complex and interconnected infrastructure setups.

Example 5: Inter-module Communication

module "backend" {
  source = "./modules/backend"
  db_user = var.db_user
  db_password = var.db_password
}

module "frontend" {
  source = "./modules/frontend"
  backend_address = module.backend.address
}

This example shows how inputs and outputs can facilitate communication between modules. The backend’s address output is used as an input for the frontend module.

Best Practices for Using Inputs and Outputs in Terraform Modules

  • Keep module interfaces clean and minimal. Only expose variables and outputs that are necessary.
  • Use descriptive names and descriptions for your variables and outputs to make your configurations more readable.
  • Consider using type constraints and validation rules to make your modules more robust and self-documenting.

Conclusion

Understanding and effectively implementing inputs and outputs are critical for leveraging the full power of Terraform modules. They enable module reusability, reduce code duplication, and enhance the readability and maintainability of your Terraform configurations. With the examples provided, you’re well-equipped to start incorporating these practices into your Terraform projects.