terraform-sage

Cross-platform tool for easier Terraform deployments

Features

Requirements

Terraform >= 0.11 (older not tested)

Quick start

  1. Install Terraform.

  2. Download executable/binary file in according to the used operation system from the releases page.

  3. Link executable/binary file to operation system, so you could invoke terraform-sage everywhere:

  4. Go to your directory with code and create a directory for terraform files. For example, let's name it terraform.

  5. Inside of the terraform directory, create the configs directory. This directory will store all required Terraform modules related for each environment. So you will get something like this: <sources> ├ docker ├ microservice └ terraform └ configs ├ dev │ ├ ... │ └ variables.tf ├ staging │ ├ ... │ └ variables.tf └ production ├ ... └ variables.tf The configs directory is required and used for correct and smooth work of this wrapper. The terraform-sage will track environments defined in configs directory and will make an according action depends on the invoked Terraform command.

    P.S. Also see the project structure section for more information about recommended project structure.

  6. Create the base template (I recommend to name it as main.tpl), that stores main definition of your resources and a backend storage for Terraform state. For making the writing base template easier, I recommend you to start with from main.tf module where you will describe everything what you need and then rename it to main.tpl. After when all resources / modules are described you will need to specify one of available backend storages for Terraform states, and then append {{CONFIG_NAME}} string to the key (which is basically is the file name for Terraform state), like this: terraform { backend "s3" { bucket = "state-bucket" key = "terraform/state-{{CONFIG_NAME}}.tfstate" region = "us-east-1" } } The {{CONFIG_NAME}} string is using as the template parameter that will be replaced on the used environment name during the terraform-sage command call. This feature will help us in handling different environments without duplicating source code of the main.tf file.

  7. Then, instead of direct calls to Terraform CLI, you can use the following commands: terraform-sage init terraform-sage plan terraform-sage apply terraform-sage destroy terraform-sage output Also CLI provides two additional commands for generating Terraform's main.tf modules and retrieving the list of available environments. terraform-sage generate terraform-sage list For more information about acceptable arguments and options for each command, call any desired command with the --help option.

F.A.Q.

Q: Why this tool / wrapper was written?
A: Being a quite lazy developer, I tired of writing long Terraform command calls with specifying used *.tf / *.tfvars modules when you need to create or update something on any existing environment. The mess with modules increases when developers are using different operation systems and "additional" scripts for simplifying their Terraform workflows.

Q: How can I pass extra arguments for Terraform command (i.e. terraform-sage apply)?
A: For this case you will need to append . in the end, and then specify a list of arguments as you were working with actual Terraform CLI, like this: terraform apply dev --dir=examples/approach_two . -var-file=my-variables.tf

Project structure

The terraform-sage application relies on the certain project structure for correct work. Therefore, I recommend to developers two ways to organize their own projects: - Approach #1:

terraform ├ configs │ ├ dev │ │ ├ secrets.tf │ │ └ variables.tf │ ├ staging │ │ ├ secrets.tf │ │ └ variables.tf │ └ production │ ├ secrets.tf │ └ variables.tf ├ resources │ ├ rds │ │ ├ main.tf │ │ ├ output.tf │ │ └ variables.tf │ └ sqs │ ├ main.tf │ ├ output.tf │ └ variables.tf └ main.tpl

Pros:

Development

To start developing you will need:

Before attaching to the node, you will need to build the local dev image and start it in detached mode. Run the following command from the project root folder:

docker-compose -f docker-compose.dev.yml up -d

Then connect to the app node with bash via exec command:

docker-compose -f docker-compose.dev.yml exec app bash

And now, you're ready to go! Use the cargo tool command inside of the container as you would like.

License

The terraform-sage project is published under BSD license. For more details read the LICENSE file.