Skip to content

Commit 610e2b1

Browse files
Atru/docs for 1.14 lang constructs (#947)
This PR adds docs for the `action` block and related updates for 1.14.
2 parents f32f1f2 + 28b115a commit 610e2b1

File tree

6 files changed

+408
-5
lines changed

6 files changed

+408
-5
lines changed

content/terraform/v1.14.x (alpha)/data/language-nav-data.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,10 @@
278278
{
279279
"title": "Configuration blocks",
280280
"routes": [
281+
{
282+
"title": "action",
283+
"path": "block/action"
284+
},
281285
{
282286
"title": "check",
283287
"path": "block/check"

content/terraform/v1.14.x (alpha)/docs/cli/commands/apply.mdx

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ description: The `terraform apply` command executes the actions proposed in a Te
66

77
# `terraform apply` command
88

9-
The `terraform apply` command executes the actions proposed in a Terraform
9+
The `terraform apply` command executes the operations proposed in a Terraform
1010
plan.
1111

1212
> **Hands On:** Try the [Apply Terraform Configuration](/terraform/tutorials/cli/apply?utm_source=WEBSITE&utm_medium=WEB_IO&utm_offer=ARTICLE_PAGE&utm_content=DOCS) tutorial to learn how Terraform applies a configuration, how Terraform recovers from errors during apply, and common ways to use this command.
@@ -17,7 +17,7 @@ Usage: `terraform apply [options] [plan file]`
1717

1818
### Automatic Plan Mode
1919

20-
When you run `terraform apply` without passing a saved plan file, Terraform automatically creates a new execution plan as if you had run [`terraform plan`](/terraform/cli/commands/plan), prompts you to approve that plan, and takes the indicated actions. You can use all of the [planning modes](/terraform/cli/commands/plan#planning-modes) and
20+
When you run `terraform apply` without passing a saved plan file, Terraform automatically creates a new execution plan as if you had run [`terraform plan`](/terraform/cli/commands/plan), prompts you to approve that plan, and performs the indicated operations. You can use all of the [planning modes](/terraform/cli/commands/plan#planning-modes) and
2121
[planning options](/terraform/cli/commands/plan#planning-options) to customize how Terraform will create the plan.
2222

2323
You can pass the `-auto-approve` option to instruct Terraform to apply the plan without asking for confirmation.
@@ -26,7 +26,7 @@ You can pass the `-auto-approve` option to instruct Terraform to apply the plan
2626

2727
### Saved Plan Mode
2828

29-
When you pass a [saved plan file](/terraform/cli/commands/plan#out-filename) to `terraform apply`, Terraform takes the actions in the saved plan without prompting you for confirmation. You may want to use this two-step workflow when [running Terraform in automation](/terraform/tutorials/automation/automate-terraform?utm_source=WEBSITE&utm_medium=WEB_IO&utm_offer=ARTICLE_PAGE&utm_content=DOCS).
29+
When you pass a [saved plan file](/terraform/cli/commands/plan#out-filename) to `terraform apply`, Terraform performs the operations in the saved plan without prompting you for confirmation. You may want to use this two-step workflow when [running Terraform in automation](/terraform/tutorials/automation/automate-terraform?utm_source=WEBSITE&utm_medium=WEB_IO&utm_offer=ARTICLE_PAGE&utm_content=DOCS).
3030

3131
Use [`terraform show`](/terraform/cli/commands/show) to inspect a saved plan file before applying it.
3232

@@ -62,6 +62,8 @@ The following options change how the apply command executes and reports on the a
6262
[Running Terraform in Automation](/terraform/tutorials/automation/automate-terraform?utm_source=WEBSITE&utm_medium=WEB_IO&utm_offer=ARTICLE_PAGE&utm_content=DOCS) for some
6363
different approaches.
6464

65+
- `-invoke=action.<TYPE>.<LABEL>`: Specifies a provider action defined in the configuration to invoke. Refer to the [`action` block reference](/terraform/language/block/action) for more information.
66+
6567
- `-json` - Enables the [machine readable JSON UI][machine-readable-ui] output.
6668
This implies `-input=false`, so the configuration must have no unassigned
6769
variable values to continue. To enable this flag, you must also either enable
@@ -86,8 +88,7 @@ The following options change how the apply command executes and reports on the a
8688
[walks the graph](/terraform/internals/graph#walking-the-graph). Defaults to
8789
10\.
8890

89-
- `-replace=resource` - Terraform will plan to replace this resource instance
90-
instead of doing an update or no-op action.
91+
- `-replace=resource` - Specifies a resource instance that Terraform plans to replace instead of performing an update or no-op operation.
9192

9293
- All [planning modes](/terraform/cli/commands/plan#planning-modes) and
9394
[planning options](/terraform/cli/commands/plan#planning-options) for
@@ -117,3 +118,37 @@ though the root module directory was overridden, use
117118
[the `TF_DATA_DIR` environment variable](/terraform/cli/config/environment-variables#tf_data_dir)
118119
to direct Terraform to write the `.terraform` directory to a location other
119120
than the current working directory.
121+
122+
## Examples
123+
124+
The following examples show how to use the `terraform apply` command for common use cases.
125+
126+
### Pass values to input variables
127+
128+
The following command sets the `env` input variable to `prod` at runtime:
129+
130+
```shell-session
131+
$ terraform apply -var='env=prod'
132+
```
133+
134+
The following command sets input variable values by using a local file named `my-vars.tfvars`:
135+
136+
```shell-session
137+
$ terraform apply -var-file='my-vars.tfvars'
138+
```
139+
140+
### Invoke actions
141+
142+
The following command targets the `aws_lambda_invoke` action configuration named `test`. As a result, Terraform invokes the action and ignores all other configurations:
143+
144+
```shell-session
145+
$ terraform apply -invoke='action.aws_lambda_invoke.test`
146+
```
147+
148+
You can use `for_each` and `count` [meta-arguments](/terraform/language/meta-arguments) in `action` configurations. These meta-arguments can invoke an action multiple times. Refer to the [`action` block reference](/terraform/language/block/action) for more information.
149+
150+
The following command invokes the `aws_lambda_invoke` action configuration named `test` one time:
151+
152+
```shell-session
153+
$ terraform apply -invoke='action.aws_lambda_invoke.test["first-member-of-a-set"]'
154+
```

content/terraform/v1.14.x (alpha)/docs/cli/commands/plan.mdx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ and later.
125125

126126
In addition to alternate [planning modes](#planning-modes), there are several options that can modify planning behavior. These options are available for both `terraform plan` and [`terraform apply`](/terraform/cli/commands/apply).
127127

128+
- `-invoke=action.<TYPE>.<LABEL>`: Specifies an action to create a plan for. When set, Terraform excludes all other configurations from the plan.
129+
128130
- `-refresh=false` - Disables the default behavior of synchronizing the
129131
Terraform state with remote objects before checking for configuration changes. This can make the planning operation faster by reducing the number of remote API requests. However, setting `refresh=false` causes Terraform to ignore external changes, which could result in an incomplete or incorrect plan. You cannot use `refresh=false` in refresh-only planning mode because it would effectively disable the entirety of the planning operation.
130132

@@ -363,3 +365,29 @@ though the root module directory was overridden, use
363365
[the `TF_DATA_DIR` environment variable](/terraform/cli/config/environment-variables#tf_data_dir)
364366
to direct Terraform to write the `.terraform` directory to a location other
365367
than the current working directory.
368+
369+
## Examples
370+
371+
The following examples show how to use the `terraform plan` command for common use cases.
372+
373+
### Pass values to input variables
374+
375+
The following command sets the `env` input variable to `prod` so that Terraform uses that value when it creates the plan:
376+
377+
```shell-session
378+
$ terraform plan -var='env=prod'
379+
```
380+
381+
The following command sets input variable values by using a local file named `my-vars.tfvars`:
382+
383+
```shell-session
384+
$ terraform plan -var-file='my-vars.tfvars'
385+
```
386+
387+
### Invoke actions
388+
389+
The following command creates a plan to invoke an `aws_lambda_invoke` action named `test`:
390+
391+
```shell-session
392+
$ terraform plan -invoke='action.aws_lambda_invoke.test'
393+
```
Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
---
2+
page_title: action block reference
3+
description: Learn how to configure action blocks in Terraform configuration so that you can invoke provider actions to perform day-2 operations
4+
---
5+
6+
# `action` block reference
7+
8+
The `action` block specifies a provider-defined action that you can invoke using the Terraform CLI or during an apply operation. Actions are operations included with providers that let Terraform perform day-two operations.
9+
10+
## Configuration model
11+
12+
The `action` block supports the following configuration:
13+
14+
- [`action "<TYPE>" "<LABEL>"`](#action) &nbsp block
15+
- [`config`](#config) &nbsp; map of arguments or nested blocks
16+
- [`PROVIDER ARGUMENTS`](#config)
17+
- [`for_each`](#for_each) &nbsp; map or set of strings | mutually exclusive with `count`
18+
- [`count`](#count) &nbsp; number | mutually exclusive with `for_each`
19+
- [`provider`](#provider) &nbsp; reference
20+
21+
## Complete configuration
22+
23+
The following `action` block includes all supported built-in arguments:
24+
25+
```hcl
26+
action "<TYPE>" "<LABEL>" {
27+
config {
28+
<PROVIDER ARGUMENTS>
29+
}
30+
for_each = { # `for_each` and `count` are mutually exclusive
31+
<KEY> = <VALUE>
32+
}
33+
for_each = [ # `for_each` accepts a map or a set of strings
34+
"<VALUE>",
35+
"<VALUE>"
36+
]
37+
count = <NUMBER> # `for_each` and `count` are mutually exclusive
38+
provider = <REFERENCE.TO.ALIAS>
39+
}
40+
```
41+
42+
## Specification
43+
44+
An `action` block supports the following configuration.
45+
46+
### `action "<TYPE>" "<LABEL>"`
47+
48+
You must set the following arguments for every `action` block:
49+
50+
- `TYPE`: Specifies the type of action to invoke. Refer to the provider documentation for information about the types of actions you can declare.
51+
- `LABEL`: Specifies a label for the `action` block. Terraform uses this label as part of the unique name for the action as configured by the block. The label does not affect the operation performed by Terraform when a user invokes the action. Refer to [References to Named Values](/terraform/language/expressions/references) and [Resource naming](/terraform/language/style#resource-naming) for expression syntax and label recommendations.
52+
53+
Use the `action.<TYPE>.<LABEL>` syntax to reference the action elsewhere in your configuration.
54+
55+
### `config`
56+
57+
The `config` block sets values for arguments or nested blocks defined by the provider.
58+
59+
```hcl
60+
action "<TYPE>" "<LABEL>" {
61+
config {
62+
<provider-specific arguments>
63+
}
64+
# . . .
65+
}
66+
```
67+
The `config` block may contain arguments, nested blocks, or a combination of both. Refer to the provider documentation for arguments you can configure in the `config` block for the type of action you want to invoke.
68+
69+
70+
#### Summary
71+
72+
- Data type: Map of arguments or nested blocks
73+
- Example: [Define an action](#define-an-action)
74+
75+
### `for_each`
76+
77+
The `for_each` meta-argument instructs Terraform to invoke the action once for each member of a list or key-value pair in a map.
78+
79+
<Tabs>
80+
81+
<Tab heading="List of values">
82+
83+
```hcl
84+
action "<TYPE>" "<LABEL>" {
85+
for_each = [ "<VALUE>" ]
86+
# . . .
87+
}
88+
```
89+
90+
</Tab>
91+
92+
<Tab heading="Map of key-value pairs">
93+
94+
```hcl
95+
action "<TYPE>" "<LABEL>" {
96+
for_each = {
97+
"<KEY>" = "<VALUE>"
98+
}
99+
}
100+
```
101+
102+
</Tab>
103+
104+
</Tabs>
105+
106+
The `for_each` meta-argument accepts a map or a set of strings and invokes the action once for each item in that map or set. Each invocation is associated with a distinct infrastructure object.
107+
108+
You can use pure functions, such as `toset()` and `tomap()`, to create a map or set for use in the `for_each` argument. All values must be known when Terraform plans your action, whether iterating over the keys of a map or set of strings. Otherwise, Terraform prints an error message that `for_each` has dependencies that it cannot determine before applying the configuration.
109+
110+
Keys in the `for_each` argument cannot be the result of or rely on the result of impure functions, such as `uuid`, `bcrypt`, or `timestamp`, because Terraform defers evaluating impure functions during the main evaluation step.
111+
112+
The `for_each` argument does not implicitly convert lists or tuples to sets. To declare resource instances based on a nested data structure or combinations of elements from multiple data structures, you can use Terraform expressions and functions to derive a suitable value. Refer to the following examples for more information:
113+
114+
- [Transform a multi-level nested structure into a flat list](/terraform/language/functions/flatten#flattening-nested-structures-for-for_each).
115+
- [Combine collections to produce a list of element combinations](/terraform/language/functions/setproduct#finding-combinations-for-for_each).
116+
117+
You cannot use sensitive values, such as [sensitive input variables](/terraform/language/block/variable#sensitive), [sensitive outputs](/terraform/language/block/output#sensitive), or [sensitive resource attributes](/terraform/language/expressions/references#sensitive-resource-attributes), as arguments in `for_each`. Because Terraform uses the value in `for_each` to identify the action instance and always discloses it in UI output, sensitive values are not allowed. Terraform returns an error if you attempt to use sensitive values as `for_each` arguments.
118+
119+
If you transform a value containing sensitive data into an argument for use in `for_each`, be aware that most functions in Terraform will return a sensitive result if given an argument with sensitive content. In many cases, you can achieve similar results with a `for` expression. For example, to call `keys(local.map)` where `local.map` is an object with sensitive values, but non-sensitive keys, you can create a value to pass to `for_each` using `toset([for k,v in local.map : k])`.
120+
121+
Refer to [Manage sensitive data](/terraform/language/manage-sensitive-data) for more information.
122+
123+
The `for_each` argument exposes an `each` object that you can reference within the same block to modify specific instances of the action. The object has the following attributes:
124+
125+
- `each.key`: Map key or list member that corresponds to an instance.
126+
- `each.value`: Map value that corresponds to an instance.
127+
128+
Use the `<TYPE>.<NAME>[<KEY>]` syntax to access an instance of a resource created using `for_each`. For example, `aws_lambda_invoke.server["a_group"]` refers to an invocation of the `aws_lambda_invoke` action named `server` from a key named `a_group`.
129+
130+
The `for_each` argument is a meta-argument, which is built into Terraform and controls the way that Terraform creates resources. Refer to [Meta-arguments](/terraform/language/meta-arguments) for more information.
131+
132+
#### Summary
133+
134+
- Data type: Map or set of strings.
135+
- Default: None.
136+
- Example: [Invoke an action multiple times](#invoke-an-action-multiple-times).
137+
138+
### `count`
139+
140+
The `count` meta-argument instructs Terraform to invoke an action multiple times using the same configuration.
141+
142+
```hcl
143+
resource {
144+
count = <number>
145+
}
146+
```
147+
148+
The value must be a whole number. You can reference variables or local values and use expressions to compute the value, but the value must resolve to a whole number.
149+
150+
In blocks where `count` is set, Terraform exposes an additional `count` object. You can reference the object to modify the configuration of each invocation. The `count` object has an `index` attribute starting from `0`.
151+
152+
To refer to an individual instance of an invocation using the `count` meta-argument, use the `<TYPE>.<NAME>[INDEX]` syntax. For example, `aws_lambda_invoke.server[0]` refers to the first invocation of the `aws_lambda_invoke` action named `server`.
153+
154+
<Tip>
155+
156+
You can use the `count` argument as a conditional. For example, setting a `count = var.creator ? 3 : 0` instructs Terraform to invoke an action three times when a variable named `creator` is set to `true`. When `creator` is set to `false`, Terraform does not invoke the action. Refer to [​​Conditional Expressions](/terraform/language/expressions/conditionals) for more information.
157+
158+
</Tip>
159+
160+
The `count` argument is a meta-argument, which is built into Terraform and controls the way that Terraform creates resources. Refer to [Meta-arguments](/terraform/language/meta-arguments) for more information.
161+
162+
#### Summary
163+
164+
- Data type: Number.
165+
- Default: None.
166+
- Example: [Invoke an action multiple times](#invoke-an-action-multiple-times).
167+
168+
### `provider`
169+
170+
The `provider` argument instructs Terraform to use an alternate provider configuration to invoke the action.
171+
172+
```hcl
173+
action "<TYPE>" "<LABEL>" {
174+
provider = <provider>.<alias>
175+
}
176+
```
177+
178+
By default, Terraform automatically selects a provider based on the action type, but you can create multiple provider configurations and use a non-default configuration for specific actions.
179+
180+
Use the `<PROVIDER>.<ALIAS>` syntax to reference a provider configuration in the `provider` argument.
181+
182+
The `provider` argument is a meta-argument, which is built into Terraform and controls the way that Terraform creates resources. Refer to [Meta-arguments](/terraform/language/meta-arguments) for more information.
183+
184+
#### Summary
185+
186+
- Data type: Reference.
187+
- Default: None.
188+
- Example: [Select an alternate provider configuration](#select-an-alternate-provider-configuration).
189+
190+
## Examples
191+
192+
The following examples show how to write configuration for common use cases.
193+
194+
### Define an action
195+
196+
The following example adds an `aws_lambda_invoke` action. You can invoke an action using the CLI or configure Terraform to invoke the action during a resource lifecycle state.
197+
198+
```hcl
199+
action "aws_lambda_invoke" "example" {
200+
config {
201+
values = var.values
202+
timeout = 3000
203+
}
204+
}
205+
```
206+
207+
### Select an alternate provider configuration
208+
209+
The following example configures Terraform to apply the `aws.alias` provider configuration when invoking the action:
210+
211+
```hcl
212+
action "aws_lambda_invocation" "example" {
213+
provider = aws.alias
214+
config {
215+
input = count.index
216+
timeout = 3000
217+
}
218+
}
219+
```
220+
221+
### Invoke an action multiple times
222+
223+
You can add a `count` or `for_each` meta-argument to invoke an action multiple times.
224+
225+
<Tabs>
226+
227+
<Tab heading="count">
228+
229+
Terraform invokes the action three times.
230+
231+
```hcl
232+
action "aws_lambda_invocation" "example" {
233+
count = 3
234+
config {
235+
input = count.index
236+
timeout = 3000
237+
}
238+
}
239+
```
240+
241+
</Tab>
242+
243+
<Tab heading="for_each">
244+
245+
Terraform invokes the action once for each member of the map, resulting in two invocations.
246+
247+
```hcl
248+
action "aws_lambda_invocation" "example" {
249+
for_each = tomap({
250+
a_group = "eastus"
251+
another_group = "westus2"
252+
})
253+
config {
254+
input = count.index
255+
timeout = 3000
256+
}
257+
}
258+
```
259+
260+
</Tab>
261+
262+
</Tabs>

0 commit comments

Comments
 (0)