github.com/loicalbertin/terraform@v0.6.15-0.20170626182346-8e2583055467/website/docs/modules/sources.html.markdown (about)

     1  ---
     2  layout: "docs"
     3  page_title: "Module Sources"
     4  sidebar_current: "docs-modules-sources"
     5  description: Explains the use of the source parameter, which tells Terraform where modules can be found.
     6  ---
     7  
     8  # Module Sources
     9  
    10  As documented in the [Usage section](/docs/modules/usage.html), the only required parameter when using a module is `source`. The `source` parameter tells Terraform where the module can be found and what constraints to put on the module. Constraints can include a specific version or Git branch.
    11  
    12  Terraform manages modules for you: it downloads them, organizes them on disk, checks for updates, etc. Terraform uses this `source` parameter to determine where it should retrieve and update modules from.
    13  
    14  Terraform supports the following sources:
    15  
    16    * Local file paths
    17  
    18    * GitHub
    19  
    20    * Bitbucket
    21  
    22    * Generic Git, Mercurial repositories
    23  
    24    * HTTP URLs
    25  
    26    * S3 buckets
    27  
    28  Each is documented further below.
    29  
    30  ## Local File Paths
    31  
    32  The easiest source is the local file path. For maximum portability, this should be a relative file path into a subdirectory. This allows you to organize your Terraform configuration into modules within one repository, for example:
    33  
    34  ```hcl
    35  module "consul" {
    36    source = "./consul"
    37  }
    38  ```
    39  
    40  Updates for file paths are automatic: when "downloading" the module using the [get command](/docs/commands/get.html), Terraform will create a symbolic link to the original directory. Therefore, any changes are automatically available.
    41  
    42  ## GitHub
    43  
    44  Terraform will automatically recognize GitHub URLs and turn them into a link to the specific Git repository. The syntax is simple:
    45  
    46  ```hcl
    47  module "consul" {
    48    source = "github.com/hashicorp/example"
    49  }
    50  ```
    51  
    52  Subdirectories within the repository can also be referenced:
    53  
    54  ```hcl
    55  module "consul" {
    56    source = "github.com/hashicorp/example//subdir"
    57  }
    58  ```
    59  
    60  These will fetch the modules using HTTPS.  If you want to use SSH instead:
    61  
    62  ```hcl
    63  module "consul" {
    64    source = "git@github.com:hashicorp/example.git//subdir"
    65  }
    66  ```
    67  
    68  **Note:** The double-slash, `//`, is important. It is what tells Terraform that that is the separator for a subdirectory, and not part of the repository itself.
    69  
    70  GitHub source URLs require that Git is installed on your system and that you have access to the repository.
    71  
    72  You can use the same parameters to GitHub repositories as you can generic Git repositories (such as tags or branches). See the documentation for generic Git repositories for more information.
    73  
    74  ### Private GitHub Repos
    75  
    76  If you need Terraform to be able to fetch modules from private GitHub repos on a remote machine (like Terraform Enterprise or a CI server), you'll need to provide Terraform with credentials that can be used to authenticate as a user with read access to the private repo.
    77  
    78  First, create a [machine user](https://developer.github.com/guides/managing-deploy-keys/#machine-users) on GitHub with read access to the private repo in question, then embed this user's credentials into the `source` parameter:
    79  
    80  ```hcl
    81  module "private-infra" {
    82    source = "git::https://MACHINE-USER:MACHINE-PASS@github.com/org/privatemodules//modules/foo"
    83  }
    84  ```
    85  
    86  **Note:** Terraform does not yet support interpolations in the `source` field, so the machine username and password will have to be embedded directly into the `source` string. You can track [GH-1439](https://github.com/hashicorp/terraform/issues/1439) to learn when this limitation is addressed.
    87  
    88  ## Bitbucket
    89  
    90  Terraform will automatically recognize public Bitbucket URLs and turn them into a link to the specific Git or Mercurial repository, for example:
    91  
    92  ```hcl
    93  module "consul" {
    94    source = "bitbucket.org/hashicorp/consul"
    95  }
    96  ```
    97  
    98  Subdirectories within the repository can also be referenced:
    99  
   100  ```hcl
   101  module "consul" {
   102    source = "bitbucket.org/hashicorp/consul//subdir"
   103  }
   104  ```
   105  
   106  **Note:** The double-slash, `//`, is important. It is what tells Terraform that this is the separator for a subdirectory, and not part of the repository itself.
   107  
   108  Bitbucket URLs will require that Git or Mercurial is installed on your system, depending on the type of repository.
   109  
   110  ## Private Bitbucket Repos
   111  Private bitbucket repositories must be specified similar to the Generic Git Respository section below.
   112  
   113  ```hcl
   114  module "consul" {
   115    source = "git::https://bitbucket.org/foocompany/module_name.git
   116  }
   117  ```
   118  
   119  You can also specify branches and version withs the ?ref query
   120  
   121  ```hcl
   122  module "consul" {
   123    source = "git::https://bitbucket.org/foocompany/module_name.git?ref=hotfix
   124  }
   125  ```
   126  
   127  You will need to run a `terraform get -update=true` if you want to pull the latest versions. This can be handy when you are rapidly iterating on a module in development.
   128  
   129  ## Generic Git Repository
   130  
   131  Generic Git repositories are also supported. The value of `source` in this case should be a complete Git-compatible URL. Using generic Git repositories requires that Git is installed on your system.
   132  
   133  ```hcl
   134  module "consul" {
   135    source = "git://hashicorp.com/consul.git"
   136  }
   137  ```
   138  
   139  You can also use protocols such as HTTP or SSH to reference a module, but you'll have specify to Terraform that it is a Git module, by prefixing the URL with `git::` like so:
   140  
   141  ```hcl
   142  module "consul" {
   143    source = "git::https://hashicorp.com/consul.git"
   144  }
   145  
   146  module "ami" {
   147    source = "git::ssh://git@github.com/owner/repo.git"
   148  }
   149  ```
   150  
   151  If you do not specify the type of `source` then Terraform will attempt to use the closest match, for example assuming `https://hashicorp.com/consul.git` is a HTTP URL.
   152  
   153  The URLs for Git repositories support the following query parameters:
   154  
   155    * `ref` - The ref to checkout. This can be a branch, tag, commit, etc.
   156  
   157  ```hcl
   158  module "consul" {
   159    source = "git::https://hashicorp.com/consul.git?ref=master"
   160  }
   161  ```
   162  
   163  Terraform will cache the module locally by default `terraform get` is run, so successive updates to master or a specified branch will not be factored into future plans. Run `terraform get -update=true` to get the latest version of the branch. This is handy in development, but potentially bothersome in production if you don't have control of the repository.
   164  
   165  ## Generic Mercurial Repository
   166  
   167  Generic Mercurial repositories are supported. The value of `source` in this case should be a complete Mercurial-compatible URL. Using generic Mercurial repositories requires that Mercurial is installed on your system. You must tell Terraform that your `source` is a Mercurial repository by prefixing it with `hg::`.
   168  
   169  ```hcl
   170  module "consul" {
   171    source = "hg::http://hashicorp.com/consul.hg"
   172  }
   173  ```
   174  
   175  URLs for Mercurial repositories support the following query parameters:
   176  
   177    * `rev` - The rev to checkout. This can be a branch, tag, commit, etc.
   178  
   179  ```hcl
   180  module "consul" {
   181    source = "hg::http://hashicorp.com/consul.hg?rev=default"
   182  }
   183  ```
   184  
   185  ## HTTP URLs
   186  
   187  An HTTP or HTTPS URL can be used to redirect Terraform to get the module source from one of the other sources.  For HTTP URLs, Terraform will make a `GET` request to the given URL. An additional `GET` parameter, `terraform-get=1`, will be appended, allowing
   188  you to optionally render the page differently when Terraform is requesting it.
   189  
   190  Terraform then looks for the resulting module URL in the following order:
   191  
   192  1. Terraform will look to see if the header `X-Terraform-Get` is present. The header should contain the source URL of the actual module.
   193  
   194  2. Terraform will look for a `<meta>` tag with the name of `terraform-get`, for example:
   195  
   196  ```html
   197  <meta name="terraform-get” content="github.com/hashicorp/example" />
   198  ```
   199  
   200  ### S3 Bucket
   201  
   202  Terraform can also store modules in an S3 bucket. To access the bucket
   203  you must have appropriate AWS credentials in your configuration or
   204  available via shared credentials or environment variables.
   205  
   206  There are a variety of S3 bucket addressing schemes, most are
   207  [documented in the S3
   208  configuration](http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingBucket.html#access-bucket-intro).
   209  Here are a couple of examples.
   210  
   211  Using the `s3` protocol.
   212  
   213  ```hcl
   214  module "consul" {
   215    source = "s3::https://s3-eu-west-1.amazonaws.com/consulbucket/consul.zip"
   216  }
   217  ```
   218  
   219  Or directly using the bucket's URL.
   220  
   221  ```hcl
   222  module "consul" {
   223    source = "consulbucket.s3-eu-west-1.amazonaws.com/consul.zip"
   224  }
   225  ```
   226  
   227  
   228  ## Unarchiving
   229  
   230  Terraform will automatically unarchive files based on the extension of
   231  the file being requested (over any protocol). It supports the following
   232  archive formats:
   233  
   234  * tar.gz and tgz
   235  * tar.bz2 and tbz2
   236  * zip
   237  * gz
   238  * bz2