github.com/hobbeswalsh/terraform@v0.3.7-0.20150619183303-ad17cf55a0fa/website/source/intro/getting-started/modules.html.md (about)

     1  ---
     2  layout: "intro"
     3  page_title: "Modules"
     4  sidebar_current: "gettingstarted-modules"
     5  description: |-
     6    Up to this point, we've been configuring Terraform by editing Terraform configurations directly. As our infrastructure grows, this practice has a few key problems: a lack of organization, a lack of reusability, and difficulties in management for teams.
     7  ---
     8  
     9  # Modules
    10  
    11  Up to this point, we've been configuring Terraform by editing Terraform
    12  configurations directly. As our infrastructure grows, this practice has a few
    13  key problems: a lack of organization, a lack of reusability, and difficulties
    14  in management for teams.
    15  
    16  _Modules_ in Terraform are self-contained packages of Terraform configurations
    17  that are managed as a group. Modules are used to create reusable components,
    18  improve organization, and to treat pieces of infrastructure as a black box.
    19  
    20  This section of the getting started will cover the basics of using modules.
    21  Writing modules is covered in more detail in the
    22  [modules documentation](/docs/modules/index.html).
    23  
    24  ~> **Warning!** The examples on this page are _**not** eligible_ for the AWS
    25  [free-tier](http://aws.amazon.com/free/). Do not execute the examples on this
    26  page unless you're willing to spend a small amount of money.
    27  
    28  ## Using Modules
    29  
    30  If you have any instances running from prior steps in the getting
    31  started guide, use `terraform destroy` to destroy them, and remove all
    32  configuration files.
    33  
    34  As an example, we're going to use the
    35  [Consul Terraform module](https://github.com/hashicorp/consul/tree/master/terraform)
    36  which will setup a complete [Consul](https://www.consul.io) cluster
    37  for us.
    38  
    39  Create a configuration file with the following contents:
    40  
    41  ```
    42  provider "aws" {
    43  	access_key = "AWS ACCESS KEY"
    44  	secret_key = "AWS SECRET KEY"
    45  	region = "AWS REGION"
    46  }
    47  
    48  module "consul" {
    49  	source = "github.com/hashicorp/consul/terraform/aws"
    50  
    51  	key_name = "AWS SSH KEY NAME"
    52  	key_path = "PATH TO ABOVE PRIVATE KEY"
    53  	region = "AWS REGION"
    54  	servers = "3"
    55  }
    56  ```
    57  
    58  (Note that the `provider` block can be omitted in favor of environment
    59  variables. See the [AWS Provider docs](/docs/providers/aws/index.html)
    60  for details.)
    61  
    62  The `module` block tells Terraform to create and manage a module. It is
    63  very similar to the `resource` block. It has a logical name -- in this
    64  case "consul" -- and a set of configurations.
    65  
    66  The `source` configuration is the only mandatory key for modules. It tells
    67  Terraform where the module can be retrieved. Terraform automatically
    68  downloads and manages modules for you. For our example, we're getting the
    69  module directly from GitHub. Terraform can retrieve modules from a variety
    70  of sources including Git, Mercurial, HTTP, and file paths.
    71  
    72  The other configurations are parameters to our module. Please fill them
    73  in with the proper values.
    74  
    75  Prior to running any command such as `plan` with a configuration that
    76  uses modules, you'll have to [get](/docs/commands/get.html) the modules.
    77  This is done using the [get command](/docs/commands/get.html).
    78  
    79  ```
    80  $ terraform get
    81  ...
    82  ```
    83  
    84  This command will download the modules if they haven't been already.
    85  By default, the command will not check for updates, so it is safe (and fast)
    86  to run multiple times. You can use the `-u` flag to check and download
    87  updates.
    88  
    89  ## Planning and Apply Modules
    90  
    91  With the modules downloaded, we can now plan and apply it. If you run
    92  `terraform plan`, you should see output similar to below:
    93  
    94  ```
    95  $ terraform plan
    96  ...
    97  + module.consul
    98      4 resource(s)
    99  ```
   100  
   101  As you can see, the module is treated like a black box. In the plan, Terraform
   102  shows the module managed as a whole. It does not show what resources within
   103  the module will be created. If you care, you can see that by specifying
   104  a `-module-depth=-1` flag.
   105  
   106  Next, run `terraform apply` to create the module. Note that as we warned above,
   107  the resources this module creates are outside of the AWS free tier, so this
   108  will have some cost associated with it.
   109  
   110  ```
   111  $ terraform apply
   112  ...
   113  Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
   114  ```
   115  
   116  After a few minutes, you'll have a three server Consul cluster up and
   117  running! Without any knowledge of how Consul works, how to install Consul,
   118  or how to configure Consul into a cluster, you've created a real cluster in
   119  just minutes.
   120  
   121  ## Module Outputs
   122  
   123  Just as we parameterized the module with configurations such as
   124  `servers` above, modules can also output information (just like a resource).
   125  
   126  You'll have to reference the module's code or documentation to know what
   127  outputs it supports for now, but for this guide we'll just tell you that the
   128  Consul module has an output named `server_address` that has the address of
   129  one of the Consul servers that was setup.
   130  
   131  To reference this, we'll just put it into our own output variable. But this
   132  value could be used anywhere: in another resource, to configure another
   133  provider, etc.
   134  
   135  ```
   136  output "consul_address" {
   137  	value = "${module.consul.server_address}"
   138  }
   139  ```
   140  
   141  The syntax for referencing module outputs should be very familiar. The
   142  syntax is `${module.NAME.ATTRIBUTE}`. The `NAME` is the logical name
   143  we assigned earlier, and the `ATTRIBUTE` is the output attribute.
   144  
   145  If you run `terraform apply` again, Terraform should make no changes, but
   146  you'll now see the "consul\_address" output with the address of our Consul
   147  server.
   148  
   149  ## Next
   150  
   151  For more information on modules, the types of sources supported, how
   152  to write modules, and more, read the in depth
   153  [module documentation](/docs/modules/index.html).
   154  
   155  We've now concluded the getting started guide, however
   156  there are a number of [next steps](/intro/getting-started/next-steps.html)
   157  to get started with Terraform.