What is the difference between a module and a resource?

It’s not really clear some refer to a bunch of files as a module ?
and some refer to a block with module tag
module a {
}
as a module ?

And how can I have a resource that uses a module ?

Hey Ibrahim! I’m not positive what you’re asking in the first sentence.

However, A Module is a container for multiple resources that are used together. The Modules section of the Terraform documentation is helpful here.

Regarding how resources access values from within a module, or as you asked how can a resource use something contained within a module, you’ll need to declare output values. The same page above has a specific section you can refer to on Accessing Module Output Values.

There’s also a really great introduction on the HashiCorp Learn Track for Terraform: Terraform Getting Started | Modules

Hope this helps.

3 Likes

Quick breakdown in pseudo-code version. Hope this helps.
You have these two directories
/modules/
/myproject/

You have a folder within /modules/
/modules/aws_stuff/
With this file
main.tf

Data within /modules/aws_stuff/main.tf
resource “aws_instance” “host1” {
name = host1
}

resource “aws_eip” “elastic_ip” {
name = eip1
}

Now you import that module in file /myproject/main.tf
module “aws” {
source = “…/modules/aws_stuff”
}

So the big idea behind modules is it keep your code DRY and allows you to reuse large chunks of resources as well as control how infrastructure is created because maybe you could change the name of the resources but the basic layout is controlled by however your module is laid out.

1 Like

So another question let’s say I have to use provisioners , A module can’t have provisioners right ?

Nope your module can have provisioners. Below is an example of a module I have that creates a resource and I use a provisioner just to ssh to the box to confirm ssh is going to work.

resource "aws_instance" "host" {
  vpc_security_group_ids      = [var.aws_sg]
  associate_public_ip_address = true
  subnet_id                   = var.subnet
  ami                         = var.ami
  instance_type               = var.size
  key_name                    = aws_key_pair.host_key.key_name
  availability_zone           = var.a_zone
  depends_on                  = [aws_key_pair.host_key]
  root_block_device {
    delete_on_termination = var.delete_root_volume
  }
  tags = {
    Status = var.dev_or_prod
    Name   = var.instance_name
  }
  provisioner "remote-exec" {
    inline = ["echo 'Im alive!'"]

    connection {
      type        = "ssh"
      user        = "ubuntu"
      private_key = file("~/.ssh/id_rsa")
      host        = aws_instance.host.public_ip
    }
  }
}

Very help full info. Thanks

1 Like

For those who get confused with Modules — my explanation in English :slight_smile:

1 Like

Now, what if I want to import a module in a file consisting of different resources.
The module file is in another github repo and the resource file is in another github repo.
Should I just go by simply giving the source of the module or I will have to give a link based directory for eg : “git@ something” ?

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.