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