As the ecosystem around Terraform continues to grow we’re going to continue to blog about tools that we find interesting in this space. Today, we’ll talk about Bridgecrew Yor, which can be used to automatically tag infrastructure across a number of IaC platforms.
Yor supports a number of different “Built-in Taggers”. The “Git-based Tagger” is super helpful for organizations adopting GitOps:
“The git_* tags connect cloud resources to individual git commits and establish clear ownership between developers and resources which are routinely change.”
The “custom tagger”, which we’ll interact with today, allows you to build any tagging standard to meet your organizational.
The combination of the two essentially brings together a necessary GitOps paper trail plus your tagging standard to meet the general organizational needs.
Integrate with Scalr
Scalr has many native integrations out of the box, but we have added flexibility by providing custom hooks to integrate with any tool in the cloud ecosystem, which is exactly what we’ll do with Yor. To do this we are going to leverage a couple of Scalr features:
Custom hooks - This allows you to inject any command or script during the following Terraform phases:
Pre-init
Pre-plan
Post-plan
Pre-apply
Post-apply
Workspace Tags - These are tags associated with a workspace in Scalr, but in this scenario we are going to take that value and use the Yor custom taggers to inject that tag to the Terraform resources as well.
Prerequisites:
An account on scalr.io . We will be using the new beta UI, which can be accessed by adding /app2 to your URL: https://<account>.scalr.io/app2/
Terraform code in the connected VCS repository. The sample code below can be used.
Overview:
Create Terraform code if you don’t already have it.
Create the code which will pull Yor into the workflow and execute it.
Create the workspace settings and add the custom hook.
Execute the run and check the tags.
Steps
Create the Terraform Code
First, let's create some Terraform code to launch a S3 bucket. NOTE: You can use your own Terraform code, it is not required to use this sample as Yor will automatically tag any resources that it supports.
NOTE: Make sure that pre-plan.sh is added as an executable to Git.
Now, let’s create a python script (scripts.py) that the pre-plan.sh will call. This script will pull a number of different objects like VCS information, run details, and workspace tags, which Yor will then use to tag the instance with:
Next, log into Scalr and create a workspace, in this example we’ll set a VCS provider as the source and link to the repository with the code that was just created:
Drop down custom hooks and add pre-plan.sh to the before plan hook:
Add some sample workspace tags. This can only be done in the new beta UI( add /app2 to your URL https://<account>.scalr.io/app2/) or through the Scalr Terraform provider.
Click create workspace.
If you’re using the main.tf and variables.tf from the example, it will require you to set some variables in Scalr:
Execute the Run
Now all you have to do is execute the run:
During the run, you’ll see that it first executes the pre-plan.sh
Then the run goes through the rest of the pipeline resulting in an apply and the resources being tagged property in AWS:
In just a few simple steps we were able to automate our tagging strategy for Terraform using Scalr custom hooks and plugging Yor into the pipeline. As you can see, Yor is a very simple, yet powerful, tool to get up and running. The Scalr custom hooks open up the possibilities for many integrations and having a fully customized workflow without having to build a DIY pipeline.
Note: While this blog references Terraform, everything mentioned in here also applies to OpenTofu. New to OpenTofu? It is a fork of Terraform 1.5.7 as a result of the license change from MPL to BUSL by HashiCorp. OpenTofu is an open-source alternative to Terraform that is governed by the Linux Foundation. All features available in Terraform 1.5.7 or earlier are also available in OpenTofu. Find out the history of OpenTofu here.