Hereโs a comprehensive tutorial on all valid Terraform module "source" Git URL patterns, with clear examples (no Drivemode references), so you can master using modules from GitHub, GitLab, Bitbucket, and private repos.
โ
Terraform source for Modules from Git: All Patterns & Examples
Terraform supports loading modules from Git repositories using either HTTPS, SSH, or GitHub short syntax. Below are all common patterns with examples.
๐น 1. GitHub (HTTPS)
Use this when you don’t want SSH keys or are using public repos.
module "vpc" {
source = "git::https://github.com/terraform-aws-modules/terraform-aws-vpc.git"
}
Code language: JavaScript (javascript)
โ With Subdirectory
module "s3" {
source = "git::https://github.com/your-org/terraform-modules.git//modules/s3_bucket"
}
Code language: JavaScript (javascript)
โ With Tag / Branch
source = "git::https://github.com/your-org/terraform-modules.git//modules/s3_bucket?ref=v1.2.0"
source = "git::https://github.com/your-org/terraform-modules.git//modules/s3_bucket?ref=main"
Code language: JavaScript (javascript)
๐น 2. GitHub (SSH)
Use SSH if you’re pulling private modules using a deploy key or SSH agent.
module "eks" {
source = "git::ssh://git@github.com/your-org/terraform-modules.git//modules/eks_cluster?ref=main"
}
Code language: JavaScript (javascript)
โ Use
git::ssh://for SSH โ don’t use rawgit@github.com.
๐น 3. GitHub (Short Syntax โ Only for Public Modules)
module "vpc" {
source = "github.com/terraform-aws-modules/terraform-aws-vpc"
}
Code language: JavaScript (javascript)
โ ๏ธ This works only for the root of the repo (no subdirectories, no tags).
๐น 4. GitLab (HTTPS)
module "firewall" {
source = "git::https://gitlab.com/your-org/infra-modules.git//firewall?ref=main"
}
Code language: JavaScript (javascript)
๐น 5. Bitbucket (HTTPS)
module "app" {
source = "git::https://bitbucket.org/your-team/terraform-modules.git//modules/app?ref=release-1.0"
}
Code language: JavaScript (javascript)
๐น 6. Private Git Repos (with SSH)
module "db" {
source = "git::ssh://git@bitbucket.org/your-org/terraform-db-modules.git//rds?ref=main"
}
Code language: JavaScript (javascript)
- Make sure your runner (Terraform Cloud or CI) has access via SSH key.
- Can also use
.netrcorGIT_ASKPASSfor HTTPS auth in private repos.
๐ฆ Structure of a Terraform Git Source URL
git::<protocol>://<host>/<repo>.git//<subdir>?ref=<branch|tag|commit>
Code language: HTML, XML (xml)
| Part | Example |
|---|---|
git:: prefix | tells Terraform to treat it as a Git source |
https:// or ssh:// | Git protocol |
//modules/x | points to subdirectory inside the repo |
?ref= | tag, branch, or commit hash |
โ Pro Tips
| Tip | Reason |
|---|---|
Always pin a tag using ?ref=... | Prevents breaking changes |
Use double slashes // before module subdir | Required for subdirectory paths |
| Prefer HTTPS for public or Terraform Cloud | Easier integration |
| Use SSH only if youโve configured keys properly | Avoids access errors |
๐ Real-World Example
Letโs say you have this repo:
https://github.com/acme-corp/terraform-infra-modules
Code language: JavaScript (javascript)
With a folder structure like:
terraform-infra-modules/
โโโ vpc/
โโโ eks/
โโโ s3/
You can use:
module "vpc" {
source = "git::https://github.com/acme-corp/terraform-infra-modules.git//vpc?ref=v1.0.0"
}
module "eks" {
source = "git::ssh://git@github.com/acme-corp/terraform-infra-modules.git//eks?ref=main"
}
Code language: JavaScript (javascript)
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals