mirror of
https://github.com/opentffoundation/opentf.git
synced 2025-12-19 17:59:05 -05:00
[v1.11 Backport] Small tweaks to documentation (#3578)
Signed-off-by: James Humphries <james@james-humphries.co.uk>
This commit is contained in:
@@ -7,11 +7,11 @@ description: >-
|
||||
# The `enabled` Meta-Argument
|
||||
|
||||
:::note
|
||||
A given resource or module block cannot use `enabled` together with `count` or `for_each`.
|
||||
A given resource or module block cannot use `enabled` together with `count` or `for_each`. See [Restrictions](#restrictions) for more details.
|
||||
:::
|
||||
|
||||
The `enabled` meta-argument in a `lifecycle` block provides a cleaner way to conditionally create or skip a
|
||||
single resource or module instance. Unlike the traditional `count = var.enabled ? 1 : 0`
|
||||
The `enabled` meta-argument, introduced in OpenTofu v1.11, provides a cleaner way to conditionally create or skip a
|
||||
single resource or module instance in a `lifecycle` block. Unlike the traditional `count = var.enabled ? 1 : 0`
|
||||
workaround, which requires array indexing to access single instances, `enabled` allows
|
||||
direct access when the resource is created and properly handles the null state when disabled.
|
||||
|
||||
@@ -36,6 +36,58 @@ resource "aws_instance" "example" {
|
||||
When `enabled` is `true` (or not specified), the resource behaves normally. When `enabled`
|
||||
is `false`, the resource is not created and evaluates to `null`.
|
||||
|
||||
## Migrating from count
|
||||
|
||||
If you have existing resources using the `count = var.enabled ? 1 : 0` pattern,
|
||||
you can migrate to `enabled` without manual state manipulation.
|
||||
|
||||
After migration, you no longer need to reference your resource using the `[0]`
|
||||
instance key. Instead of `aws_instance.example[0]`, you can simply use
|
||||
`aws_instance.example`. OpenTofu automatically handles the state migration
|
||||
from the indexed instance to the non-indexed instance.
|
||||
|
||||
### Before
|
||||
|
||||
```hcl
|
||||
variable "create_instance" {
|
||||
type = bool
|
||||
default = true
|
||||
}
|
||||
|
||||
resource "aws_instance" "example" {
|
||||
count = var.create_instance ? 1 : 0
|
||||
# ...
|
||||
}
|
||||
|
||||
output "instance_id" {
|
||||
value = length(aws_instance.example) > 0 ? aws_instance.example[0].id : "not-created"
|
||||
}
|
||||
```
|
||||
|
||||
### After
|
||||
|
||||
```hcl
|
||||
variable "create_instance" {
|
||||
type = bool
|
||||
default = true
|
||||
}
|
||||
|
||||
resource "aws_instance" "example" {
|
||||
# ...
|
||||
lifecycle {
|
||||
enabled = var.create_instance
|
||||
}
|
||||
}
|
||||
|
||||
output "instance_id" {
|
||||
value = aws_instance.example != null ? aws_instance.example.id : "not-created"
|
||||
}
|
||||
```
|
||||
|
||||
When you apply this change, OpenTofu will automatically move your existing
|
||||
resource from `aws_instance.example[0]` to `aws_instance.example`. No `moved`
|
||||
block or manual state manipulation is required.
|
||||
|
||||
## Accessing Disabled Resources
|
||||
|
||||
When a resource might be disabled, it evaluates to `null` and cannot be accessed directly.
|
||||
|
||||
@@ -272,8 +272,7 @@ random_pet.animal: Creation complete after 0s [id=jae-known-mongoose]
|
||||
### Ephemerality
|
||||
[inpage-ephemeral]: #ephemeral-variables
|
||||
|
||||
If a variable is set to `ephemeral`, the module author will limit its usage
|
||||
only to the allowed ephemeral contexts:
|
||||
Setting a variable as `ephemeral` restricts its usage to the following ephemeral contexts:
|
||||
* [Ephemeral Resources](../ephemerality/ephemeral-resources.mdx)
|
||||
* [Ephemeral Variables](../values/variables.mdx#ephemerality)
|
||||
* [Ephemeral Outputs](../values/outputs.mdx#ephemerality)
|
||||
@@ -283,8 +282,8 @@ only to the allowed ephemeral contexts:
|
||||
* [Resource `connection` blocks](../resources/provisioners/connection.mdx#ephemeral-usage)
|
||||
* [Resource `write-only` attributes](../ephemerality/write-only-attributes.mdx)
|
||||
|
||||
OpenTofu will not store this type of variables in the [state](../../language/state/index.mdx) at all.
|
||||
These will be saved in the plan only by their name for further processing during apply.
|
||||
OpenTofu will not store ephemeral variable values in the [state](../../language/state/index.mdx) at all.
|
||||
Only the variable names will be saved in the plan for further processing during apply.
|
||||
|
||||
Declare a variable as ephemeral by setting the `ephemeral` argument to `true`:
|
||||
```hcl
|
||||
@@ -342,7 +341,7 @@ the collection or structure itself is not null.
|
||||
[inpage-deprecated]: #marking-variable-as-deprecated
|
||||
|
||||
The `deprecated` argument in a variable block indicates its deprecation and potential
|
||||
removal in the future. This attribute should contain non-empty string and should provide
|
||||
removal in the future. This attribute should contain a non-empty string and should provide
|
||||
instructions on how to migrate away from usage of this variable. Here is an example of the
|
||||
configuration:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user