github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/website/source/docs/modules/create.html.markdown (about)

     1  ---
     2  layout: "docs"
     3  page_title: "Creating Modules"
     4  sidebar_current: "docs-modules-create"
     5  description: |-
     6    Creating modules in Terraform is easy. You may want to do this to better organize your code, to make a reusable component, or just to learn more about Terraform. For any reason, if you already know the basics of Terraform, creating a module is a piece of cake.
     7  ---
     8  
     9  # Creating Modules
    10  
    11  Creating modules in Terraform is easy. You may want to do this to better
    12  organize your code, to make a reusable component, or just to learn more about
    13  Terraform. For any reason, if you already know the basics of Terraform,
    14  creating a module is a piece of cake.
    15  
    16  Modules in Terraform are just folders with Terraform files. In fact,
    17  when you run `terraform apply`, the current working directory holding
    18  the Terraform files you're applying comprise what is called the
    19  _root module_. It itself is a valid module.
    20  
    21  Therefore, you can enter the source of any module, run `terraform apply`,
    22  and expect it to work (assuming you satisfy the required variables, if any).
    23  
    24  ## An Example
    25  
    26  Within a folder containing Terraform configurations, create a subfolder
    27  "child". In this subfolder, make one empty "main.tf" file. Then, back in
    28  the root folder containing the "child" folder, add this to one of the
    29  Terraform files:
    30  
    31  ```
    32  module "child" {
    33  	source = "./child"
    34  }
    35  ```
    36  
    37  This will work. You've created your first module! You can add resources
    38  to the child module to see how that interaction works.
    39  
    40  ## Inputs/Outputs
    41  
    42  To make modules more useful than simple isolated containers of Terraform
    43  configurations, modules can be configured and also have outputs that can be
    44  consumed by the configuration using the module.
    45  
    46  Inputs of a module are [variables](/docs/configuration/variables.html)
    47  and outputs are [outputs](/docs/configuration/outputs.html). There is no
    48  special syntax to define these, they're defined just like any other
    49  variables or outputs.
    50  
    51  In the "child" module we created above, add the following:
    52  
    53  ```
    54  variable "memory" {}
    55  
    56  output "received" {
    57  	value = "${var.memory}"
    58  }
    59  ```
    60  
    61  This will create a required variable "memory" and then an output "received"
    62  that will simply be the value of the memory variable.
    63  
    64  You can then configure the module and use the output like so:
    65  
    66  ```
    67  module "child" {
    68  	source = "./child"
    69  
    70  	memory = "1G"
    71  }
    72  
    73  output "child_memory" {
    74  	value = "${module.child.received}"
    75  }
    76  ```
    77  
    78  If you run `apply`, you'll again see that this works.
    79  
    80  And that is all there is to it. Variables and outputs are used to configure
    81  modules and provide results. Resources within a module are isolated,
    82  and the whole thing is managed as a single unit.
    83  
    84  ## Paths and Embedded Files
    85  
    86  It is sometimes useful to embed files within the module that aren't
    87  Terraform configuration files, such as a script to provision a resource
    88  or a file to upload.
    89  
    90  In these cases, you can't use a relative path, since paths in Terraform
    91  are generally relative to the working directory that Terraform was executed
    92  from. Instead, you want to use a module-relative path. To do this, use
    93  the [path interpolated variables](/docs/configuration/interpolation.html).
    94  
    95  An example is shown below:
    96  
    97  ```
    98  resource "aws_instance" "server" {
    99  	...
   100  
   101  	provisioner "remote-exec" {
   102  		script = "${path.module}/script.sh"
   103  	}
   104  }
   105  ```
   106  
   107  In the above, we use `${path.module}` to get a module-relative path. This
   108  is usually what you'll want in any case.
   109  
   110  ## Nested Modules
   111  
   112  You can use a module within a module just like you would anywhere else.
   113  This module will be hidden from the root user, so you'll have re-expose any
   114  variables if you need to, as well as outputs.
   115  
   116  The [get command](/docs/commands/get.html) will automatically get all
   117  nested modules as well.
   118  
   119  You don't have to worry about conflicting versions of modules, since
   120  Terraform builds isolated subtrees of all dependencies. For example,
   121  one module might use version 1.0 of module "foo" and another module
   122  might use version 2.0 of module "foo", and this would all work fine
   123  within Terraform since the modules are created separately.