If you're managing Google Cloud Platform (GCP) resources by clicking through the console, you're probably spending more time than you need to—and risking inconsistencies across environments. Terraform offers a better way: infrastructure as code (IaC) that lets you define, version, and deploy your cloud resources using configuration files.
This guide walks you through setting up Terraform for GCP, from initial configuration to deploying your first resources.
Why Use Terraform with Google Cloud?
Terraform is an open-source tool that lets you describe your infrastructure in human-readable configuration files. Instead of manually creating virtual machines, networks, and storage buckets through the GCP console, you write code that defines what you want, and Terraform handles the rest.
The benefits for small businesses are concrete:
- Repeatability: Deploy identical environments for development, staging, and production
- Version control: Track infrastructure changes in Git alongside your application code
- Documentation: Your Terraform files serve as living documentation of your infrastructure
- Cost control: Easily spin down entire environments when not needed
- Team collaboration: Multiple team members can work on infrastructure without stepping on each other's toes
If you're looking to modernize your cloud infrastructure or implement better automation practices, Terraform is an excellent starting point.
Prerequisites
Before you begin, you'll need:
- A Google Cloud Platform account with billing enabled
- A GCP project created (you can use an existing one)
- Terraform installed on your local machine (download from terraform.io)
- Basic familiarity with command-line tools
- A text editor (VS Code, Sublime, or even Notepad++ works fine)
Setting Up GCP Authentication
Terraform needs permission to create and manage resources in your GCP project. The most straightforward method for getting started is using a service account.
- In the GCP Console, navigate to "IAM & Admin" > "Service Accounts"
- Click "Create Service Account"
- Give it a name like "terraform-sa" and click "Create"
- Grant it the "Editor" role (for production, you'll want more granular permissions)
- Click "Done," then find your new service account in the list
- Click the three dots menu and select "Manage keys"
- Click "Add Key" > "Create new key" > "JSON"
- Save the downloaded JSON file securely—this is your credential file
Set an environment variable pointing to this file:
bash export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your-key-file.json"
For Windows PowerShell, use:
powershell $env:GOOGLE_APPLICATION_CREDENTIALS="C:\path\to\your-key-file.json"
Creating Your First Terraform Configuration
Create a new directory for your Terraform project and create a file called main.tf. This is where you'll define your infrastructure.
Here's a simple configuration that creates a Google Cloud Storage bucket:
hcl terraform { required_providers { google = { source = "hashicorp/google" version = "~> 5.0" } } }
provider "google" { project = "your-project-id" region = "us-central1" }
resource "google_storage_bucket" "example_bucket" { name = "my-unique-bucket-name-12345" location = "US" force_destroy = true
uniform_bucket_level_access = true }
Replace your-project-id with your actual GCP project ID, and choose a globally unique bucket name.
Understanding the Configuration
Let's break down what each section does:
- terraform block: Specifies which providers you're using and their versions
- provider block: Configures the Google Cloud provider with your project and default region
- resource block: Defines an actual infrastructure component—in this case, a storage bucket
The resource block follows the pattern: resource "resource_type" "local_name". The resource type tells Terraform what kind of GCP resource to create, and the local name is how you'll reference it within your Terraform code.
Deploying Your Infrastructure
Now comes the exciting part—actually creating resources. Terraform uses a three-step workflow:
1. Initialize
Run this command in your project directory:
bash terraform init
This downloads the Google Cloud provider plugin and sets up your working directory. You only need to run this once per project (or when you add new providers).
2. Plan
Before making any changes, see what Terraform intends to do:
bash terraform plan
Terraform will show you exactly what resources it will create, modify, or destroy. This is your safety check—review it carefully.
3. Apply
When you're ready to proceed:
bash terraform apply
Terraform will show you the plan again and ask for confirmation. Type yes to proceed. In a few moments, your storage bucket will exist in GCP.
Expanding Your Infrastructure
Once you're comfortable with the basics, you can add more resources. Here's an example that adds a Compute Engine instance:
hcl resource "google_compute_instance" "web_server" { name = "web-server-01" machine_type = "e2-micro" zone = "us-central1-a"
boot_disk { initialize_params { image = "debian-cloud/debian-11" } }
network_interface { network = "default" access_config { // Ephemeral public IP } } }
Add this to your main.tf file and run terraform plan to see what will change, then terraform apply to create the instance.
Best Practices for Small Teams
Use variables: Instead of hardcoding values, create a variables.tf file to make your configurations reusable.
Store state remotely: By default, Terraform stores state locally. For team collaboration, use a GCS bucket as a remote backend.
Organize by environment: Create separate directories or workspaces for development, staging, and production.
Version control everything: Commit your .tf files to Git, but never commit your service account key or state files.
Start small: Don't try to Terraform your entire infrastructure at once. Start with non-critical resources and expand gradually.
Managing Changes and Cleanup
When you modify your Terraform files, run terraform plan to preview changes before applying them. Terraform is smart enough to figure out what needs to be updated, created, or destroyed.
To remove all resources defined in your configuration:
bash terraform destroy
This is particularly useful for development environments you only need during business hours.
Next Steps
Once you've mastered the basics, explore:
- Terraform modules for reusable infrastructure components
- Remote state storage in GCS buckets
- Terraform Cloud for team collaboration
- Integration with CI/CD pipelines for automated deployments
If you need help implementing infrastructure as code for your business or want custom automation solutions tailored to your workflow, we're here to help.
Ready to Modernize Your Infrastructure?
Terraform can transform how you manage cloud resources, but getting started can feel overwhelming. Whether you need help with initial setup, migrating existing infrastructure to code, or building a complete automation pipeline, reach out to our team. We'll help you implement infrastructure as code in a way that makes sense for your business.
FAQ
Q: Do I need to know programming to use Terraform?
A: Not really. Terraform uses HCL (HashiCorp Configuration Language), which is more like a configuration format than a programming language. If you can read and write JSON or YAML, you can learn Terraform. Basic command-line skills are helpful, but you don't need to be a developer.
Q: Will Terraform increase my GCP costs?
A: Terraform itself is free and doesn't add costs. However, it makes spinning up resources so easy that you might create more infrastructure than you need. The flip side is that Terraform also makes it easy to destroy resources when you're done with them, which can actually reduce costs by ensuring you're not paying for idle development environments.
Q: Can I use Terraform to manage existing GCP resources I created manually?
A: Yes, through a process called "importing." You can import existing resources into Terraform's state and then manage them with code going forward. It requires some manual work to write the corresponding Terraform configuration, but it's definitely possible and often worthwhile for critical infrastructure.
Related services
Need a hand with this?
O-Cyrus helps small businesses with websites, DNS, custom apps, and the everyday tech that keeps things running.
Talk to us →