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

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay