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