2014-07-23 18:40:15 +02:00
|
|
|
---
|
2021-12-15 03:41:17 +01:00
|
|
|
page_title: 'Provisioner: local-exec'
|
|
|
|
description: >-
|
|
|
|
The `local-exec` provisioner invokes a local executable after a resource is
|
|
|
|
created. This invokes a process on the machine running Terraform, not on the
|
|
|
|
resource. See the `remote-exec` provisioner to run commands on the resource.
|
2014-07-23 18:40:15 +02:00
|
|
|
---
|
|
|
|
|
|
|
|
# local-exec Provisioner
|
|
|
|
|
2017-04-05 17:29:27 +02:00
|
|
|
The `local-exec` provisioner invokes a local executable after a resource is
|
|
|
|
created. This invokes a process on the machine running Terraform, not on the
|
|
|
|
resource. See the `remote-exec`
|
2021-12-15 03:41:17 +01:00
|
|
|
[provisioner](/language/resources/provisioners/remote-exec) to run commands on the
|
2017-04-05 17:29:27 +02:00
|
|
|
resource.
|
2014-07-23 18:40:15 +02:00
|
|
|
|
2017-04-05 17:29:27 +02:00
|
|
|
Note that even though the resource will be fully created when the provisioner is
|
|
|
|
run, there is no guarantee that it will be in an operable state - for example
|
|
|
|
system services such as `sshd` may not be started yet on compute resources.
|
2016-08-30 11:24:36 +02:00
|
|
|
|
2019-09-06 00:50:04 +02:00
|
|
|
-> **Note:** Provisioners should only be used as a last resort. For most
|
|
|
|
common situations there are better alternatives. For more information, see
|
2021-12-15 03:41:17 +01:00
|
|
|
[the main Provisioners page](/language/resources/provisioners).
|
2019-09-06 00:50:04 +02:00
|
|
|
|
2014-07-23 18:40:15 +02:00
|
|
|
## Example usage
|
|
|
|
|
2017-04-05 17:29:27 +02:00
|
|
|
```hcl
|
2014-07-23 18:40:15 +02:00
|
|
|
resource "aws_instance" "web" {
|
2017-02-18 23:48:50 +01:00
|
|
|
# ...
|
2017-04-05 17:29:27 +02:00
|
|
|
|
2017-02-18 23:48:50 +01:00
|
|
|
provisioner "local-exec" {
|
2020-11-18 02:24:30 +01:00
|
|
|
command = "echo ${self.private_ip} >> private_ips.txt"
|
2017-02-18 23:48:50 +01:00
|
|
|
}
|
2014-07-23 18:40:15 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Argument Reference
|
|
|
|
|
2014-07-29 08:51:24 +02:00
|
|
|
The following arguments are supported:
|
2014-07-23 18:40:15 +02:00
|
|
|
|
|
|
|
* `command` - (Required) This is the command to execute. It can be provided
|
|
|
|
as a relative path to the current working directory or as an absolute path.
|
|
|
|
It is evaluated in a shell, and can use environment variables or Terraform
|
|
|
|
variables.
|
2017-08-22 19:07:32 +02:00
|
|
|
|
2018-02-16 20:31:11 +01:00
|
|
|
* `working_dir` - (Optional) If provided, specifies the working directory where
|
2021-05-18 17:04:54 +02:00
|
|
|
`command` will be executed. It can be provided as a relative path to the
|
2021-12-15 03:41:17 +01:00
|
|
|
current working directory or as an absolute path. The directory must exist.
|
2018-02-16 20:31:11 +01:00
|
|
|
|
2017-08-22 19:07:32 +02:00
|
|
|
* `interpreter` - (Optional) If provided, this is a list of interpreter
|
|
|
|
arguments used to execute the command. The first argument is the
|
|
|
|
interpreter itself. It can be provided as a relative path to the current
|
|
|
|
working directory or as an absolute path. The remaining arguments are
|
|
|
|
appended prior to the command. This allows building command lines of the
|
|
|
|
form "/bin/bash", "-c", "echo foo". If `interpreter` is unspecified,
|
|
|
|
sensible defaults will be chosen based on the system OS.
|
|
|
|
|
2018-03-06 00:58:49 +01:00
|
|
|
* `environment` - (Optional) block of key value pairs representing the
|
|
|
|
environment of the executed command. inherits the current process environment.
|
|
|
|
|
2017-08-22 19:07:32 +02:00
|
|
|
### Interpreter Examples
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
resource "null_resource" "example1" {
|
|
|
|
provisioner "local-exec" {
|
|
|
|
command = "open WFH, '>completed.txt' and print WFH scalar localtime"
|
|
|
|
interpreter = ["perl", "-e"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
resource "null_resource" "example2" {
|
|
|
|
provisioner "local-exec" {
|
|
|
|
command = "Get-Date > completed.txt"
|
|
|
|
interpreter = ["PowerShell", "-Command"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
2018-03-06 00:58:49 +01:00
|
|
|
|
|
|
|
```hcl
|
|
|
|
resource "aws_instance" "web" {
|
|
|
|
# ...
|
|
|
|
|
|
|
|
provisioner "local-exec" {
|
|
|
|
command = "echo $FOO $BAR $BAZ >> env_vars.txt"
|
|
|
|
|
2019-03-08 16:59:28 +01:00
|
|
|
environment = {
|
2018-03-06 00:58:49 +01:00
|
|
|
FOO = "bar"
|
|
|
|
BAR = 1
|
|
|
|
BAZ = "true"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|