12 Automating Terraform
This chapter covers:
- Developing a CI/CD pipeline for automating Terraform deployments
- Running Terraform at scale
- Generating Terraform configuration code
- Toggling dynamic blocks with a conditional expression
If you want to know how to run Terraform in automation, this chapter is for you. Until now, I have assumed you are deploying Terraform from your local machine. This is a reasonable assumption for individuals and even small teams, as long as you are using a remote state backend. On the other hand, large teams and organizations with many individual contributors, may benefit from automating Terraform
In chapter 6 we discussed how HashiCorp has two products for running Terraform in automation, these are: Terraform Cloud and Terraform Enterprise. Both of these products are basically the same; Terraform Cloud is simply the SaaS version of Terraform Enterprise. In this chapter we will develop a CI/CD pipeline for deploying Terraform workspaces in automation, modeled after the design of Terraform Enterprise. The stages of the CI/CD pipeline are shown in figure 12.1.
Figure 12.1 A CI/CD pipeline for Terraform deployments with four stages. Changes to configuration code stored in a VCS source repository trigger a “terraform plan” to be run. If the plan succeeds, a manual approval is required before the changes are applied into production.
