|
| 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)   block |
| 15 | + - [`config`](#config) map of arguments or nested blocks |
| 16 | + - [`PROVIDER ARGUMENTS`](#config) |
| 17 | + - [`for_each`](#for_each) map or set of strings | mutually exclusive with `count` |
| 18 | + - [`count`](#count) number | mutually exclusive with `for_each` |
| 19 | + - [`provider`](#provider) 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