|
| 1 | +page_title: "local_command Action - terraform-provider-local" |
| 2 | +subcategory: "" |
| 3 | +description: |- |
| 4 | + Invokes an executable on the local machine. All environment variables visible to the Terraform process are passed through to the child process. After the child process successfully executes, the stdout will be returned for Terraform to display to the user. |
| 5 | + Any non-zero exit code will be treated as an error and will return a diagnostic to Terraform containing the stderr message if available. |
| 6 | +--- |
| 7 | + |
| 8 | +# local_command (Action) |
| 9 | + |
| 10 | +Invokes an executable on the local machine. All environment variables visible to the Terraform process are passed through to the child process. After the child process successfully executes, the `stdout` will be returned for Terraform to display to the user. |
| 11 | + |
| 12 | +Any non-zero exit code will be treated as an error and will return a diagnostic to Terraform containing the `stderr` message if available. |
| 13 | + |
| 14 | +## Example Usage |
| 15 | + |
| 16 | +For the following bash script (`example_script.sh`): |
| 17 | +```bash |
| 18 | +#!/bin/bash |
| 19 | + |
| 20 | +DATA=$(</dev/stdin) |
| 21 | +echo "stdin: $DATA, args: $@" |
| 22 | +``` |
| 23 | + |
| 24 | +Here is an example configuration that will run the script after a resource is created: |
| 25 | + |
| 26 | +```terraform |
| 27 | +resource "terraform_data" "test" { |
| 28 | + lifecycle { |
| 29 | + action_trigger { |
| 30 | + events = [after_create] |
| 31 | + actions = [action.local_command.bash_example] |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | +
|
| 36 | +action "local_command" "bash_example" { |
| 37 | + config { |
| 38 | + command = "bash" |
| 39 | + arguments = ["example_script.sh", "arg1", "arg2"] |
| 40 | + stdin = jsonencode({ |
| 41 | + "key1" : "value1" |
| 42 | + "key2" : "value2" |
| 43 | + }) |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +The `stdout` will be displayed when the action is invoked: |
| 49 | +```bash |
| 50 | + $ terraform apply -auto-approve |
| 51 | + |
| 52 | +# .. Terraform CLI output truncated for example purposes |
| 53 | + |
| 54 | +Plan: 1 to add, 0 to change, 0 to destroy. Actions: 1 to invoke. |
| 55 | +terraform_data.test: Creating... |
| 56 | +terraform_data.test: Creation complete after 0s [id=4b41b541-5550-590a-9949-657e91baa346] |
| 57 | +Action started: action.local_command.bash_example (triggered by terraform_data.test) |
| 58 | +Action action.local_command.bash_example (triggered by terraform_data.test): |
| 59 | + |
| 60 | +stdin: {"key1":"value1","key2":"value2"}, args: arg1 arg2 |
| 61 | + |
| 62 | + |
| 63 | +Action complete: action.local_command.bash_example (triggered by terraform_data.test) |
| 64 | + |
| 65 | +Apply complete! Resources: 1 added, 0 changed, 0 destroyed. |
| 66 | +``` |
| 67 | + |
| 68 | +<!-- action schema generated by tfplugindocs --> |
| 69 | +## Schema |
| 70 | + |
| 71 | +### Required |
| 72 | + |
| 73 | +- `command` (String) Executable name to be discovered on the PATH or absolute path to executable. |
| 74 | + |
| 75 | +### Optional |
| 76 | + |
| 77 | +- `arguments` (List of String) Arguments to be passed to the given command. Any `null` arguments will be removed from the list. |
| 78 | +- `stdin` (String) Data to be passed to the given command's standard input. |
| 79 | +- `working_directory` (String) The directory where the command should be executed. Defaults to the Terraform working directory. |
0 commit comments