Creating Reusable Infrastructure with Terraform Modules in GCP
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
Environment before
Environment after
Jun is a Cloud Labs Developer with previous experience as a Software Engineer and Cloud Developer. He holds the AWS Certified Solutions Architect and DevOps Engineer Professional certifications. He also holds the AWS Certified Solutions Architect, Developer, and SysOps Administrator Associate certifications.
Jun is focused on giving back to the growing cloud community by sharing his knowledge and experience with students and creating engaging content.