github.com/hugorut/terraform@v1.1.3/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 `remote` backend.
   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. The [Git documentation](https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection#_single_revisions) contains a complete list.
   243  
   244  ```hcl
   245  # referencing a specific release
   246  module "vpc" {
   247    source = "git::https://example.com/vpc.git?ref=v1.2.0"
   248  }
   249  
   250  # referencing a specific commit SHA-1 hash
   251  module "storage" {
   252    source = "git::https://example.com/storage.git?ref=51d462976d84fdea54b47d80dcabbf680badcdb8"
   253  }
   254  ```
   255  
   256  ### "scp-like" address syntax
   257  
   258  When using Git over SSH, we recommend using the `ssh://`-prefixed URL form
   259  for consistency with all of the other URL-like git address forms.
   260  You may opt to use the alternative "scp-like" syntax instead, in which case you
   261  must omit the `ssh://` scheme part and include only the `git::` part.
   262  For example:
   263  
   264  ```hcl
   265  module "storage" {
   266    source = "git::username@example.com:storage.git"
   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](/cloud-docs/workspaces/settings/ssh-keys).
   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  ## Modules in Package Sub-directories
   431  
   432  When the source of a module is a version control repository or archive file
   433  (generically, a "package"), the module itself may be in a sub-directory relative
   434  to the root of the package.
   435  
   436  A special double-slash syntax is interpreted by Terraform to indicate that
   437  the remaining path after that point is a sub-directory within the package.
   438  For example:
   439  
   440  * `hashicorp/consul/aws//modules/consul-cluster`
   441  * `git::https://example.com/network.git//modules/vpc`
   442  * `https://example.com/network-module.zip//modules/vpc`
   443  * `s3::https://s3-eu-west-1.amazonaws.com/examplecorp-terraform-modules/network.zip//modules/vpc`
   444  
   445  If the source address has arguments, such as the `ref` argument supported for
   446  the version control sources, the sub-directory portion must be _before_ those
   447  arguments:
   448  
   449  * `git::https://example.com/network.git//modules/vpc?ref=v1.2.0`
   450  
   451  Terraform will still extract the entire package to local disk, but will read
   452  the module from the subdirectory. As a result, it is safe for a module in
   453  a sub-directory of a package to use [a local path](#local-paths) to another
   454  module as long as it is in the _same_ package.