github.com/iaas-resource-provision/iaas-rpc@v1.0.7-0.20211021023331-ed21f798c408/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  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 use a self-hosted Terraform
   120  Enterprise instance, its private registry hostname is the same as the host
   121  where you'd access the web UI and the host you'd use when configuring
   122  the `remote` backend.
   123  
   124  Registry modules support versioning. You can provide a specific version as shown
   125  in the above examples, or use flexible
   126  [version constraints](/docs/language/modules/syntax.html#version).
   127  
   128  You can learn more about the registry at the
   129  [Terraform Registry documentation](/docs/registry/modules/use.html#using-modules).
   130  
   131  To access modules from a private registry, you may need to configure an access
   132  token [in the CLI config](/docs/cli/config/config-file.html#credentials). Use the
   133  same hostname as used in the module source string. For a private registry
   134  within Terraform Cloud, use the same authentication token as you would
   135  use with the Enterprise API or command-line clients.
   136  
   137  ## GitHub
   138  
   139  Terraform will recognize unprefixed `github.com` URLs and interpret them
   140  automatically as Git repository sources.
   141  
   142  ```hcl
   143  module "consul" {
   144    source = "github.com/hashicorp/example"
   145  }
   146  ```
   147  
   148  The above address scheme will clone over HTTPS. To clone over SSH, use the
   149  following form:
   150  
   151  ```hcl
   152  module "consul" {
   153    source = "git@github.com:hashicorp/example.git"
   154  }
   155  ```
   156  
   157  These GitHub schemes are treated as convenient aliases for
   158  [the general Git repository address scheme](#generic-git-repository), and so
   159  they obtain credentials in the same way and support the `ref` argument for
   160  selecting a specific revision. You will need to configure credentials in
   161  particular to access private repositories.
   162  
   163  ## Bitbucket
   164  
   165  Terraform will recognize unprefixed `bitbucket.org` URLs and interpret them
   166  automatically as BitBucket repositories:
   167  
   168  ```hcl
   169  module "consul" {
   170    source = "bitbucket.org/hashicorp/terraform-consul-aws"
   171  }
   172  ```
   173  
   174  This shorthand works only for public repositories, because Terraform must
   175  access the BitBucket API to learn if the given repository uses Git or Mercurial.
   176  
   177  Terraform treats the result either as [a Git source](#generic-git-repository)
   178  or [a Mercurial source](#generic-mercurial-repository) depending on the
   179  repository type. See the sections on each version control type for information
   180  on how to configure credentials for private repositories and how to specify
   181  a specific revision to install.
   182  
   183  ## Generic Git Repository
   184  
   185  Arbitrary Git repositories can be used by prefixing the address with the
   186  special `git::` prefix. After this prefix, any valid
   187  [Git URL](https://git-scm.com/docs/git-clone#_git_urls)
   188  can be specified to select one of the protocols supported by Git.
   189  
   190  For example, to use HTTPS or SSH:
   191  
   192  ```hcl
   193  module "vpc" {
   194    source = "git::https://example.com/vpc.git"
   195  }
   196  
   197  module "storage" {
   198    source = "git::ssh://username@example.com/storage.git"
   199  }
   200  ```
   201  
   202  Terraform installs modules from Git repositories by running `git clone`, and
   203  so it will respect any local Git configuration set on your system, including
   204  credentials. To access a non-public Git repository, configure Git with
   205  suitable credentials for that repository.
   206  
   207  If you use the SSH protocol then any configured SSH keys will be used
   208  automatically. This is the most common way to access non-public Git
   209  repositories from automated systems because it allows access to private
   210  repositories without interactive prompts.
   211  
   212  If using the HTTP/HTTPS protocol, or any other protocol that uses
   213  username/password credentials, configure
   214  [Git Credentials Storage](https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage)
   215  to select a suitable source of credentials for your environment.
   216  
   217  If your Terraform configuration will be used within [Terraform Cloud](https://www.hashicorp.com/products/terraform),
   218  only SSH key authentication is supported, and
   219  [keys can be configured on a per-workspace basis](/docs/cloud/workspaces/ssh-keys.html).
   220  
   221  ### Selecting a Revision
   222  
   223  By default, Terraform will clone and use the default branch (referenced by
   224  `HEAD`) in the selected repository. You can override this using the
   225  `ref` argument:
   226  
   227  ```hcl
   228  module "vpc" {
   229    source = "git::https://example.com/vpc.git?ref=v1.2.0"
   230  }
   231  ```
   232  
   233  The value of the `ref` argument can be any reference that would be accepted
   234  by the `git checkout` command, including branch and tag names.
   235  
   236  ### "scp-like" address syntax
   237  
   238  When using Git over SSH, we recommend using the `ssh://`-prefixed URL form
   239  for consistency with all of the other URL-like git address forms.
   240  You may opt to use the alternative "scp-like" syntax instead, in which case you
   241  must omit the `ssh://` scheme part and include only the `git::` part.
   242  For example:
   243  
   244  ```hcl
   245  module "storage" {
   246    source = "git::username@example.com:storage.git"
   247  }
   248  ```
   249  
   250  
   251  If you use the `ssh://` URL scheme then Terraform will assume that the colon
   252  marks the beginning of a port number, rather than the beginning of the path.
   253  This matches how Git itself interprets these different forms, aside from
   254  the Terraform-specific `git::` selector prefix.
   255  
   256  ## Generic Mercurial Repository
   257  
   258  You can use arbitrary Mercurial repositories by prefixing the address with the
   259  special `hg::` prefix. After this prefix, any valid
   260  [Mercurial URL](https://www.mercurial-scm.org/repo/hg/help/urls)
   261  can be specified to select one of the protocols supported by Mercurial.
   262  
   263  ```hcl
   264  module "vpc" {
   265    source = "hg::http://example.com/vpc.hg"
   266  }
   267  ```
   268  
   269  Terraform installs modules from Mercurial repositories by running `hg clone`, and
   270  so it will respect any local Mercurial configuration set on your system,
   271  including credentials. To access a non-public repository, configure Mercurial
   272  with suitable credentials for that repository.
   273  
   274  If you use the SSH protocol then any configured SSH keys will be used
   275  automatically. This is the most common way to access non-public Mercurial
   276  repositories from automated systems because it allows access to private
   277  repositories without interactive prompts.
   278  
   279  If your Terraform configuration will be used within [Terraform Cloud](https://www.hashicorp.com/products/terraform),
   280  only SSH key authentication is supported, and
   281  [keys can be configured on a per-workspace basis](/docs/cloud/workspaces/ssh-keys.html).
   282  
   283  ### Selecting a Revision
   284  
   285  You can select a non-default branch or tag using the optional `ref` argument:
   286  
   287  ```hcl
   288  module "vpc" {
   289    source = "hg::http://example.com/vpc.hg?ref=v1.2.0"
   290  }
   291  ```
   292  
   293  ## HTTP URLs
   294  
   295  When you use an HTTP or HTTPS URL, Terraform will make a `GET` request to
   296  the given URL, which can return _another_ source address. This indirection
   297  allows using HTTP URLs as a sort of "vanity redirect" over a more complicated
   298  module source address.
   299  
   300  Terraform will append an additional query string argument `terraform-get=1` to
   301  the given URL before sending the `GET` request, allowing the server to
   302  optionally return a different result when Terraform is requesting it.
   303  
   304  If the response is successful (`200`-range status code), Terraform looks in
   305  the following locations in order for the next address to access:
   306  
   307  * The value of a response header field named `X-Terraform-Get`.
   308  
   309  * If the response is an HTML page, a `meta` element with the name `terraform-get`:
   310  
   311    ```html
   312    <meta name="terraform-get"
   313          content="github.com/hashicorp/example" />
   314    ```
   315  
   316  In either case, the result is interpreted as another module source address
   317  using one of the forms documented elsewhere on this page.
   318  
   319  If an HTTP/HTTPS URL requires authentication credentials, use a `.netrc`
   320  file in your home directory to configure these. For information on this format,
   321  see [the documentation for using it in `curl`](https://ec.haxx.se/usingcurl-netrc.html).
   322  
   323  ### Fetching archives over HTTP
   324  
   325  As a special case, if Terraform detects that the URL has a common file
   326  extension associated with an archive file format then it will bypass the
   327  special `terraform-get=1` redirection described above and instead just use
   328  the contents of the referenced archive as the module source code:
   329  
   330  ```hcl
   331  module "vpc" {
   332    source = "https://example.com/vpc-module.zip"
   333  }
   334  ```
   335  
   336  The extensions that Terraform recognizes for this special behavior are:
   337  
   338  * `zip`
   339  * `tar.bz2` and `tbz2`
   340  * `tar.gz` and `tgz`
   341  * `tar.xz` and `txz`
   342  
   343  If your URL _doesn't_ have one of these extensions but refers to an archive
   344  anyway, use the `archive` argument to force this interpretation:
   345  
   346  ```hcl
   347  module "vpc" {
   348    source = "https://example.com/vpc-module?archive=zip"
   349  }
   350  ```
   351  
   352  -> **Note:** If the content of the archive file is a directory, you will need to
   353  include that directory in the module source. Read the section on
   354  [Modules in Package Sub-directories](#modules-in-package-sub-directories) for more
   355  information.
   356  
   357  ## S3 Bucket
   358  
   359  You can use archives stored in S3 as module sources using the special `s3::`
   360  prefix, followed by
   361  [an S3 bucket object URL](http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingBucket.html#access-bucket-intro).
   362  
   363  ```hcl
   364  module "consul" {
   365    source = "s3::https://s3-eu-west-1.amazonaws.com/examplecorp-terraform-modules/vpc.zip"
   366  }
   367  ```
   368  
   369  -> **Note:** Buckets in AWS's us-east-1 region must use the hostname `s3.amazonaws.com` (instead of `s3-us-east-1.amazonaws.com`).
   370  
   371  The `s3::` prefix causes Terraform to use AWS-style authentication when
   372  accessing the given URL. As a result, this scheme may also work for other
   373  services that mimic the S3 API, as long as they handle authentication in the
   374  same way as AWS.
   375  
   376  The resulting object must be an archive with one of the same file
   377  extensions as for [archives over standard HTTP](#fetching-archives-over-http).
   378  Terraform will extract the archive to obtain the module source tree.
   379  
   380  The module installer looks for AWS credentials in the following locations,
   381  preferring those earlier in the list when multiple are available:
   382  
   383  * The `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables.
   384  * The default profile in the `.aws/credentials` file in your home directory.
   385  * If running on an EC2 instance, temporary credentials associated with the
   386    instance's IAM Instance Profile.
   387  
   388  ## GCS Bucket
   389  
   390  You can use archives stored in Google Cloud Storage as module sources using the special `gcs::`
   391  prefix, followed by
   392  [a GCS bucket object URL](https://cloud.google.com/storage/docs/request-endpoints#typical).
   393  
   394  For example
   395  
   396  * `gcs::https://www.googleapis.com/storage/v1/BUCKET_NAME/PATH_TO_MODULE`
   397  * `gcs::https://www.googleapis.com/storage/v1/BUCKET_NAME/PATH/TO/module.zip`
   398  
   399  ```hcl
   400  module "consul" {
   401    source = "gcs::https://www.googleapis.com/storage/v1/modules/foomodule.zip"
   402  }
   403  ```
   404  
   405  The module installer uses Google Cloud SDK to authenticate with GCS. To set credentials you can:
   406  
   407  * Enter the path of your service account key file in the GOOGLE_APPLICATION_CREDENTIALS environment variable, or;
   408  * 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
   409  * On your computer, you can make your Google identity available by running `gcloud auth application-default login`.
   410  
   411  
   412  ## Modules in Package Sub-directories
   413  
   414  When the source of a module is a version control repository or archive file
   415  (generically, a "package"), the module itself may be in a sub-directory relative
   416  to the root of the package.
   417  
   418  A special double-slash syntax is interpreted by Terraform to indicate that
   419  the remaining path after that point is a sub-directory within the package.
   420  For example:
   421  
   422  * `hashicorp/consul/aws//modules/consul-cluster`
   423  * `git::https://example.com/network.git//modules/vpc`
   424  * `https://example.com/network-module.zip//modules/vpc`
   425  * `s3::https://s3-eu-west-1.amazonaws.com/examplecorp-terraform-modules/network.zip//modules/vpc`
   426  
   427  If the source address has arguments, such as the `ref` argument supported for
   428  the version control sources, the sub-directory portion must be _before_ those
   429  arguments:
   430  
   431  * `git::https://example.com/network.git//modules/vpc?ref=v1.2.0`
   432  
   433  Terraform will still extract the entire package to local disk, but will read
   434  the module from the subdirectory. As a result, it is safe for a module in
   435  a sub-directory of a package to use [a local path](#local-paths) to another
   436  module as long as it is in the _same_ package.