terraform/website/source/docs/modules/sources.html.markdown

168 lines
6.1 KiB
Markdown
Raw Normal View History

2014-09-26 23:23:38 +02:00
---
layout: "docs"
page_title: "Module Sources"
sidebar_current: "docs-modules-sources"
description: Explains the use of the source parameter which tells Terraform where the module can be found.
2014-09-26 23:23:38 +02:00
---
# Module Sources
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.
2014-09-26 23:23:38 +02:00
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.
2014-09-26 23:23:38 +02:00
Terraform supports the following sources:
* Local file paths
* GitHub
* BitBucket
* Generic Git, Mercurial repositories
* HTTP URLs
Each is documented further below.
## Local File Paths
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:
2014-09-26 23:23:38 +02:00
```
module "consul" {
source = "./consul"
}
```
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.
2014-09-26 23:23:38 +02:00
## GitHub
Terraform will automatically recognize GitHub URLs and turn them into a link to the specific Git repository. The syntax is simple:
2014-09-26 23:23:38 +02:00
```
module "consul" {
source = "github.com/hashicorp/example"
}
```
Subdirectories within the repository can also be referenced:
```
module "consul" {
2014-09-26 23:58:41 +02:00
source = "github.com/hashicorp/example//subdir"
2014-09-26 23:23:38 +02:00
}
```
**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.
2014-09-26 23:58:41 +02:00
GitHub source URLs require that Git is installed on your system and that you have access to the repository.
2014-09-26 23:23:38 +02:00
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.
2014-09-26 23:23:38 +02:00
### Private GitHub Repos
If you need Terraform to be able to fetch modules from private GitHub repos on a remote machine (like Atlas 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.
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:
```
module "private-infra" {
source = "git::https://MACHINE-USER:MACHINE-PASS@github.com/org/privatemodules//modules/foo"
}
```
**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.
2014-09-26 23:23:38 +02:00
## BitBucket
Terraform will automatically recognize BitBucket URLs and turn them into a link to the specific Git or Mercurial repository, for example:
2014-09-26 23:23:38 +02:00
```
module "consul" {
source = "bitbucket.org/hashicorp/consul"
2014-09-26 23:23:38 +02:00
}
```
Subdirectories within the repository can also be referenced:
```
module "consul" {
source = "bitbucket.org/hashicorp/consul//subdir"
2014-09-26 23:23:38 +02:00
}
```
**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.
2014-09-26 23:58:41 +02:00
BitBucket URLs will require that Git or Mercurial is installed on your system, depending on the type of repository.
2014-09-26 23:23:38 +02:00
## Generic Git Repository
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.
2014-09-26 23:23:38 +02:00
```
module "consul" {
source = "git://hashicorp.com/consul.git"
2014-09-26 23:23:38 +02:00
}
```
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:
2014-09-26 23:23:38 +02:00
```
module "consul" {
source = "git::https://hashicorp.com/consul.git"
2014-09-26 23:23:38 +02:00
}
module "ami" {
source = "git::ssh://git@github.com/owner/repo.git"
}
2014-09-26 23:23:38 +02:00
```
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.
2014-09-26 23:23:38 +02:00
The URLs for Git repositories support the following query parameters:
2014-09-26 23:23:38 +02:00
* `ref` - The ref to checkout. This can be a branch, tag, commit, etc.
2014-09-26 23:23:38 +02:00
```
module "consul" {
source = "git::https://hashicorp.com/consul.git?ref=master"
2014-09-26 23:23:38 +02:00
}
```
## Generic Mercurial Repository
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::`.
2014-09-26 23:23:38 +02:00
```
module "consul" {
source = "hg::http://hashicorp.com/consul.hg"
2014-09-26 23:23:38 +02:00
}
```
URLs for Mercurial repositories support the following query parameters:
2014-09-26 23:23:38 +02:00
* `rev` - The rev to checkout. This can be a branch, tag, commit, etc.
```
module "consul" {
source = "hg::http://hashicorp.com/consul.hg?ref=master"
}
```
2014-09-26 23:23:38 +02:00
## HTTP URLs
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
2014-09-26 23:23:38 +02:00
you to optionally render the page differently when Terraform is requesting it.
Terraform then looks for the resulting module URL in the following order:
2014-09-26 23:23:38 +02:00
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.
2. Terraform will look for a `<meta>` tag with the name of `terraform-get`, for example:
```
<meta name=“terraform-get” content="github.com/hashicorp/example" />
```