This is a basic example on how to use the local-exec
provisioner in terraform, and I will use it to write a environment variable’s value to disk.
Installing Terraform
Get the latest version of terraform, for this post, I will be using the latest version of the time of writing:
1 2 3 |
|
Ensure that it’s working:
1 2 |
|
Terraform local-exec
The local-exec provisioner allows us to run a command locally, so to test that we will write the environment variable owner=ruan
to disk.
First setup our main.tf
:
1 2 3 4 5 |
|
As you can see our local-exec
provisioner is issuing the command echo to write the environment variable owner
’s value to a file on disk, and the file name is file_
+ the null resource’s id.
As we are referencing a variable, we need to define the variable, I will define it in variables.tf
:
1
|
|
As you can see, I am not defining the value, as I will define the value at runtime.
Initialize
When we initialize terraform, terraform builds up a dependency tree from all the .tf
files and downloads any dependencies it requires:
1
|
|
Apply
Run our deployment and pass our variable at runtime:
1 2 3 4 5 6 7 8 |
|
View the written file:
1 2 |
|
If we wanted to define the environment variable in the variables.tf
file, it will look like this:
1 2 3 4 |
|
The github repository for this code is located at: