Skip to content

Surrogate is a templating engine and acts like a swiss army knife for all your templating needs.

License

Notifications You must be signed in to change notification settings

vmanikes/Surrogate

Repository files navigation

Surrogate

alt text

Rust CI DevSkim Clippy

Surrogate is a templating engine and acts like a swiss army knife for all your templating needs.

Installation

  • Make sure you have rust and cargo installed
  • Run the following command to build the surrogate binary cargo build --release and you can see the binary listed in the target directory
  • Move the built binary to /usr/bin

Usage

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

What problems does it solve?

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 where main.tf is defined.
  • Copy the contents of main.tf to main.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}} with us-east-1 and will create a new file called main.tf with contents replaced
  • Cool ain't it eh !!!

About

Surrogate is a templating engine and acts like a swiss army knife for all your templating needs.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published