Terraform, by HashiCorp, is an open source Infrastructure as Code (IaC) software. Terraform creates execution plans to build data center infrastructure through the use of configuration files. Enabling users to confidently create, change, or destroy their infrastructure. The benefits of using IaC software such as Terraform includes:
- Automation – Include in your continuous build process
- Transparency – Understand how changes affect the entire workflow
- Reproducible – Build once and replicate anywhere
Now that we understand what Terraform is, let’s jump into how Terraform works.
Define your resources
The first thing we must do when using Terraform, is define which resources are needed for our infrastructure. It is through Terraform configuration files, that we identify existing resources, create new resources, and link dependent resources. For example, if we were going to create an Amazon Web Service (AWS) lambda we would need the following resources:
- Lambda
- S3 bucket
- Path to deployment package
- IAM Role
In identifying the required resources for our application, we can configure them to our specifications. Configuration is resource specific and Terraform has some great documentation explaining the various configuration options.
Configure your Terraform backend
Once we define and configure our resources, we must decide on our backend. Terraform uses a state file to track the resources it creates, in order to identify what changes are needed to our infrastructure when updates are made. This state file can reside on our local machine or remotely. We suggest storing it remotely, especially if you are working with a team. This ensures that the latest version of your state file and the associated resources are accessible to everyone. At this point, we can setup our backend by executing a ‘terraform init’ command.
Plan and apply your changes
The last step before deploying our infrastructure is to run a terraform plan. In this step, Terraform shows you a preview of the requested infrastructure changes. If all of the proposed changes are correct, we can deploy the changes by running a ‘terraform apply’. When attempting to apply changes, Terraform will again show you a preview of changes except it will require confirmation to proceed with the deployment. Upon confirmation, Terraform will attempt to deploy our infrastructure changes.
Conclusion
Terraform is the Infrastructure as Code software of choice at TG4. With the help of Jenkins, Terraform enables us to build out our application infrastructure in our development environment and deploy to our quality assurance and production environments seamlessly. Prior to using Terraform, our engineers spent most of their time deploying application infrastructure changes across environments. We also had issues with unused resources running in our development environment. After transitioning to Terraform, we have seen an increase in efficiency from our engineers and a huge decrease in unused resources.
Have Terraform questions? Contact us, we would love to help you.