replace null_resource with terraform_data

Replace references to null_resource with terraform_data, and link the
use of terraform_data from replace_triggered_by.
This commit is contained in:
James Bardin 2023-02-14 09:15:36 -05:00
parent 0328d116c5
commit df65d16022
6 changed files with 20 additions and 30 deletions

View File

@ -96,7 +96,7 @@
]
},
{
"title": "The <code>terraform_data</code> Resource",
"title": "The <code>terraform_data</code> Resource Type",
"path": "resources/terraform-data"
}
]

View File

@ -144,6 +144,8 @@ The arguments available within a `lifecycle` block are `create_before_destroy`,
}
```
If a local value, module output, or variable needs to be tracked by `replace_triggered_by`, it can be stored in a [`terraform_data`](language/resources/terraform-data) managed resource.
## Custom Condition Checks
You can add `precondition` and `postcondition` blocks with a `lifecycle` block to specify assumptions and guarantees about how resources and data sources operate. The following examples creates a precondition that checks whether the AMI is properly configured.

View File

@ -67,7 +67,7 @@ The following arguments are supported:
### Interpreter Examples
```hcl
resource "null_resource" "example1" {
resource "terraform_data" "example1" {
provisioner "local-exec" {
command = "open WFH, '>completed.txt' and print WFH scalar localtime"
interpreter = ["perl", "-e"]
@ -76,7 +76,7 @@ resource "null_resource" "example1" {
```
```hcl
resource "null_resource" "example2" {
resource "terraform_data" "example2" {
provisioner "local-exec" {
command = "Get-Date > completed.txt"
interpreter = ["PowerShell", "-Command"]

View File

@ -1,23 +1,22 @@
---
page_title: Provisioners Without a Resource
description: >-
A null_resource allows you to configure provisioners that are not directly
associated with a single existing resource.
A terraform_data managed resource allows you to configure provisioners that
are not directly associated with a single existing resource.
---
# Provisioners Without a Resource
[null]: https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource
If you need to run provisioners that aren't directly associated with a specific
resource, you can associate them with a `null_resource`.
Instances of [`null_resource`][null] are treated like normal resources, but they
don't do anything. Like with any other resource, you can configure
[provisioners](/language/resources/provisioners/syntax) and [connection
details](/language/resources/provisioners/connection) on a `null_resource`. You can also
use its `triggers` argument and any meta-arguments to control exactly where in
the dependency graph its provisioners will run.
Instances of [`terraform_data`](language/resources/terraform-data) are treated
like normal resources, but they don't do anything. Like with any other resource
type, you can configure [provisioners](/language/resources/provisioners/syntax)
and [connection details](/language/resources/provisioners/connection) on a
`terraform_data` resource. You can also use its `triggers` argument and any
meta-arguments to control exactly where in the dependency graph its
provisioners will run.
~> **Important:** Use provisioners as a last resort. There are better alternatives for most situations. Refer to
[Declaring Provisioners](/language/resources/provisioners/syntax) for more details.
@ -31,16 +30,14 @@ resource "aws_instance" "cluster" {
# ...
}
resource "null_resource" "cluster" {
# Changes to any instance of the cluster requires re-provisioning
triggers = {
cluster_instance_ids = "${join(",", aws_instance.cluster.*.id)}"
}
resource "terraform_data" "cluster" {
# Replacement of any instance of the cluster requires re-provisioning
triggers_replace = aws_instance.cluster.[*].id
# Bootstrap script can run on any instance of the cluster
# So we just choose the first in this case
connection {
host = "${element(aws_instance.cluster.*.public_ip, 0)}"
host = aws_instance.cluster.[0].public_ip
}
provisioner "remote-exec" {
@ -51,12 +48,3 @@ resource "null_resource" "cluster" {
}
}
```
## Argument Reference
In addition to meta-arguments supported by all resources, `null_resource`
supports the following specific arguments:
- `triggers` - A map of values which should cause this set of provisioners to
re-run. Values are meant to be interpolated references to variables or
attributes of other resources.

View File

@ -1,5 +1,5 @@
---
page_title: The terraform_data Managed Resource
page_title: The terraform_data Managed Resource Type
description: >-
Retrieves the root module output values from a Terraform state snapshot stored
in a remote backend.

View File

@ -143,7 +143,7 @@
</li>
<li>
<a href="/docs/language/resources/terraform-data.html">Terraform Data Resource</a>
<a href="/docs/language/resources/terraform-data.html">Terraform Data Resource Type</a>
</li>
</ul>