Learn how to effectively reference and utilize resources created by Terraform modules in your infrastructure code.
Terraform modules are a powerful way to organize and reuse your infrastructure code. Often, you'll need to use resources created in one module within another module. This guide explains how to pass resource information between Terraform modules using outputs and references.
To use a resource created in one Terraform module within another module, you need to follow these steps:
Output the necessary values from the source module:
output block to expose the attributes of the resource you want to reference.Reference the output in the consuming module:
module.<MODULE_NAME>.<OUTPUT_NAME>.<MODULE_NAME> with the name of the module where the output is defined.<OUTPUT_NAME> with the name of the output you want to use.Example:
Let's say you have a module named "networking" that creates a VPC and outputs its ID:
# networking/outputs.tf
output "vpc_id" {
value = aws_vpc.main.id
}Now, you have another module named "compute" that needs to create an EC2 instance within that VPC:
# compute/main.tf
resource "aws_instance" "example" {
# ... other configurations ...
subnet_id = module.networking.vpc_id
}
module "networking" {
source = "../networking"
# ... other configurations ...
}In this example:
vpc_id.module.networking.vpc_id when defining the subnet_id for the EC2 instance.Important Notes:
This code demonstrates how to use Terraform modules to create a VPC and launch an EC2 instance in it. The networking module defines a VPC and outputs its ID. The compute module defines an EC2 instance and takes the VPC ID as an input. The root module calls both modules and passes the VPC ID from the networking module to the compute module. This allows for reusable infrastructure components and managed dependencies between them.
This example demonstrates how to create a VPC in one module and use its ID to launch an EC2 instance in another module.
1. Networking Module (networking)
This module creates a VPC and outputs its ID.
# networking/variables.tf
variable "vpc_cidr" {
description = "The CIDR block for the VPC"
type = string
default = "10.0.0.0/16"
}
# networking/main.tf
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr
enable_dns_hostnames = true
enable_dns_support = true
tags = {
Name = "main-vpc"
}
}
# networking/outputs.tf
output "vpc_id" {
value = aws_vpc.main.id
description = "The ID of the VPC"
}2. Compute Module (compute)
This module creates an EC2 instance and takes the VPC ID as an input from the networking module.
# compute/variables.tf
variable "vpc_id" {
description = "The ID of the VPC to launch the instance in"
type = string
}
# compute/main.tf
resource "aws_instance" "example" {
ami = "ami-0c55b159c9e670235" # Replace with your desired AMI ID
instance_type = "t2.micro"
subnet_id = var.vpc_id
tags = {
Name = "example-instance"
}
}
module "networking" {
source = "../networking"
}3. Root Module (main.tf)
This module calls both the networking and compute modules.
module "networking" {
source = "./networking"
}
module "compute" {
source = "./compute"
vpc_id = module.networking.vpc_id
}Explanation:
networking module defines a VPC and outputs its ID using the output block.compute module defines an EC2 instance and takes the VPC ID as an input variable.root module calls both modules and passes the vpc_id output from the networking module to the compute module using module.networking.vpc_id.This example demonstrates how to use Terraform modules to create reusable infrastructure components and manage dependencies between them. By using outputs and module references, you can create complex infrastructure deployments while maintaining code clarity and reusability.
This table summarizes how to use resources from one Terraform module in another:
| Step | Source Module | Consuming Module
By using outputs and module references, you can create modular, reusable, and maintainable Terraform code for managing your infrastructure. This approach not only improves code organization but also enhances collaboration and reduces the risk of errors during deployments. Remember to follow best practices for documentation, version control, and security to maximize the benefits of using Terraform modules in your projects.
How to Reference a Resource Created by a Terraform Module ... | Jun 18, 2023 ... Step 1: Defining the Output in the Terraform Module. The first step is to define the output for the resource you want to reference in the module ...
References to Values - Configuration Language | Terraform ... | ... Terraform as a reference to a managed resource. For more information about how ... If you are writing a shared module which needs a prefix to help create ...
Understanding Terraform Data Sources in Modules | Random Blurbs ... | So over the last week I have been battling an issue in Terraform that truly drove me nuts and I think understanding can help someone else who is struggling with the same issue. What is a data sourc…
Referencing resource created in one module in another module ... | Suppose I have two files/modules in Azure Bicep, both are called in a 'main.bicep'. One is called 'storage.bicep' and contains, among others, the following code to create a storageAccount:
How to use one module's variable in another module in terraform ... | To get the output from one module to another module in Terraform, you can use the following steps: Here is an example of how to do this: In this example,...