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