This script will pull down an S3 remote configuration before running any terraform actions. Assumes the following structure:
main.tf
terraform.cfg
env/dev/vars
env/staging/vars
env/whatever/vars
env/whatever/somefile.tf
terraform.cfg
is required for the remote configuration.
You can use it like so:
./terraform-env.sh dev show
./terraform-env.sh staging plan -out plan
./terraform-env.sh production apply -var="some_var=some_value"
You can use the script just like you'd use terraform
, passing whatever arguments you would before. It will only pull the latest environment if the current environment is different from the one you're requesting.
Caveat: this won't work if you've written a plan out to a file and then try to apply that plan as terraform apply
doesn't accept the -var-file
flag when reading from a plan. For reference:
./terraform-env.sh plan -out plan
./terraform-env.sh apply plan
The above will not work. It's recommend you just run terraform apply plan
as the plan file will contain all the necessary bits to run the apply.
This script also allows you to use environment-specific config files. For example, you could have specific dns settings for production in env/production/dns-extra.tf
and the script will copy the file into the directory you're calling the script from, run terraform, then delete the copied file.