Surrogate is a templating engine and acts like a swiss army knife for all your templating needs.
- Make sure you have
rust
andcargo
installed - Run the following command to build the surrogate binary
cargo build --release
and you can see the binary listed in thetarget
directory - Move the built binary to
/usr/bin
surrogate --help
_____ _
/ ____| | |
| (___ _ _ _ __ _ __ ___ __ _ __ _| |_ ___
\___ \| | | | '__| '__/ _ \ / _` |/ _` | __/ _ \
____) | |_| | | | | | (_) | (_| | (_| | || __/
|_____/ \__,_|_| |_| \___/ \__, |\__,_|\__\___|
__/ |
|___/
Terraform backend and version templater
Usage: surrogate [OPTIONS]
Options:
-d, --directory <DIRECTORY> Directory against which surrogate should run [default: .]
-h, --help Print help information
-V, --version Print version information
Consider this example where the following is defined in your main.tf
file
terraform {
backend "s3" {
bucket = "some-s3-bucket"
key = "sometestkey"
region = "us-east-1"
}
}
Let's say you want to make the region
dynamic and wanted to use a variable that is defined in variables.tf
terraform {
backend "s3" {
bucket = "some-s3-bucket"
key = "sometestkey"
region = var.region
}
}
Now when you run terraform apply
, you will get the error Error: Variables not allowed
. This is really annoying and
is the only place where things needs
to be hardcoded.
Let's see how surrogate can help
- Let's create a file called
main.tf.tpl
in the same place wheremain.tf
is defined. - Copy the contents of
main.tf
tomain.tf.tpl
and change the fields that need to be dynamic in the following way
terraform {
backend "s3" {
bucket = "some-s3-bucket"
key = "sometestkey"
region = {{ region }}
}
}
- Create a file in the root of your project called
Surrogate.json
with the following contents
{
"region": "us-east-1"
}
- You can now go ahead and delete the
main.tf
file and run the surrogate cli to see the magic
surrogate -d {path to the project}
- Surrogate now will look for all the file with
.tpl
suffix and replace all the occurrences of{{region}}
withus-east-1
and will create a new file calledmain.tf
with contents replaced - Cool ain't it eh !!!