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