github.com/jaredpalmer/terraform@v1.1.0-alpha20210908.0.20210911170307-88705c943a03/website/docs/language/modules/sources.html.md (about) 1 --- 2 layout: "language" 3 page_title: "Module Sources" 4 sidebar_current: "docs-modules-sources" 5 description: "The source argument tells Terraform where to find child modules's configurations in locations like GitHub, the Terraform Registry, Bitbucket, Git, Mercurial, S3, and GCS." 6 --- 7 8 # Module Sources 9 10 The `source` argument in [a `module` block](/docs/language/modules/syntax.html) 11 tells Terraform where to find the source code for the desired child module. 12 13 Terraform uses this during the module installation step of `terraform init` 14 to download the source code to a directory on local disk so that it can be 15 used by other Terraform commands. 16 17 > **Hands-on:** Try our HashiCorp Learn tutorials to use modules from [the 18 > registry](https://learn.hashicorp.com/tutorials/terraform/module-use) 19 >or [locally](https://learn.hashicorp.com/tutorials/terraform/module-create). 20 21 The module installer supports installation from a number of different source 22 types, as listed below. 23 24 * [Local paths](#local-paths) 25 26 * [Terraform Registry](#terraform-registry) 27 28 * [GitHub](#github) 29 30 * [Bitbucket](#bitbucket) 31 32 * Generic [Git](#generic-git-repository), [Mercurial](#generic-mercurial-repository) repositories 33 34 * [HTTP URLs](#http-urls) 35 36 * [S3 buckets](#s3-bucket) 37 38 * [GCS buckets](#gcs-bucket) 39 40 * [Modules in Package Sub-directories](#modules-in-package-sub-directories) 41 42 Each of these is described in the following sections. Module source addresses 43 use a _URL-like_ syntax, but with extensions to support unambiguous selection 44 of sources and additional features. 45 46 We recommend using local file paths for closely-related modules used primarily 47 for the purpose of factoring out repeated code elements, and using a native 48 Terraform module registry for modules intended to be shared by multiple calling 49 configurations. We support other sources so that you can potentially distribute 50 Terraform modules internally with existing infrastructure. 51 52 Many of the source types will make use of "ambient" credentials available 53 when Terraform is run, such as from environment variables or credentials files 54 in your home directory. This is covered in more detail in each of the following 55 sections. 56 57 We recommend placing each module that is intended to be re-usable in the root 58 of its own repository or archive file, but it is also possible to 59 [reference modules from subdirectories](#modules-in-package-sub-directories). 60 61 ## Local Paths 62 63 Local path references allow for factoring out portions of a configuration 64 within a single source repository. 65 66 ```hcl 67 module "consul" { 68 source = "./consul" 69 } 70 ``` 71 72 A local path must begin with either `./` or `../` to indicate that a local 73 path is intended, to distinguish from 74 [a module registry address](#terraform-registry). 75 76 Local paths are special in that they are not "installed" in the same sense 77 that other sources are: the files are already present on local disk (possibly 78 as a result of installing a parent module) and so can just be used directly. 79 Their source code is automatically updated if the parent module is upgraded. 80 81 Note that Terraform does not consider an _absolute_ filesystem path (starting 82 with a slash, a drive letter, or similar) to be a local path. Instead, 83 Terraform will treat that in a similar way as a remote module and copy it into 84 the local module cache. An absolute path is a "package" in the sense described 85 in [Modules in Package Sub-directories](#modules-in-package-sub-directories). 86 We don't recommend using absolute filesystem paths to refer to Terraform 87 modules, because it will tend to couple your configuration to the filesystem 88 layout of a particular computer. 89 90 ## Terraform Registry 91 92 A module registry is the native way of distributing Terraform modules for use 93 across multiple configurations, using a Terraform-specific protocol that 94 has full support for module versioning. 95 96 [Terraform Registry](https://registry.terraform.io/) is an index of modules 97 shared publicly using this protocol. This public registry is the easiest way 98 to get started with Terraform and find modules created by others in the 99 community. 100 101 You can also use a 102 [private registry](/docs/registry/private.html), either 103 via the built-in feature from Terraform Cloud, or by running a custom 104 service that implements 105 [the module registry protocol](/docs/registry/api.html). 106 107 Modules on the public Terraform Registry can be referenced using a registry 108 source address of the form `<NAMESPACE>/<NAME>/<PROVIDER>`, with each 109 module's information page on the registry site including the exact address 110 to use. 111 112 ```hcl 113 module "consul" { 114 source = "hashicorp/consul/aws" 115 version = "0.1.0" 116 } 117 ``` 118 119 The above example will use the 120 [Consul module for AWS](https://registry.terraform.io/modules/hashicorp/consul/aws) 121 from the public registry. 122 123 For modules hosted in other registries, prefix the source address with an 124 additional `<HOSTNAME>/` portion, giving the hostname of the private registry: 125 126 ```hcl 127 module "consul" { 128 source = "app.terraform.io/example-corp/k8s-cluster/azurerm" 129 version = "1.1.0" 130 } 131 ``` 132 133 If you are using the SaaS version of Terraform Cloud, its private 134 registry hostname is `app.terraform.io`. If you use a self-hosted Terraform 135 Enterprise instance, its private registry hostname is the same as the host 136 where you'd access the web UI and the host you'd use when configuring 137 the `remote` backend. 138 139 Registry modules support versioning. You can provide a specific version as shown 140 in the above examples, or use flexible 141 [version constraints](/docs/language/modules/syntax.html#version). 142 143 You can learn more about the registry at the 144 [Terraform Registry documentation](/docs/registry/modules/use.html#using-modules). 145 146 To access modules from a private registry, you may need to configure an access 147 token [in the CLI config](/docs/cli/config/config-file.html#credentials). Use the 148 same hostname as used in the module source string. For a private registry 149 within Terraform Cloud, use the same authentication token as you would 150 use with the Enterprise API or command-line clients. 151 152 ## GitHub 153 154 Terraform will recognize unprefixed `github.com` URLs and interpret them 155 automatically as Git repository sources. 156 157 ```hcl 158 module "consul" { 159 source = "github.com/hashicorp/example" 160 } 161 ``` 162 163 The above address scheme will clone over HTTPS. To clone over SSH, use the 164 following form: 165 166 ```hcl 167 module "consul" { 168 source = "git@github.com:hashicorp/example.git" 169 } 170 ``` 171 172 These GitHub schemes are treated as convenient aliases for 173 [the general Git repository address scheme](#generic-git-repository), and so 174 they obtain credentials in the same way and support the `ref` argument for 175 selecting a specific revision. You will need to configure credentials in 176 particular to access private repositories. 177 178 ## Bitbucket 179 180 Terraform will recognize unprefixed `bitbucket.org` URLs and interpret them 181 automatically as BitBucket repositories: 182 183 ```hcl 184 module "consul" { 185 source = "bitbucket.org/hashicorp/terraform-consul-aws" 186 } 187 ``` 188 189 This shorthand works only for public repositories, because Terraform must 190 access the BitBucket API to learn if the given repository uses Git or Mercurial. 191 192 Terraform treats the result either as [a Git source](#generic-git-repository) 193 or [a Mercurial source](#generic-mercurial-repository) depending on the 194 repository type. See the sections on each version control type for information 195 on how to configure credentials for private repositories and how to specify 196 a specific revision to install. 197 198 ## Generic Git Repository 199 200 Arbitrary Git repositories can be used by prefixing the address with the 201 special `git::` prefix. After this prefix, any valid 202 [Git URL](https://git-scm.com/docs/git-clone#_git_urls) 203 can be specified to select one of the protocols supported by Git. 204 205 For example, to use HTTPS or SSH: 206 207 ```hcl 208 module "vpc" { 209 source = "git::https://example.com/vpc.git" 210 } 211 212 module "storage" { 213 source = "git::ssh://username@example.com/storage.git" 214 } 215 ``` 216 217 Terraform installs modules from Git repositories by running `git clone`, and 218 so it will respect any local Git configuration set on your system, including 219 credentials. To access a non-public Git repository, configure Git with 220 suitable credentials for that repository. 221 222 If you use the SSH protocol then any configured SSH keys will be used 223 automatically. This is the most common way to access non-public Git 224 repositories from automated systems because it allows access to private 225 repositories without interactive prompts. 226 227 If using the HTTP/HTTPS protocol, or any other protocol that uses 228 username/password credentials, configure 229 [Git Credentials Storage](https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage) 230 to select a suitable source of credentials for your environment. 231 232 If your Terraform configuration will be used within [Terraform Cloud](https://www.hashicorp.com/products/terraform), 233 only SSH key authentication is supported, and 234 [keys can be configured on a per-workspace basis](/docs/cloud/workspaces/ssh-keys.html). 235 236 ### Selecting a Revision 237 238 By default, Terraform will clone and use the default branch (referenced by 239 `HEAD`) in the selected repository. You can override this using the 240 `ref` argument. The value of the `ref` argument can be any reference that would be accepted 241 by the `git checkout` command, such as branch, SHA-1 hash (short or full), or tag names. The [Git documentation](https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection#_single_revisions) contains a complete list. 242 243 ```hcl 244 # referencing a specific release 245 module "vpc" { 246 source = "git::https://example.com/vpc.git?ref=v1.2.0" 247 } 248 249 # referencing a specific commit SHA-1 hash 250 module "storage" { 251 source = "git::https://example.com/storage.git?ref=51d462976d84fdea54b47d80dcabbf680badcdb8" 252 } 253 ``` 254 255 ### "scp-like" address syntax 256 257 When using Git over SSH, we recommend using the `ssh://`-prefixed URL form 258 for consistency with all of the other URL-like git address forms. 259 You may opt to use the alternative "scp-like" syntax instead, in which case you 260 must omit the `ssh://` scheme part and include only the `git::` part. 261 For example: 262 263 ```hcl 264 module "storage" { 265 source = "git::username@example.com:storage.git" 266 } 267 ``` 268 269 270 If you use the `ssh://` URL scheme then Terraform will assume that the colon 271 marks the beginning of a port number, rather than the beginning of the path. 272 This matches how Git itself interprets these different forms, aside from 273 the Terraform-specific `git::` selector prefix. 274 275 ## Generic Mercurial Repository 276 277 You can use arbitrary Mercurial repositories by prefixing the address with the 278 special `hg::` prefix. After this prefix, any valid 279 [Mercurial URL](https://www.mercurial-scm.org/repo/hg/help/urls) 280 can be specified to select one of the protocols supported by Mercurial. 281 282 ```hcl 283 module "vpc" { 284 source = "hg::http://example.com/vpc.hg" 285 } 286 ``` 287 288 Terraform installs modules from Mercurial repositories by running `hg clone`, and 289 so it will respect any local Mercurial configuration set on your system, 290 including credentials. To access a non-public repository, configure Mercurial 291 with suitable credentials for that repository. 292 293 If you use the SSH protocol then any configured SSH keys will be used 294 automatically. This is the most common way to access non-public Mercurial 295 repositories from automated systems because it allows access to private 296 repositories without interactive prompts. 297 298 If your Terraform configuration will be used within [Terraform Cloud](https://www.hashicorp.com/products/terraform), 299 only SSH key authentication is supported, and 300 [keys can be configured on a per-workspace basis](/docs/cloud/workspaces/ssh-keys.html). 301 302 ### Selecting a Revision 303 304 You can select a non-default branch or tag using the optional `ref` argument: 305 306 ```hcl 307 module "vpc" { 308 source = "hg::http://example.com/vpc.hg?ref=v1.2.0" 309 } 310 ``` 311 312 ## HTTP URLs 313 314 When you use an HTTP or HTTPS URL, Terraform will make a `GET` request to 315 the given URL, which can return _another_ source address. This indirection 316 allows using HTTP URLs as a sort of "vanity redirect" over a more complicated 317 module source address. 318 319 Terraform will append an additional query string argument `terraform-get=1` to 320 the given URL before sending the `GET` request, allowing the server to 321 optionally return a different result when Terraform is requesting it. 322 323 If the response is successful (`200`-range status code), Terraform looks in 324 the following locations in order for the next address to access: 325 326 * The value of a response header field named `X-Terraform-Get`. 327 328 * If the response is an HTML page, a `meta` element with the name `terraform-get`: 329 330 ```html 331 <meta name="terraform-get" 332 content="github.com/hashicorp/example" /> 333 ``` 334 335 In either case, the result is interpreted as another module source address 336 using one of the forms documented elsewhere on this page. 337 338 If an HTTP/HTTPS URL requires authentication credentials, use a `.netrc` 339 file in your home directory to configure these. For information on this format, 340 see [the documentation for using it in `curl`](https://everything.curl.dev/usingcurl/netrc). 341 342 ### Fetching archives over HTTP 343 344 As a special case, if Terraform detects that the URL has a common file 345 extension associated with an archive file format then it will bypass the 346 special `terraform-get=1` redirection described above and instead just use 347 the contents of the referenced archive as the module source code: 348 349 ```hcl 350 module "vpc" { 351 source = "https://example.com/vpc-module.zip" 352 } 353 ``` 354 355 The extensions that Terraform recognizes for this special behavior are: 356 357 * `zip` 358 * `tar.bz2` and `tbz2` 359 * `tar.gz` and `tgz` 360 * `tar.xz` and `txz` 361 362 If your URL _doesn't_ have one of these extensions but refers to an archive 363 anyway, use the `archive` argument to force this interpretation: 364 365 ```hcl 366 module "vpc" { 367 source = "https://example.com/vpc-module?archive=zip" 368 } 369 ``` 370 371 -> **Note:** If the content of the archive file is a directory, you will need to 372 include that directory in the module source. Read the section on 373 [Modules in Package Sub-directories](#modules-in-package-sub-directories) for more 374 information. 375 376 ## S3 Bucket 377 378 You can use archives stored in S3 as module sources using the special `s3::` 379 prefix, followed by 380 [an S3 bucket object URL](https://docs.aws.amazon.com/AmazonS3/latest/userguide/UsingBucket.html). 381 382 ```hcl 383 module "consul" { 384 source = "s3::https://s3-eu-west-1.amazonaws.com/examplecorp-terraform-modules/vpc.zip" 385 } 386 ``` 387 388 -> **Note:** Buckets in AWS's us-east-1 region must use the hostname `s3.amazonaws.com` (instead of `s3-us-east-1.amazonaws.com`). 389 390 The `s3::` prefix causes Terraform to use AWS-style authentication when 391 accessing the given URL. As a result, this scheme may also work for other 392 services that mimic the S3 API, as long as they handle authentication in the 393 same way as AWS. 394 395 The resulting object must be an archive with one of the same file 396 extensions as for [archives over standard HTTP](#fetching-archives-over-http). 397 Terraform will extract the archive to obtain the module source tree. 398 399 The module installer looks for AWS credentials in the following locations, 400 preferring those earlier in the list when multiple are available: 401 402 * The `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables. 403 * The default profile in the `.aws/credentials` file in your home directory. 404 * If running on an EC2 instance, temporary credentials associated with the 405 instance's IAM Instance Profile. 406 407 ## GCS Bucket 408 409 You can use archives stored in Google Cloud Storage as module sources using the special `gcs::` 410 prefix, followed by 411 [a GCS bucket object URL](https://cloud.google.com/storage/docs/request-endpoints#typical). 412 413 For example 414 415 * `gcs::https://www.googleapis.com/storage/v1/BUCKET_NAME/PATH_TO_MODULE` 416 * `gcs::https://www.googleapis.com/storage/v1/BUCKET_NAME/PATH/TO/module.zip` 417 418 ```hcl 419 module "consul" { 420 source = "gcs::https://www.googleapis.com/storage/v1/modules/foomodule.zip" 421 } 422 ``` 423 424 The module installer uses Google Cloud SDK to authenticate with GCS. To set credentials you can: 425 426 * Enter the path of your service account key file in the GOOGLE_APPLICATION_CREDENTIALS environment variable, or; 427 * If you're running Terraform from a GCE instance, default credentials are automatically available. See [Creating and Enabling Service Accounts](https://cloud.google.com/compute/docs/access/create-enable-service-accounts-for-instances) for Instances for more details 428 * On your computer, you can make your Google identity available by running `gcloud auth application-default login`. 429 430 431 ## Modules in Package Sub-directories 432 433 When the source of a module is a version control repository or archive file 434 (generically, a "package"), the module itself may be in a sub-directory relative 435 to the root of the package. 436 437 A special double-slash syntax is interpreted by Terraform to indicate that 438 the remaining path after that point is a sub-directory within the package. 439 For example: 440 441 * `hashicorp/consul/aws//modules/consul-cluster` 442 * `git::https://example.com/network.git//modules/vpc` 443 * `https://example.com/network-module.zip//modules/vpc` 444 * `s3::https://s3-eu-west-1.amazonaws.com/examplecorp-terraform-modules/network.zip//modules/vpc` 445 446 If the source address has arguments, such as the `ref` argument supported for 447 the version control sources, the sub-directory portion must be _before_ those 448 arguments: 449 450 * `git::https://example.com/network.git//modules/vpc?ref=v1.2.0` 451 452 Terraform will still extract the entire package to local disk, but will read 453 the module from the subdirectory. As a result, it is safe for a module in 454 a sub-directory of a package to use [a local path](#local-paths) to another 455 module as long as it is in the _same_ package.