github.com/jzbruno/terraform@v0.10.3-0.20180104230435-18975d727047/website/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 25 [the AWS free tier](https://aws.amazon.com/free/). Do not try the examples 26 on this 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 The [Terraform Registry](https://registry.terraform.io/) includes a directory 35 of ready-to-use modules for various common purposes, which can serve as 36 larger building-blocks for your infrastructure. 37 38 In this example, we're going to use 39 [the Consul Terraform module for AWS](https://registry.terraform.io/modules/hashicorp/consul/aws), 40 which will set up a complete [Consul](https://www.consul.io) cluster. 41 This and other modules can found via the search feature on the Terraform 42 Registry site. 43 44 Create a configuration file with the following contents: 45 46 ```hcl 47 provider "aws" { 48 access_key = "AWS ACCESS KEY" 49 secret_key = "AWS SECRET KEY" 50 region = "us-east-1" 51 } 52 53 module "consul" { 54 source = "hashicorp/consul/aws" 55 56 aws_region = "us-east-1" # should match provider region 57 num_servers = "3" 58 } 59 ``` 60 61 The `module` block begins with the example given on the Terraform Registry 62 page for this module, telling Terraform to create and manage this module. 63 This is similar to a `resource` block: it has a name used within this 64 configuration -- in this case, `"consul"` -- and a set of input values 65 that are listed in 66 [the module's "Inputs" documentation](https://registry.terraform.io/modules/hashicorp/consul/aws?tab=inputs). 67 68 (Note that the `provider` block can be omitted in favor of environment 69 variables. See the [AWS Provider docs](/docs/providers/aws/index.html) 70 for details. This module requires that your AWS account has a default VPC.) 71 72 The `source` attribute is the only mandatory argument for modules. It tells 73 Terraform where the module can be retrieved. Terraform automatically 74 downloads and manages modules for you. 75 76 In this case, the module is retrieved from the official Terraform Registry. 77 Terraform can also retrieve modules from a variety of sources, including 78 private module registries or directly from Git, Mercurial, HTTP, and local 79 files. 80 81 The other attributes shown are inputs to our module. This module supports many 82 additional inputs, but all are optional and have reasonable values for 83 experimentation. 84 85 After adding a new module to configuration, it is necessary to run (or re-run) 86 `terraform init` to obtain and install the new module's source code: 87 88 ``` 89 $ terraform init 90 # ... 91 ``` 92 93 By default, this command does not check for new module versions that may be 94 available, so it is safe to run multiple times. The `-upgrade` option will 95 additionally check for any newer versions of existing modules and providers 96 that may be available. 97 98 ## Apply Changes 99 100 With the Consul module (and its dependencies) installed, we can now apply 101 these changes to create the resources described within. 102 103 If you run `terraform apply`, you will see a large list of all of the 104 resources encapsulated in the module. The output is similar to what we 105 saw when using resources directly, but the resource names now have 106 module paths prefixed to their names, like in the following example: 107 108 ``` 109 + module.consul.module.consul_clients.aws_autoscaling_group.autoscaling_group 110 id: <computed> 111 arn: <computed> 112 default_cooldown: <computed> 113 desired_capacity: "6" 114 force_delete: "false" 115 health_check_grace_period: "300" 116 health_check_type: "EC2" 117 launch_configuration: "${aws_launch_configuration.launch_configuration.name}" 118 max_size: "6" 119 metrics_granularity: "1Minute" 120 min_size: "6" 121 name: <computed> 122 protect_from_scale_in: "false" 123 tag.#: "2" 124 tag.2151078592.key: "consul-clients" 125 tag.2151078592.propagate_at_launch: "true" 126 tag.2151078592.value: "consul-example" 127 tag.462896764.key: "Name" 128 tag.462896764.propagate_at_launch: "true" 129 tag.462896764.value: "consul-example-client" 130 termination_policies.#: "1" 131 termination_policies.0: "Default" 132 vpc_zone_identifier.#: "6" 133 vpc_zone_identifier.1880739334: "subnet-5ce4282a" 134 vpc_zone_identifier.3458061785: "subnet-16600f73" 135 vpc_zone_identifier.4176925006: "subnet-485abd10" 136 vpc_zone_identifier.4226228233: "subnet-40a9b86b" 137 vpc_zone_identifier.595613151: "subnet-5131b95d" 138 vpc_zone_identifier.765942872: "subnet-595ae164" 139 wait_for_capacity_timeout: "10m" 140 ``` 141 142 The `module.consul.module.consul_clients` prefix shown above indicates 143 not only that the resource is from the `module "consul"` block we wrote, 144 but in fact that this module has its own `module "consul_clients"` block 145 within it. Modules can be nested to decompose complex systems into 146 managable components. 147 148 The full set of resources created by this module includes an autoscaling group, 149 security groups, IAM roles and other individual resources that all support 150 the Consul cluster that will be created. 151 152 Note that as we warned above, the resources created by this module are 153 not eligible for the AWS free tier and so proceeding further will have some 154 cost associated. To proceed with the creation of the Consul cluster, type 155 `yes` at the confirmation prompt. 156 157 ``` 158 # ... 159 160 module.consul.module.consul_clients.aws_security_group.lc_security_group: Creating... 161 description: "" => "Security group for the consul-example-client launch configuration" 162 egress.#: "" => "<computed>" 163 ingress.#: "" => "<computed>" 164 name: "" => "<computed>" 165 name_prefix: "" => "consul-example-client" 166 owner_id: "" => "<computed>" 167 revoke_rules_on_delete: "" => "false" 168 vpc_id: "" => "vpc-22099946" 169 170 # ... 171 172 Apply complete! Resources: 34 added, 0 changed, 0 destroyed. 173 ``` 174 175 After several minutes and many log messages about all of the resources 176 being created, you'll have a three-server Consul cluster up and running. 177 Without needing any knowledge of how Consul works, how to install Consul, 178 or how to form a Consul cluster, you've created a working cluster in just 179 a few minutes. 180 181 ## Module Outputs 182 183 Just as the module instance had input arguments such as `num_servers` above, 184 module can also produce _output_ values, similar to resource attributes. 185 186 [The module's outputs reference](https://registry.terraform.io/modules/hashicorp/consul/aws?tab=outputs) 187 describes all of the different values it produces. Overall, it exposes the 188 id of each of the resources it creates, as well as echoing back some of the 189 input values. 190 191 One of the supported outputs is called `asg_name_servers`, and its value 192 is the name of the auto-scaling group that was created to manage the Consul 193 servers. 194 195 To reference this, we'll just put it into our _own_ output value. This 196 value could actually be used anywhere: in another resource, to configure 197 another provider, etc. 198 199 Add the following to the end of the existing configuration file created 200 above: 201 202 ```hcl 203 output "consul_server_asg_name" { 204 value = "${module.consul.asg_name_servers}" 205 } 206 ``` 207 208 The syntax for referencing module outputs is `${module.NAME.OUTPUT}`, where 209 `NAME` is the module name given in the header of the `module` configuration 210 block and `OUTPUT` is the name of the output to reference. 211 212 If you run `terraform apply` again, Terraform will make no changes to 213 infrastructure, but you'll now see the "consul\_server\_asg\_name" output with 214 the name of the created auto-scaling group: 215 216 ``` 217 # ... 218 219 Apply complete! Resources: 0 added, 0 changed, 0 destroyed. 220 221 Outputs: 222 223 consul_server_asg_name = tf-asg-2017103123350991200000000a 224 ``` 225 226 If you look in the Auto-scaling Groups section of the EC2 console you should 227 find an autoscaling group of this name, and from there find the three 228 Consul servers it is running. (If you can't find it, make sure you're looking 229 in the right region!) 230 231 ## Destroy 232 233 Just as with top-level resources, we can destroy the resources created by 234 the Consul module to avoid ongoing costs: 235 236 ``` 237 $ terraform destroy 238 # ... 239 240 Terraform will perform the following actions: 241 242 - module.consul.module.consul_clients.aws_autoscaling_group.autoscaling_group 243 244 - module.consul.module.consul_clients.aws_iam_instance_profile.instance_profile 245 246 - module.consul.module.consul_clients.aws_iam_role.instance_role 247 248 # ... 249 ``` 250 251 As usual, Terraform describes all of the actions it will take. In this case, 252 it plans to destroy all of the resources that were created by the module. 253 Type `yes` to confirm and, after a few minutes and even more log output, 254 all of the resources should be destroyed: 255 256 ``` 257 Destroy complete! Resources: 34 destroyed. 258 ``` 259 260 With all of the resources destroyed, you can delete the configuration file 261 we created above. We will not make any further use of it, and so this avoids 262 the risk of accidentally re-creating the Consul cluster. 263 264 ## Next 265 266 For more information on modules, the types of sources supported, how 267 to write modules, and more, read the in-depth 268 [module documentation](/docs/modules/index.html). 269 270 Next, we learn about [Terraform's remote collaboration features](/intro/getting-started/remote.html).