10 Testing and refactoring

 

This chapter covers

  • Tainting and rotating AWS access keys provisioned by Terraform
  • Refactoring module expansions
  • Migrating state with terraform mv and terraform state
  • Importing existing resources with terraform import
  • Testing IaC with terraform-exec

The ancient Greek philosopher Heraclitus is famous for positing that “Life is flux.” In other words, change is inevitable, and to resist change is to resist the essence of our existence. Perhaps nowhere is change more pronounced than in the software industry. Due to changing customer requirements and shifting market conditions, software is guaranteed to change. If not actively maintained, software degrades over time. Refactoring and testing are steps that developers take to keep software current.

Refactoring is the art of improving the design of code without changing existing behavior or adding new functionality. Benefits of refactoring include the following:

  • Maintainability—The ability to quickly fix bugs and address problems faced by customers.
  • ExtensibilityHow easy it is to add new features. If your software is extensible, then you are more agile and able to respond to marketplace changes.
  • Reusability—The ability to remove duplicated and highly coupled code. Reusable code is readable and easier to maintain.

10.1 Self-service infrastructure provisioning

10.1.1 Architecture

10.1.2 Code

10.1.3 Preliminary deployment

10.1.4 Tainting and rotating access keys

10.2 Refactoring Terraform configuration

10.2.1 Modularizing code

10.2.2 Module expansions

10.2.3 Replacing multi-line strings with local values

10.2.4 Looping through multiple module instances

10.2.5 New IAM module