source

Updated at:
Copy as MD

The source argument in a module block tells Terraform where to find the module code.

The source argument defines where Terraform downloads the module code during terraform init, making it available for terraform plan and terraform apply. All supported source types are covered below.

For the full module block syntax, see Configure a module block. For the official Terraform reference, see Module Sources.

How it works

When you run terraform init, Terraform reads the source argument and downloads the module code to a local cache. Subsequent commands (terraform plan, terraform apply) use that cached code.

The source argument:

  • Must be a literal string. It does not support variable references, local values, or template expressions such as "${var.module_path}".

  • Is typically paired with a version pin (version for Terraform Registry modules, ref for Git sources) to lock the exact code used in each run.

  • Supports five source types: local paths, Terraform Registry, GitHub, generic Git repositories, and HTTP URLs.

After changing the source value, run terraform init again so Terraform can download the updated module code.

Source types

Local paths

Use a local path to reuse code within the same repository. The path must start with ./ or ../ to distinguish it from a remote source.

module "vpc" {
  source = "./vpc"
}

module "ecs" {
  source = "../modules/ecs"
}

Local path modules are not downloaded or copied — Terraform reads them directly from disk. When the parent module changes, the local module updates automatically.

Avoid using absolute file system paths to reference local modules.

To reference a child module in a subdirectory:

module "vpc_subnet" {
  source = "./vpc/modules/subnet"
}

Terraform Registry

Terraform Registry is the native distribution platform for Terraform modules. It supports full semantic versioning.

module "ecs" {
  source  = "alibaba/ecs-instance/alicloud"
  version = "1.2.0"
}

The address format is <NAMESPACE>/<NAME>/<PROVIDER>:

  • <NAMESPACE>: the organization that publishes the module. Alibaba Cloud uses four namespaces:

    • alibaba: primary Alibaba Cloud modules

    • aliyun: backup Alibaba Cloud namespace

    • terraform-alicloud-modules: legacy organization method

    • alibabacloud-automation: modules from the Alibaba Cloud automation team

  • <NAME>: describes the resource type the module manages. For example, ecs-instance creates and manages Elastic Compute Service (ECS) instances. Other common names include vpc, slb, rds, and security-group.

  • <PROVIDER>: the Terraform provider. Set this to alicloud for all Alibaba Cloud modules.

Examples:

  • alibaba/vpc/alicloud — creates Alibaba Cloud virtual private clouds (VPCs)

  • terraform-alicloud-modules/security-group/alicloud — creates Alibaba Cloud security groups

  • alibabacloud-automation/multiple-vpc-networks-peering/alicloud — deploys multi-VPC peering in a single region

Browse all available Alibaba Cloud modules at Terraform Registry.

To reference a child module inside a Registry module, use //:

module "nat_gateway" {
  source  = "alibaba/vpc/alicloud//modules/nat-gateway"
  version = "1.10.0"
}

GitHub

Terraform recognizes GitHub URLs and treats them as Git sources automatically. Both HTTPS and SSH are supported:

# HTTPS
module "vpc" {
  source = "github.com/alibabacloud-automation/terraform-alicloud-ram"
}

# SSH
module "vpc" {
  source = "git@github.com:alibabacloud-automation/terraform-alicloud-ram.git"
}

GitHub sources follow the same rules as generic Git sources — use ref to pin a version and // to reference a child module:

# Reference a child module
module "vpc_subnet" {
  source = "github.com/alibabacloud-automation/terraform-alicloud-ram//modules/ram-user"
}

# Reference a child module at a specific version using SSH
module "vpc_subnet" {
  source = "git@github.com:alibabacloud-automation/terraform-alicloud-ram.git//modules/ram-user?ref=v2.1.0"
}

Generic Git repositories

To use any Git repository as a source, prefix the URL with git:::

# HTTPS
module "vpc" {
  source = "git::https://github.com/alibabacloud-automation/terraform-alicloud-vpc.git"
}

# SSH
module "vpc" {
  source = "git::ssh://git@github.com/alibabacloud-automation/terraform-alicloud-vpc.git"
}

Reference a child module with //:

# Public repository
module "vpc_subnet" {
  source = "git::https://github.com/alibabacloud-automation/terraform-alicloud-ram.git//modules/ram-user?ref=v2.1.0"
}

# Private repository
module "security_component" {
  source = "git::ssh://git@example.com/company/terraform-modules.git//security/waf?ref=v1.0.0"
}

Specify a version

By default, Terraform clones the default branch. Use the ref query parameter to pin a specific branch, tag, or SHA-1 hash:

# Tag
module "vpc" {
  source = "git::https://github.com/alibabacloud-automation/terraform-alicloud-vpc.git?ref=v1.11.0"
}

# Commit SHA-1 hash
module "vpc" {
  source = "git::https://github.com/alibabacloud-automation/terraform-alicloud-vpc.git?ref=51d462976d84fdea54b47d80dcabbf680badcdb8"
}

Shallow clone

For large repositories, use depth=1 to perform a shallow clone and reduce download time:

module "vpc" {
  source = "git::https://github.com/alibabacloud-automation/terraform-alicloud-vpc.git?depth=1&ref=v1.11.0"
}

HTTP URLs

Terraform sends a GET request to the URL and follows the returned source address. If the URL points directly to an archive file, Terraform uses its contents as the module source:

# Redirects to another source address
module "vpc" {
  source = "https://example.com/modules/vpc"
}

# Direct archive file
module "vpc" {
  source = "https://example.com/vpc-module.zip"
}

Supported archive formats: zip, bz2, tar.bz2, tar.tbz2, tbz2, gz, tar.gz, tgz, xz, tar.xz, txz.

Reference a child module inside an archive with //:

module "vpc_subnet" {
  source = "https://example.com/vpc-module.zip//modules/subnet"
}

Referencing child modules

All source types except local paths support the // separator to reference a subdirectory within the source as a child module:

Source type

Example

Terraform Registry

alibaba/vpc/alicloud//modules/nat-gateway

GitHub

github.com/alibabacloud-automation/terraform-alicloud-ram//modules/ram-user

Generic Git

git::https://example.com/network.git//modules/vpc?ref=v1.2.0

HTTP archive

https://example.com/vpc-module.zip//modules/subnet

Best practices

Choose the right source type

  • Use local paths for business-critical modules that live in the same repository.

  • Use Terraform Registry for modules shared across multiple configurations.

  • Use SSH for private Git repositories in production environments.

Pin versions in production

Always specify an exact version in production to prevent unexpected changes:

  • Registry modules: use the version argument with semantic versioning constraints.

  • Git sources: use ref with a tag or SHA-1 hash rather than a branch name.

Use Alibaba Cloud modules

Prefer official Alibaba Cloud modules when available. Before using a module, verify it supports the target region and check whether any region-specific resource limits apply.

Keep modules up to date

Update modules regularly to pick up security fixes. Use HTTPS or SSH for all remote sources, and configure credentials for private repositories.

Examples

Alibaba Cloud Registry module

provider "alicloud" {
  region = "cn-hangzhou"
}

module "ecs" {
  source  = "alibaba/ecs-instance/alicloud"
  version = "2.5.0"

  instance_type = "ecs.g6.large"
  # Other configurations.
}

Private company module repository

module "security_baseline" {
  source = "git::ssh://git@example.com/company/security-baseline.git?ref=v1.0.0"

  environment = "production"
  # Other configurations.
}

Multi-environment configuration

The same source address works in multiple module blocks with different labels, letting you deploy the same module with different configurations — for example, one block per environment:

# Development environment
module "vpc_dev" {
  source  = "alibaba/vpc/alicloud"
  version = "~> 1.10.0"  # Allows patch version updates.

  environment = "dev"
  # Other configurations.
}

# Production environment
module "vpc_prod" {
  source  = "alibaba/vpc/alicloud"
  version = "1.10.0"  # Pinned to an exact version.

  environment = "prod"
  # Other configurations.
}