New action: Terraform CLI

New action: Terraform CLI

With its simple syntax, Terraform allows you to easily provision, change, and version resources in every cloud. Becoming one of the most popular IaC (Infrastructure as Code) tools out there, it is no wonder it quickly became one of the most requested features for Buddy. Well - it's finally here.

Terraform and Continuous Delivery

Besides deploying the code, Terraform allows users to automate infrastructural changes. Terraform config is kept in the repository, making it possible to version. The delivery pipeline will automatically run 'terraform apply', adjusting our infrastructure to the requirements included in the code.

Terraform configuration and state

The infrastructure is described in Terraform configuration files (.tf). Running 'terraform apply' for the first time, will create the resources described. Apart from that, it will create something called Terraform state which will serve as a database for consecutive runs.

Terraform state is crucial to the entire process as it lets us know what we have to do in the following 'terraform apply' executions.

Example:

  1. In our configuration file, we state that a single 'EC2 t3.medium' instance will be required
  2. First execution will create the Virtual Machine (as the state is empty) and will save its ID to the Terraform State
  3. Another run will check our Terraform State and, seeing that the VM is already included, will not execute anything. Without the Terraform State, the VM would be created on every single execution
Warning
IMPORTANT: Please make sure that Terraform State is persistent. Without it, Terraform will create a new infrastructure with every consecutive run instead of updating the already existing one!

To maintain your state, it is recommended to use something called Terraform Backends. We recommend using AWS S3 or Google GCS

Terraform in Buddy

You can use Terraform CLI action in our delivery pipelines. This action allows you to provide commands that will be executed in the context of the repository containing the '.tf' file. It also allows you to select the Terraform version.

Image loading...

Hint
We recommend using a fixed Terraform version instead of the "latest". There were cases in the past where Terraform states varied between versions which caused issues. Using a fixed version gives you more control and prevents errors when changing or updating.

Terraform backend configuration can be done directly in the the '.tf' file in the repository, or by using the environment variables set in the Variables tab of the action. On top of this, Buddy provides dedicated AWS and GCP integration which allows for further backend configuration options in S3/DynamoDB and Google Cloud Storage.

Image loading...

Example use

Image loading...

The pipeline above is one of the examples of how Terraform can be used. After the application is built, Terraform will validate if the configuration files are correct. If everything checks out, the following Terraform action will plan changes that will be executed after the manual approval. In this scenario we have used Rsync as our deployment action as an example.

Image loading...

To ensure that the infrastructure changes trigger only when there are changes to the '*tif' file, we have added a trigger condition to actions 2-6. That way, whenever there are no infrastructural changes, the pipeline will build and deploy your app normally.

Jarek Dylewski

Jarek Dylewski

Customer Support

A journalist and an SEO specialist trying to find himself in the unforgiving world of coders. Gamer, a non-fiction literature fan and obsessive carnivore. Jarek uses his talents to convert the programming lingo into a cohesive and approachable narration.

Aug 10th 2022
Share