Modular GCP VM Hands-On

Review a small Terraform project that calls a reusable compute module and passes provider configuration and variables from the root module.

TrackTerraform Learning Journey
Current SectionModules And Reuse
Progress9 of 12

Modular GCP VM Hands-On

This example turns a simple GCP VM deployment into a more maintainable Terraform project by separating the root module from a reusable compute_instance child module.

What this example teaches

  • How root modules pass values into child modules.
  • How to keep variables, outputs, and resource definitions in separate files.
  • How modules reduce duplication when you want to provision the same pattern more than once.
  • How terraform.tfvars supplies environment-specific values without hardcoding them in resource blocks.

Files to inspect

  • main.tf: provider configuration plus the module call.
  • variables.tf: input variables for the root module.
  • terraform.tfvars: example project-specific values.
  • modules/compute_instance/main.tf: the reusable VM definition.
  • modules/compute_instance/variables.tf: module inputs.
  • modules/compute_instance/outputs.tf: values exposed by the module.

Suggested workflow

  1. Compare this folder with the earlier single-file VM example.
  2. Trace how project_id, zone, and startup script values flow from root to child module.
  3. Run terraform plan to confirm the module still resolves into concrete GCP resources.
  4. Duplicate the module call to understand how reuse scales.

Notes before reuse

  • Keep service account keys out of source control.
  • Use remote state if multiple people will run this configuration.
  • Add outputs and validation rules as the module grows.