github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/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](https://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  ```hcl
    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   = "us-east-1"
    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.  This module requires that your AWS account has a default VPC.)
    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 `-update` 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.aws_instance.server.0
    98  # ...
    99  + module.consul.aws_instance.server.1
   100  # ...
   101  + module.consul.aws_instance.server.2
   102  # ...
   103  + module.consul.aws_security_group.consul
   104  # ...
   105  
   106  Plan: 4 to add, 0 to change, 0 to destroy.
   107  ```
   108  
   109  Conceptually, the module is treated like a black box. In the plan, however
   110  Terraform shows each resource the module manages so you can see each detail
   111  about what the plan will do. If you'd like compressed plan output, you can
   112  specify the `-module-depth=` flag to get Terraform to output summaries by
   113  module.
   114  
   115  Next, run `terraform apply` to create the module. Note that as we warned above,
   116  the resources this module creates are outside of the AWS free tier, so this
   117  will have some cost associated with it.
   118  
   119  ```
   120  $ terraform apply
   121  # ...
   122  Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
   123  ```
   124  
   125  After a few minutes, you'll have a three server Consul cluster up and
   126  running! Without any knowledge of how Consul works, how to install Consul,
   127  or how to configure Consul into a cluster, you've created a real cluster in
   128  just minutes.
   129  
   130  ## Module Outputs
   131  
   132  Just as we parameterized the module with configurations such as
   133  `servers` above, modules can also output information (just like a resource).
   134  
   135  You'll have to reference the module's code or documentation to know what
   136  outputs it supports for now, but for this guide we'll just tell you that the
   137  Consul module has an output named `server_address` that has the address of
   138  one of the Consul servers that was setup.
   139  
   140  To reference this, we'll just put it into our own output variable. But this
   141  value could be used anywhere: in another resource, to configure another
   142  provider, etc.
   143  
   144  ```hcl
   145  output "consul_address" {
   146    value = "${module.consul.server_address}"
   147  }
   148  ```
   149  
   150  The syntax for referencing module outputs should be very familiar. The
   151  syntax is `${module.NAME.ATTRIBUTE}`. The `NAME` is the logical name
   152  we assigned earlier, and the `ATTRIBUTE` is the output attribute.
   153  
   154  If you run `terraform apply` again, Terraform should make no changes, but
   155  you'll now see the "consul\_address" output with the address of our Consul
   156  server.
   157  
   158  ## Next
   159  
   160  For more information on modules, the types of sources supported, how
   161  to write modules, and more, read the in depth
   162  [module documentation](/docs/modules/index.html).
   163  
   164  Next, we learn how to [use Terraform remotely and the associated benefits](/intro/getting-started/remote.html).