DEV Community

sam-nash
sam-nash

Posted on

Terraform Module Sources

How Terraform Finds Reusable Modules

Terraform lets you break down your infrastructure configuration into smaller, reusable pieces called modules. But where does Terraform find the code for these modules? That's where the source argument comes in.

The source argument tells Terraform where to look for the module's code. There are several options available, depending on where you store your modules:

Local Files: Perfect for keeping modules within the same project. Just use a path starting with ./ or ../ to point Terraform to the module directory.

module "local_database" {
  source = "./modules/database"
}
Enter fullscreen mode Exit fullscreen mode

Terraform Registry: This is a terraform platform for discovering providers, modules and security policies. You can specify the module's name and version to use a specific one.

module "aws_ec2_instance" {
  source  = "hashicorp/aws/ec2"
  version = "~> 4.0"
}
Enter fullscreen mode Exit fullscreen mode

Version Control Systems (VCS): If your modules live in a Git repository (like GitHub or Bitbucket), or a Mercurial repository, you can provide the URL to the repository.

module "private_module" {
  source = "git@github.com:your-org/your-private-module.git"
}
Enter fullscreen mode Exit fullscreen mode

Http/s URLs: Modules can also be downloaded from web servers using HTTP or HTTPS URLs. Terraform can even follow redirects to other source locations.

module "http_module" {
  source = "https://your-company-registry.com/modules/my-module"
}
Enter fullscreen mode Exit fullscreen mode

Cloud Storage: Modules stored in Amazon S3 buckets can be accessed by prefixing the URL with s3::

module "s3_module" {
  source = "s3::s3.amazonaws.com/your-bucket/my-module.zip"
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)