Skip to content

Commit e67fc0f

Browse files
committed
command: Change module-depth default to -1
This means that terraform commands like `plan`, `apply`, `show`, and `graph` will expand all modules by default. While modules-as-black-boxes is still very true in the conceptual design of modules, feedback on this behavior has consistently suggested that users would prefer to see more verbose output by default. The `-module-depth` flag and env var are retained to allow output to be optionally limited / summarized by these commands.
1 parent 2dff310 commit e67fc0f

File tree

12 files changed

+40
-29
lines changed

12 files changed

+40
-29
lines changed

command/graph.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ Options:
9999
This helps when diagnosing cycle errors.
100100
101101
-module-depth=n The maximum depth to expand modules. By default this is
102-
zero, which will not expand modules at all.
102+
-1, which will expand resources within all modules.
103103
104104
-verbose Generate a verbose, "worst-case" graph, with all nodes
105105
for potential operations in place.

command/meta.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -407,12 +407,17 @@ func (m *Meta) uiHook() *UiHook {
407407
}
408408

409409
const (
410-
// The name of the environment variable that can be used to set module depth.
410+
// ModuleDepthDefault is the default value for
411+
// module depth, which can be overridden by flag
412+
// or env var
413+
ModuleDepthDefault = -1
414+
415+
// ModuleDepthEnvVar is the name of the environment variable that can be used to set module depth.
411416
ModuleDepthEnvVar = "TF_MODULE_DEPTH"
412417
)
413418

414419
func (m *Meta) addModuleDepthFlag(flags *flag.FlagSet, moduleDepth *int) {
415-
flags.IntVar(moduleDepth, "module-depth", 0, "module-depth")
420+
flags.IntVar(moduleDepth, "module-depth", ModuleDepthDefault, "module-depth")
416421
if envVar := os.Getenv(ModuleDepthEnvVar); envVar != "" {
417422
if md, err := strconv.Atoi(envVar); err == nil {
418423
*moduleDepth = md

command/meta_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,12 @@ func TestMeta_addModuleDepthFlag(t *testing.T) {
244244
"invalid envvar is ignored": {
245245
EnvVar: "-#",
246246
Args: []string{},
247-
Expected: 0,
247+
Expected: ModuleDepthDefault,
248248
},
249249
"empty envvar is okay too": {
250250
EnvVar: "",
251251
Args: []string{},
252-
Expected: 0,
252+
Expected: ModuleDepthDefault,
253253
},
254254
}
255255

command/plan.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ Options:
181181
182182
-module-depth=n Specifies the depth of modules to show in the output.
183183
This does not affect the plan itself, only the output
184-
shown. By default, this is zero. -1 will expand all.
184+
shown. By default, this is -1, which will expand all.
185185
186186
-no-color If specified, output won't contain any color.
187187

command/show.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Usage: terraform show [options] [path]
118118
Options:
119119
120120
-module-depth=n Specifies the depth of modules to show in the output.
121-
By default this is zero. -1 will expand all.
121+
By default this is -1, which will expand all.
122122
123123
-no-color If specified, output won't contain any color.
124124

contrib/zsh-completion/_terraform

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ __get() {
4949

5050
__graph() {
5151
_arguments \
52-
'-module-depth=[(n) The maximum depth to expand modules. By default this is zero, which will not expand modules at all.]'
52+
'-module-depth=[(n) The maximum depth to expand modules. By default this is -1, which will expand all modules.]'
5353
}
5454

5555
__init() {
@@ -71,7 +71,7 @@ __plan() {
7171
'-backup=[(path) Path to backup the existing state file before modifying. Defaults to the "-state-out" path with" .backup" extension. Set to "-" to disable backup.]' \
7272
'-destroy[If set, a plan will be generated to destroy all resources managed by the given configuration and state.]' \
7373
'-input=[(true) Ask for input for variables if not directly set.]' \
74-
'-module-depth=[(n) Specifies the depth of modules to show in the output. This does not affect the plan itself, only the output shown. By default, this is zero. -1 will expand all.]' \
74+
'-module-depth=[(n) Specifies the depth of modules to show in the output. This does not affect the plan itself, only the output shown. By default, this is -1, which will expand all.]' \
7575
'-no-color[If specified, output will not contain any color.]' \
7676
'-out=[(path) Write a plan file to the given path. This can be used as input to the "apply" command.]' \
7777
'-refresh=[(true) Update state prior to checking for differences.]' \
@@ -121,7 +121,7 @@ __remote() {
121121

122122
__show() {
123123
_arguments \
124-
'-module-depth=[(n) The maximum depth to expand modules. By default this is zero, which will not expand modules at all.]' \
124+
'-module-depth=[(n) The maximum depth to expand modules. By default this is -1, which will expand all modules.]' \
125125
'-no-color[If specified, output will not contain any color.]'
126126
}
127127

website/source/docs/commands/graph.html.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Options:
3131
This helps when diagnosing cycle errors.
3232

3333
* `-module-depth=n` - The maximum depth to expand modules. By default this is
34-
zero, which will not expand modules at all.
34+
-1, which will expand all modules.
3535

3636
* `-verbose` - Generate a verbose, "worst-case" graph, with all nodes
3737
for potential operations in place.

website/source/docs/commands/plan.html.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ The command-line flags are all optional. The list of available flags are:
3939

4040
* `-module-depth=n` - Specifies the depth of modules to show in the output.
4141
This does not affect the plan itself, only the output shown. By default,
42-
this is zero. -1 will expand all.
42+
this is -1, which will expand all.
4343

4444
* `-no-color` - Disables output with coloring.
4545

website/source/docs/commands/show.html.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ file. If no path is specified, the current state will be shown.
2323
The command-line flags are all optional. The list of available flags are:
2424

2525
* `-module-depth=n` - Specifies the depth of modules to show in the output.
26-
By default this is zero. -1 will expand all.
26+
By default this is -1, which will expand all.
2727

2828
* `-no-color` - Disables output with coloring
2929

website/source/docs/configuration/environment-variables.html.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ export TF_INPUT=0
4444

4545
## TF_MODULE_DEPTH
4646

47-
When given a value, causes terraform commands to behave as if the `-module=depth=VALUE` flag was specified. Modules are treated like a black box and terraform commands do not show what resources within the module will be created. By setting this to -1, for example, you enable commands such as [plan](/docs/commands/plan.html) and [graph](/docs/commands/graph.html) to display more detailed information.
47+
When given a value, causes terraform commands to behave as if the `-module=depth=VALUE` flag was specified. By setting this to 0, for example, you enable commands such as [plan](/docs/commands/plan.html) and [graph](/docs/commands/graph.html) to display more compressed information.
4848

4949
```
50-
export TF_MODULE_DEPTH=-1
50+
export TF_MODULE_DEPTH=0
5151
```
5252

5353
For more information regarding modules, check out the section on [Using Modules](/docs/modules/usage.html).

website/source/docs/modules/usage.html.markdown

+5-7
Original file line numberDiff line numberDiff line change
@@ -104,28 +104,26 @@ resource to the module, so the module will be built first.
104104

105105
With modules, commands such as the [plan command](/docs/commands/plan.html)
106106
and
107-
[graph command](/docs/commands/graph.html) will show the module as a single
108-
unit by default. You can use the `-module-depth` parameter to expand this
109-
graph further.
107+
[graph command](/docs/commands/graph.html) will expand modules by default. You
108+
can use the `-module-depth` parameter to limit the graph.
110109

111110
For example, with a configuration similar to what we've built above, here
112111
is what the graph output looks like by default:
113112

114113
<div class="center">
115-
![Terraform Module Graph](docs/module_graph.png)
114+
![Terraform Expanded Module Graph](docs/module_graph_expand.png)
116115
</div>
117116

118-
But if we set `-module-depth=-1`, the graph will look like this:
117+
But if we set `-module-depth=0`, the graph will look like this:
119118

120119
<div class="center">
121-
![Terraform Expanded Module Graph](docs/module_graph_expand.png)
120+
![Terraform Module Graph](docs/module_graph.png)
122121
</div>
123122

124123
Other commands work similarly with modules. Note that the `-module-depth`
125124
flag is purely a formatting flag; it doesn't affect what modules are created
126125
or not.
127126

128-
129127
## Tainting resources within a module
130128

131129
The [taint command](/docs/commands/taint.html) can be used to _taint_

website/source/intro/getting-started/modules.html.md

+15-7
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module "consul" {
5050
5151
key_name = "AWS SSH KEY NAME"
5252
key_path = "PATH TO ABOVE PRIVATE KEY"
53-
region = "AWS REGION"
53+
region = "us-east-1"
5454
servers = "3"
5555
}
5656
```
@@ -94,14 +94,22 @@ With the modules downloaded, we can now plan and apply it. If you run
9494
```
9595
$ terraform plan
9696
...
97-
+ module.consul
98-
4 resource(s)
97+
+ module.consul.aws_instance.server.0
98+
...
99+
+ module.consul.aws_instance.server.1
100+
...
101+
+ module.consul.aws_instance.server.2
102+
...
103+
+ module.consul.aws_security_group.consul
104+
...
105+
Plan: 4 to add, 0 to change, 0 to destroy.
99106
```
100107

101-
As you can see, the module is treated like a black box. In the plan, Terraform
102-
shows the module managed as a whole. It does not show what resources within
103-
the module will be created. If you care, you can see that by specifying
104-
a `-module-depth=-1` flag.
108+
Conceptually, the module is treated like a black box. In the plan, however
109+
Terraform shows each resource the module manages so you can see each detail
110+
about what the plan will do. If you'd like compressed plan output, you can
111+
specify the `-module-depth=` flag to get Terraform to output summaries by
112+
module.
105113

106114
Next, run `terraform apply` to create the module. Note that as we warned above,
107115
the resources this module creates are outside of the AWS free tier, so this

0 commit comments

Comments
 (0)