@@ -45,10 +45,31 @@ Please note the following behavior when using hooks:
45
45
- Each hook has a 60 second timeout. If a hook times out the Terraform run will
46
46
fail immediately.
47
47
- Environment variables do not persist across hooks. For example, if a
48
- ` pre-plan ` hook exports environment variables, they will not be available
49
- during ` terraform plan ` or during the ` post-plan ` hook. Similarly, if
50
- ` terraform plan ` exports environment variables, they will not be available
51
- during the ` post-plan ` hook.
48
+ ` pre-plan ` hook exports environment variables, they will not be available
49
+ during the ` post-plan ` hook. Similarly, if ` terraform plan ` exports
50
+ environment variables, they will not be available during the ` post-plan ` hook.
51
+
52
+ ## Setting environment variables
53
+
54
+ Hooks may be used to set environment variables for subsequent ` terraform `
55
+ commands to use. To set an environment variable, write lines in ` KEY=value `
56
+ format to the file pointed to by the ` $TFC_AGENT_ENV ` environment variable.
57
+ Newlines are used to separate multiple environment variables. For example, to
58
+ set the ` FOO ` and ` BAR ` environment variables for the ` terraform plan ` operation
59
+ to consume, write the following in a ` pre-plan ` hook script:
60
+
61
+ ```
62
+ #!/bin/sh
63
+ echo FOO=hello >> $TFC_AGENT_ENV
64
+ echo BAR=world >> $TFC_AGENT_ENV
65
+ ```
66
+
67
+ Environment variables set by a hook script in this way are only applicable to
68
+ the current operation, and are not persisted for later operations. For example,
69
+ if ` FOO=bar ` is set in the ` pre-plan ` hook, then ` FOO=bar ` will be set during
70
+ the ` terraform plan ` command, but will not be set during ` terraform apply ` . To
71
+ use the variable during the apply phase, write the variable to the
72
+ ` $TFC_AGENT_ENV ` file again in the ` pre-apply ` hook script.
52
73
53
74
## Configuration
54
75
0 commit comments