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.tfvarssupplies 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
- Compare this folder with the earlier single-file VM example.
- Trace how
project_id,zone, and startup script values flow from root to child module. - Run
terraform planto confirm the module still resolves into concrete GCP resources. - 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.