Ever since Terraform was first launched by HashiCorp, infrastructure teams have been quick to leverage its functionality. Because deploying infrastructure via code became so much easier and error-free. This surely became a great way to deploy new infrastructure with custom configurations, but what about managing cloud infrastructure that is already defined?
Can Terraform be used to make changes to them? Or can it be used to deploy the same configurations to new environments? It maybe possible, but it’s not the preferred approach anyone follows.
Unfortunately managing existing resources is slightly complicated with Terraform. You will have to do a terraform-import and define a resource block in terraform configuration with complete resource metadata. This however is a complex process where you have to manually apply the import command for all resources that belong to a project.
Terraformer - Convert existing Infrastructure to Terraform Files (IaC)
Terraformer, a CLI tool developed by Waze SRE and made open-source soon after, became quickly popular because of its capabilities of overcoming these challenges.
How does it work?
Terraformer can do the reverse of what Terraform does. With Terraformer, you can generate tf/json and tfstate files (terraform files) based on your infrastructure’s existing configurations. The same can be done to your existing Squadcast resources too. Follow this guide to use Terraformer with Squadcast: https://developers.squadcast.com/terraform/tutorial/how-to-use-terraformer/
This is beneficial mainly for customers or users who have initially configured their Squadcast account using our API or Web UI. They can indeed use Terraformer to generate Terraform state files based on their account’s configuration. And they can continue to use Terraform going forward to manage their resources via IaC (Infrastructure as Code).
Benefits (or advantages) of using Terraformer
- Read only permissions: Terraformer requires just the RO permissions. So there is no risk of changing existing infrastructure configurations
- Fast deployment: The use of VMs, storage buckets, PaaS resources, etc., in complex distributed architectures has made it difficult to quickly deploy resources. Leveraging IaC for the same can help you deploy complex infrastructures including Squadcast resources to multiple environments in just seconds
- Error free: The use of ‘Terraform import’ can lead to minor issues and downtime because of manual intervention. The use of Terraformer (IaC) automates manual tasks and reduces the risk of error to a great extent
- Cost: The use of Terraformer can help teams spend less time migrating existing infrastructure to their cloud environment, and more time to focus on critical tasks. This reduces the overall cost significantly
Implementation - How to use Terraformer with Squadcast?
To generate Terraform files for your existing Squadcast configuration, you can use our Squadcast implementation of Terraformer.
Download Terraformer binaries
You can either go to the following URL and download the appropriate binary for your OS: https://github.com/SquadcastHub/terraformer/releases
Or you can run one of the below commands from the terminal to download the appropriate binaries:
On MacOS:
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/6a87e97b9b3e6f51a5e10444c7984f58.js</p>
On Linux:
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/04939c43c27a4127bf36858e9efb6351.js</p>
On Windows:
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/74ab6830dbefb0f0318d25af5a39c258.js</p>
Configure the binaries
Go to the downloaded folder and run the below command to make the binary executable:
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/cf65b597ec02603b700c55854f14f18d.js</p>
Move this executable to /usr/local/bin by running the below command:
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/ca432132b90327a7ede11d641e82ac40.js</p>
Setup Terraform
Create a new directory in your home folder. Inside this directory, create a new file called versions.tf and add the following code:
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/1a262364cbb0f8c53bf47e1e2a096046.js</p>
And then initiate Terraform by running the following command:
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/f5e9e9364584d2f284d4fd79057f953b.js</p>
Generate Terraform files
Use Terraformer to import resources:
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/bc42f22ead6e8af98d91a7cd8ddaa075.js</p>
This will create a new generated folder with all terraform files (including statefiles) based on the previous command.
Update “version” and add “source” in newly autogenerated “provider.tf” file
Go to /generated/squadcast/<RESOURCE_NAME>/<REGION>/provider.tf
Add source = "SquadcastHub/squadcast" to squadcast inside required_providers
Update version in required_providers by removing .exe (Windows users only)
Update “terraform_version”
First change path to this directory by running the following command:
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/eea548afd63efadca2a23868c3f744ac.js</p>
And then run the following command:
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/f20b7ac1c026daf62263e06e9f4f58e8.js</p>
Use Generated files with Terraform
Update generated files as per your requirement and apply the changes using following commands:
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/2d71a381c49ec95e97bbf45dcbe7bb5b.js</p>
Now you’re good to go! In just a few simple steps you’ve converted your entire Squadcast configuration into Infrastructure as Code. You can now use this code file to manage all resources on Squadcast such as adding users in bulk, defining roles to users, deploying the entire configuration to another environment, and much more.