2015-10-27 21:28:37 +01:00
|
|
|
---
|
2021-12-15 03:41:17 +01:00
|
|
|
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.
|
2015-10-27 21:28:37 +01:00
|
|
|
---
|
|
|
|
|
2019-04-15 23:11:02 +02:00
|
|
|
# Provisioners Without a Resource
|
2015-10-27 21:28:37 +01:00
|
|
|
|
2020-12-17 00:17:27 +01:00
|
|
|
[null]: https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource
|
2015-10-27 21:28:37 +01:00
|
|
|
|
2019-04-15 23:11:02 +02:00
|
|
|
If you need to run provisioners that aren't directly associated with a specific
|
|
|
|
resource, you can associate them with a `null_resource`.
|
2015-10-27 21:28:37 +01:00
|
|
|
|
2019-04-15 23:11:02 +02:00
|
|
|
Instances of [`null_resource`][null] are treated like normal resources, but they
|
|
|
|
don't do anything. Like with any other resource, you can configure
|
2021-12-15 03:41:17 +01:00
|
|
|
[provisioners](/language/resources/provisioners/syntax) and [connection
|
|
|
|
details](/language/resources/provisioners/connection) on a `null_resource`. You can also
|
2019-04-15 23:11:02 +02:00
|
|
|
use its `triggers` argument and any meta-arguments to control exactly where in
|
|
|
|
the dependency graph its provisioners will run.
|
2015-10-27 21:28:37 +01:00
|
|
|
|
2022-01-05 17:08:33 +01:00
|
|
|
-> **Note:** Provisioners should only be used as a last resort. For most
|
|
|
|
common situations there are better alternatives. Refer to
|
|
|
|
[Declaring Provisioners](/language/resources/provisioners/syntax) for more details.
|
|
|
|
|
2015-10-27 21:28:37 +01:00
|
|
|
## Example usage
|
|
|
|
|
2017-04-05 17:29:27 +02:00
|
|
|
```hcl
|
2015-10-27 21:28:37 +01:00
|
|
|
resource "aws_instance" "cluster" {
|
|
|
|
count = 3
|
2017-02-18 23:48:50 +01:00
|
|
|
|
2017-04-05 17:29:27 +02:00
|
|
|
# ...
|
2015-10-27 21:28:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "null_resource" "cluster" {
|
|
|
|
# Changes to any instance of the cluster requires re-provisioning
|
2019-04-15 23:11:02 +02:00
|
|
|
triggers = {
|
2015-10-27 21:28:37 +01:00
|
|
|
cluster_instance_ids = "${join(",", 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)}"
|
|
|
|
}
|
|
|
|
|
|
|
|
provisioner "remote-exec" {
|
2019-03-25 19:16:14 +01:00
|
|
|
# Bootstrap script called with private_ip of each node in the cluster
|
2015-10-27 21:28:37 +01:00
|
|
|
inline = [
|
2017-02-18 23:48:50 +01:00
|
|
|
"bootstrap-cluster.sh ${join(" ", aws_instance.cluster.*.private_ip)}",
|
2015-10-27 21:28:37 +01:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Argument Reference
|
|
|
|
|
2019-04-15 23:11:02 +02:00
|
|
|
In addition to meta-arguments supported by all resources, `null_resource`
|
|
|
|
supports the following specific arguments:
|
2015-10-27 21:28:37 +01:00
|
|
|
|
2021-12-15 03:41:17 +01:00
|
|
|
- `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.
|