github.com/paybyphone/terraform@v0.9.5-0.20170613192930-9706042ddd51/website/docs/modules/create.html.markdown (about) 1 --- 2 layout: "docs" 3 page_title: "Creating Modules" 4 sidebar_current: "docs-modules-create" 5 description: How to create modules. 6 --- 7 8 # Creating Modules 9 10 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, then creating a module is a piece of cake. 11 12 Modules in Terraform are folders with Terraform files. In fact, when you run `terraform apply`, the current working directory holding 13 the Terraform files you're applying comprise what is called the _root module_. This itself is a valid module. 14 15 Therefore, you can enter the source of any module, satisfy any required variables, run `terraform apply`, and expect it to work. 16 17 ## An Example Module 18 19 Within a folder containing Terraform configurations, create a subfolder called `child`. In this subfolder, make one empty `main.tf` file. Then, back in the root folder containing the `child` folder, add this to one of your Terraform configuration files: 20 21 ```hcl 22 module "child" { 23 source = "./child" 24 } 25 ``` 26 27 You've now created your first module! You can now add resources to the `child` module. 28 29 **Note:** Prior to running the above, you'll have to run [the get command](/docs/commands/get.html) for Terraform to sync 30 your modules. This should be instant since the module is a local path. 31 32 ## Inputs/Outputs 33 34 To make modules more useful than simple isolated containers of Terraform configurations, modules can be configured and also have outputs that can be consumed by your Terraform configuration. 35 36 Inputs of a module are [variables](/docs/configuration/variables.html) and outputs are [outputs](/docs/configuration/outputs.html). There is no special syntax to define these, they're defined just like any other variables or outputs. You can think about these variables and outputs as the API interface to your module. 37 38 Let's add a variable and an output to our `child` module. 39 40 ```hcl 41 variable "memory" {} 42 43 output "received" { 44 value = "${var.memory}" 45 } 46 ``` 47 48 This will create a required variable, `memory`, and then an output, `received`, that will be the value of the `memory` variable. 49 50 You can then configure the module and use the output like so: 51 52 ```hcl 53 module "child" { 54 source = "./child" 55 56 memory = "1G" 57 } 58 59 output "child_memory" { 60 value = "${module.child.received}" 61 } 62 ``` 63 64 If you now run `terraform apply`, you see how this works. 65 66 ## Paths and Embedded Files 67 68 It is sometimes useful to embed files within the module that aren't Terraform configuration files, such as a script to provision a resource or a file to upload. 69 70 In these cases, you can't use a relative path, since paths in Terraform are generally relative to the working directory from which Terraform was executed. Instead, you want to use a module-relative path. To do this, you should use the [path interpolated variables](/docs/configuration/interpolation.html). 71 72 ```hcl 73 resource "aws_instance" "server" { 74 # ... 75 76 provisioner "remote-exec" { 77 script = "${path.module}/script.sh" 78 } 79 } 80 ``` 81 82 Here we use `${path.module}` to get a module-relative path. 83 84 ## Nested Modules 85 86 You can nest a module within another module. This module will be hidden from your root configuration, so you'll have to re-expose any 87 variables and outputs you require. 88 89 The [get command](/docs/commands/get.html) will automatically get all nested modules. 90 91 You don't have to worry about conflicting versions of modules, since Terraform builds isolated subtrees of all dependencies. For example, one module might use version 1.0 of module `foo` and another module might use version 2.0, and this will all work fine within Terraform since the modules are created separately.