How to Reference a Resource Created by a Terraform Module?

Greetings to all DevOps enthusiasts and cloud technology practitioners! In today's blog post, we'll delve deeper into one of the key components of infrastructure as code (IaC) - Terraform.

More specifically, we'll discuss how to reference a resource created by a Terraform module. This capability provides a more flexible, scalable, and efficient way to manage your infrastructure.


What are Terraform and Terraform Modules?

Before we dive into the details, let's set the context with a quick overview of Terraform and Terraform Modules.

Terraform, developed by HashiCorp, is a powerful tool for building, changing, and versioning infrastructure safely and efficiently. It helps you manage and codify APIs into declarative configuration files that can be shared among team members, treated as code, edited, reviewed, and versioned.

Terraform modules encapsulate distinct logical components of your infrastructure by grouping resources. They can be used for creating reusable components in your Terraform code, which can be invoked multiple times with varying inputs to create different instances of the same infrastructure component.

Read More - How Terraform modules works?


The Power of Referencing

Referencing resources created by a Terraform module is a handy trick when your Terraform configuration begins to grow. It allows you to access outputs from one module in another, fostering modularity and reusability. Essentially, these references provide a way to connect different modules or to connect resources within the same module.

Now, let's break down how to achieve this.


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. This is the value that you will later access from the main configuration file that calls the module.

Imagine you have a resource defined in your module, let's say an AWS EC2 instance:

 1# File path = ../ec2_module/main.tf
 2
 3# Create an EC2 Instance 
 4resource "aws_instance" "example" {
 5  ami           = "ami-0c94855ba95c574c8"
 6  instance_type = "t2.micro"
 7}
 8
 9# Set the output (this output will be used as a reference later from another module)
10output "instance_id" {
11  value = aws_instance.example.id
12}
13 

This output will give you access to the ID of the aws_instance.example instance created by this module. It's important to note that Terraform does not automatically expose all attributes of all resources; you must explicitly define the outputs you want to access.


Step 2: Referencing the Module Output in the Main Configuration

After you have defined your output in the module, you can reference it in your main configuration where you call the module.

For instance, let's say you're creating an AWS Elastic IP resource and want to associate it with the instance created by the module. Here's how you can do it:

 1
 2## Call the EC2 Module  "../ec2_module"
 3module "ec2_module" {
 4  source = "../ec2_module"
 5}
 6
 7# Referencing the output .i.e. instance_id generated as output from ec2_module
 8resource "aws_eip" "lb" {
 9  vpc      = true
10  instance = module.ec2_module.instance_id
11}
12 

In the above example, module.ec2_module.instance_id refers to the instance_id output of the ec2_module. This line of code assigns the Elastic IP to the instance created by the module.

Please replace the placeholders and other values with the actual ones that correspond to your project.


Conclusion

Terraform offers a powerful, flexible approach to managing complex infrastructures. By making use of modules and their referencing capabilities, you can make your infrastructure code more maintainable, scalable, and efficient. Whether you're a seasoned DevOps professional or just beginning


Posts in this Series