Creating Reusable Infrastructure with Terraform Modules in GCP
Description
Terraform modules are a critical component of production-grade Terraform configurations. Modules give infrastructure developers the ability to organize services into logical, reusable components. For example, you can create a module for deploying Google Cloud Compute Engine instances and a module for deploying pre-configured Cloud Storage buckets. Each of these modules could then be used to implement common infrastructure patterns with less time spent on configurations.
Terraform configuration files that are thousands of lines of code are considered an anti-pattern. Once infrastructure code grows to a certain size and becomes unmanageable, execution plans may take over 10 minutes to complete. Instead of this monolithic approach, splitting the resource configurations up into modules can provide reusable, easy-to-test resource units.
Modules also provide a way for the community to share Terraform configurations with others. The Terraform Registry hosts thousands of available Terraform resource modules that can be used to deploy common infrastructure configurations. It is highly recommended to refer to existing modules in this registry before creating your own from scratch.
In this lab, you will learn how to configure a GCE instance module in a local Terraform project and explore the benefits of this pattern of reusable, modular infrastructure.
Learning Objectives
Upon completion of this lab, you will be able to:
- Configure Terraform modules for GCP resources
- Pass data into and between Terraform modules
Intended Audience
- Individuals looking to pass the HashiCorp Certified: Terraform Associate Exam
- Individuals and teams seeking an introduction to using Terraform with Google Cloud Platform
Prerequisites
Familiarity with the following will be beneficial but is not required:
- Basic concepts of Google Cloud Platform
- Configuring Terraform variables
The following lab can be used to fulfill the prerequisites:
Updates
June 9th, 2023 - Updated code to use Debian 11
April 27th, 2023 - Updated theia to enable autosave