github.com/jzbruno/terraform@v0.10.3-0.20180104230435-18975d727047/website/docs/modules/usage.html.markdown (about) 1 --- 2 layout: "docs" 3 page_title: "Using Modules" 4 sidebar_current: "docs-modules-usage" 5 description: Using modules in Terraform is very similar to defining resources. 6 --- 7 8 # Module Usage 9 10 Using child modules in Terraform is very similar to defining resources: 11 12 ```shell 13 module "consul" { 14 source = "hashicorp/consul/aws" 15 servers = 3 16 } 17 ``` 18 19 You can view the full documentation for configuring modules in the [Module Configuration](/docs/configuration/modules.html) section. 20 21 In modules we only specify a name, rather than a name and a type as for resources. 22 This name is used elsewhere in the configuration to reference the module and 23 its outputs. 24 25 The source tells Terraform what to create. In this example, we instantiate 26 the [Consul module for AWS](https://registry.terraform.io/modules/hashicorp/consul/aws) 27 from the [Terraform Registry](https://registry.terraform.io). Other source 28 types are supported, as described in the following section. 29 30 Just like a resource, a module's configuration can be deleted to destroy the 31 resources belonging to the module. 32 33 ## Source 34 35 The only required configuration key for a module is the `source` parameter. The 36 value of this tells Terraform where to download the module's source code. 37 Terraform comes with support for a variety of module sources. 38 39 The recommended source for external modules is a 40 [Terraform Registry](/docs/registry/index.html), which provides the full 41 capabilities of modules such as version constraints. 42 Registry modules are specified using a simple slash-separated path like the 43 `hashicorp/consul/aws` path used in the above example. The full source string 44 for each registry module can be found from the registry website. 45 46 Terraform also supports modules in local directories, identified by a relative 47 path starting with either `./` or `../`. Such local modules are useful to 48 organize code more complex repositories, and are described in more detail 49 in [_Creating Modules_](/docs/modules/create.html). 50 51 Finally, Terraform can download modules directly from various storage providers 52 and version control systems. These sources do not support versioning and other 53 registry benefits, but can be convenient for getting started when already 54 available within an organization. The full list of available sources 55 are documented in [the module sources documentation](/docs/modules/sources.html). 56 57 When a configuration uses modules, they must first be installed by running 58 [`terraform init`](/docs/commands/init.html): 59 60 ```shell 61 $ terraform init 62 ``` 63 64 This command will download any modules that haven't been updated already, 65 as well as performing other Terraform working directory initialization such 66 as installing providers. 67 68 By default the command will not check for available updates to already-installed 69 modules, but you can use the `-upgrade` option to check for available upgrades. 70 When version constraints are specified (as described in the following section) 71 a newer version will be used only if it is within the given constraint. 72 73 ## Module Versions 74 75 It is recommended to explicitly constrain the acceptable version numbers for 76 each external module so that upstream changes aren't automatically adopted, 77 since this may result in unexpected or unwanted changes changes. 78 79 The `version` attribute within the `module` block is used for this purpose: 80 81 ```shell 82 module "consul" { 83 source = "hashicorp/consul/aws" 84 version = "0.0.5" 85 86 servers = 3 87 } 88 ``` 89 90 The `version` attribute value may either be a single explicit version or 91 a version constraint expression. Constraint expressions use the following 92 syntax to specify a _range_ of versions that are acceptable: 93 94 * `>= 1.2.0`: version 1.2.0 or newer 95 * `<= 1.2.0`: version 1.2.0 or older 96 * `~> 1.2`: any non-beta patch release within the `1.2` range 97 * `>= 1.0.0, <= 2.0.0`: any version between 1.0.0 and 2.0.0 inclusive 98 99 When depending on third-party modules, references to specific versions are 100 recommended since this ensures that updates only happen when convenient to you. 101 102 For modules maintained within your organization, a version range strategy 103 may be appropriate if a semantic versioning methodology is used consistently 104 or if there is a well-defined release process that avoids unwanted updates. 105 106 Version constraints are supported only for modules installed from a module 107 registry, such as the [Terraform Registry](https://registry.terraform.io/). 108 Other module sources may provide their own versioning mechanisms within the 109 source string itself, or they may not support versions at all. In particular, 110 modules whose sources are local file paths do not support `version` because 111 they are constrained to share the same version as their caller by being 112 obtained by the same source repository. 113 114 ## Configuration 115 116 The arguments used in a `module` block, such as the `servers` parameter above, 117 correspond to [variables](/docs/configuration/variables.html) within the module 118 itself. You can therefore discover all the available variables for a module by 119 inspecting the source of it. 120 121 The special arguments `source`, `version` and `providers` are exceptions. These 122 are used for special purposes by Terraform and should therefore not be used 123 as variable names within a module. 124 125 ## Outputs 126 127 Modules encapsulate their resources. A resource in one module cannot directly depend on resources or attributes in other modules, unless those are exported through [outputs](/docs/configuration/outputs.html). These outputs can be referenced in other places in your configuration, for example: 128 129 ```hcl 130 resource "aws_instance" "client" { 131 ami = "ami-408c7f28" 132 instance_type = "t1.micro" 133 availability_zone = "${module.consul.server_availability_zone}" 134 } 135 ``` 136 137 This is deliberately very similar to accessing resource attributes. Instead of 138 referencing a resource attribute, however, the expression in this case 139 references an output of the module. 140 141 Just like with resources, interpolation expressions can create implicit 142 dependencies on resources and other modules. Since modules encapsulate 143 other resources, however, the dependency is not on the module as a whole 144 but rather on the `server_availability_zone` output specifically, which 145 allows Terraform to work on resources in different modules concurrently rather 146 than waiting for the entire module to be complete before proceeding. 147 148 ## Providers within Modules 149 150 In a configuration with multiple modules, there are some special considerations 151 for how resources are associated with provider configurations. 152 153 While in principle `provider` blocks can appear in any module, it is recommended 154 that they be placed only in the _root_ module of a configuration, since this 155 approach allows users to configure providers just once and re-use them across 156 all descendent modules. 157 158 Each resource in the configuration must be associated with one provider 159 configuration, which may either be within the same module as the resource 160 or be passed from the parent module. Providers can be passed down to descendent 161 modules in two ways: either _implicitly_ through inheritance, or _explicitly_ 162 via the `providers` argument within a `module` block. These two options are 163 discussed in more detail in the following sections. 164 165 In all cases it is recommended to keep explicit provider configurations only in 166 the root module and pass them (whether implicitly or explicitly) down to 167 descendent modules. This avoids the provider configurations from being "lost" 168 when descendent modules are removed from the configuration. It also allows 169 the user of a configuration to determine which providers require credentials 170 by inspecting only the root module. 171 172 Provider configurations are used for all operations on associated resources, 173 including destroying remote objects and refreshing state. Terraform retains, as 174 part of its state, a reference to the provider configuration that was most 175 recently used to apply changes to each resource. When a `resource` block is 176 removed from the configuration, this record in the state is used to locate the 177 appropriate configuration because the resource's `provider` argument (if any) 178 is no longer present in the configuration. 179 180 As a consequence, it is required that all resources created for a particular 181 provider configuration must be destroyed before that provider configuration is 182 removed, unless the related resources are re-configured to use a different 183 provider configuration first. 184 185 ### Implicit Provider Inheritance 186 187 For convenience in simple configurations, a child module automatically inherits 188 default (un-aliased) provider configurations from its parent. This means that 189 explicit `provider` blocks appear only in the root module, and downstream 190 modules can simply declare resources for that provider and have them 191 automatically associated with the root provider configurations. 192 193 For example, the root module might contain only a `provider` block and a 194 `module` block to instantiate a child module: 195 196 ```hcl 197 provider "aws" { 198 region = "us-west-1" 199 } 200 201 module "child" { 202 source = "./child" 203 } 204 ``` 205 206 The child module can then use any resource from this provider with no further 207 provider configuration required: 208 209 ```hcl 210 resource "aws_s3_bucket" "example" { 211 bucket = "provider-inherit-example" 212 } 213 ``` 214 215 This approach is recommended in the common case where only a single 216 configuration is needed for each provider across the entire configuration. 217 218 In more complex situations there may be [multiple provider instances](/docs/configuration/providers.html#multiple-provider-instances), 219 or a child module may need to use different provider settings than 220 its parent. For such situations, it's necessary to pass providers explicitly 221 as we will see in the next section. 222 223 ## Passing Providers Explicitly 224 225 When child modules each need a different configuration of a particular 226 provider, or where the child module requires a different provider configuration 227 than its parent, the `providers` argument within a `module` block can be 228 used to define explicitly which provider configs are made available to the 229 child module. For example: 230 231 ```hcl 232 # The default "aws" configuration is used for AWS resources in the root 233 # module where no explicit provider instance is selected. 234 provider "aws" { 235 region = "us-west-1" 236 } 237 238 # A non-default, or "aliased" configuration is also defined for a different 239 # region. 240 provider "aws" { 241 alias = "usw2" 242 region = "us-west-2" 243 } 244 245 # An example child module is instantiated with the _aliased_ configuration, 246 # so any AWS resources it defines will use the us-west-2 region. 247 module "example" { 248 source = "./example" 249 providers = { 250 aws = "aws.usw2" 251 } 252 } 253 ``` 254 255 The `providers` argument within a `module` block is similar to 256 the `provider` argument within a resource as described for 257 [multiple provider instances](/docs/configuration/providers.html#multiple-provider-instances), 258 but is a map rather than a single string because a module may contain resources 259 from many different providers. 260 261 Once the `providers` argument is used in a `module` block, it overrides all of 262 the default inheritance behavior, so it is necessary to enumerate mappings 263 for _all_ of the required providers. This is to avoid confusion and surprises 264 that may result when mixing both implicit and explicit provider passing. 265 266 Additional provider configurations (those with the `alias` argument set) are 267 _never_ inherited automatically by child modules, and so must always be passed 268 explicitly using the `providers` map. For example, a module 269 that configures connectivity between networks in two AWS regions is likely 270 to need both a source and a destination region. In that case, the root module 271 may look something like this: 272 273 ```hcl 274 provider "aws" { 275 alias = "usw1" 276 region = "us-west-1" 277 } 278 279 provider "aws" { 280 alias = "usw2" 281 region = "us-west-2" 282 } 283 284 module "tunnel" { 285 source = "./tunnel" 286 providers = { 287 "aws.src" = "aws.usw1" 288 "aws.dst" = "aws.usw2" 289 } 290 } 291 ``` 292 293 In the `providers` map, the keys are provider names as expected by the child 294 module, while the values are the names of corresponding configurations in 295 the _current_ module. The subdirectory `./tunnel` must then contain 296 _proxy configuration blocks_ like the following, to declare that it 297 requires configurations to be passed with these from the `providers` block in 298 the parent's `module` block: 299 300 ```hcl 301 provider "aws" { 302 alias = "src" 303 } 304 305 provider "aws" { 306 alias = "dst" 307 } 308 ``` 309 310 Each resource should then have its own `provider` attribute set to either 311 `"aws.src"` or `"aws.dst"` to choose which of the two provider instances to use. 312 313 At this time it is required to write an explicit proxy configuration block 314 even for default (un-aliased) provider configurations when they will be passed 315 via an explicit `providers` block: 316 317 ```hcl 318 provider "aws" { 319 } 320 ``` 321 322 If such a block is not present, the child module will behave as if it has no 323 configurations of this type at all, which may cause input prompts to supply 324 any required provider configuration arguments. This limitation will be 325 addressed in a future version of Terraform. 326 327 ## Multiple Instances of a Module 328 329 A particular module source can be instantiated multiple times: 330 331 ```hcl 332 # my_buckets.tf 333 334 module "assets_bucket" { 335 source = "./publish_bucket" 336 name = "assets" 337 } 338 339 module "media_bucket" { 340 source = "./publish_bucket" 341 name = "media" 342 } 343 ``` 344 345 ```hcl 346 # publish_bucket/bucket-and-cloudfront.tf 347 348 variable "name" {} # this is the input parameter of the module 349 350 resource "aws_s3_bucket" "example" { 351 # ... 352 } 353 354 resource "aws_iam_user" "deploy_user" { 355 # ... 356 } 357 ``` 358 359 This example defines a local child module in the `./publish_bucket` 360 subdirectory. That module has configuration to create an S3 bucket. The module 361 wraps the bucket and all the other implementation details required to configure 362 a bucket. 363 364 We can then instantiate the module multiple times in our configuration by 365 giving each instance a unique name -- here `module "assets_bucket"` and 366 `module "media_bucket"` -- whilst specifying the same `source` value. 367 368 Resources from child modules are prefixed with `module.<module-instance-name>` 369 when displayed in plan output and elsewhere in the UI. For example, the 370 `./publish_bucket` module contains `aws_s3_bucket.example`, and so the two 371 instances of this module produce S3 bucket resources with [_resource addresses_](/docs/internals/resource-addressing.html) 372 `module.assets_bucket.aws_s3_bucket.example` and `module.media_bucket.aws_s3_bucket.example` 373 respectively. These full addresses are used within the UI and on the command 374 line, but are not valid within interpolation expressions due to the 375 encapsulation behavior described above. 376 377 When refactoring an existing configuration to introduce modules, moving 378 resource blocks between modules causes Terraform to see the new location 379 as an entirely separate resource to the old. Always check the execution plan 380 after performing such actions to ensure that no resources are surprisingly 381 deleted. 382 383 Each instance of a module may optionally have different providers passed to it 384 using the `providers` argument described above. This can be useful in situations 385 where, for example, a duplicated set of resources must be created across 386 several regions or datacenters. 387 388 ## Summarizing Modules in the UI 389 390 By default, commands such as the [plan command](/docs/commands/plan.html) and 391 [graph command](/docs/commands/graph.html) will show each resource in a nested 392 module to represent the full scope of the configuration. For more complex 393 configurations, the `-module-depth` option may be useful to summarize some or all 394 of the modules as single objects. 395 396 For example, with a configuration similar to what we've built above, the default 397 graph output looks like the following: 398 399 ![Terraform Expanded Module Graph](docs/module_graph_expand.png) 400 401 If we instead set `-module-depth=0`, the graph will look like this: 402 403 ![Terraform Module Graph](docs/module_graph.png) 404 405 Other commands work similarly with modules. Note that `-module-depth` only 406 affects how modules are presented in the UI; it does not affect how modules 407 and their contained resources are processed by Terraform operations. 408 409 ## Tainting resources within a module 410 411 The [taint command](/docs/commands/taint.html) can be used to _taint_ specific 412 resources within a module: 413 414 ```shell 415 $ terraform taint -module=salt_master aws_instance.salt_master 416 ``` 417 418 It is not possible to taint an entire module. Instead, each resource within 419 the module must be tainted separately.