Skip to content
Ercan Ermis
Ercan Ermis

notes for everyone about cloud technology

  • Cloud
    • AWS
    • GCP
  • Container
    • Kubernetes
    • Docker
  • Linux
  • DevOps
  • Privacy Policy
  • Contact
Ercan Ermis

notes for everyone about cloud technology

Google Cloud Platform Automation with Terraform Easily

Ercan, April 30, 2021April 30, 2021

Once upon a time, when I was learning Google Cloud Platform, I just want to automate my workflow and deployments with terraform and, I wrote this IaaC (Infrastructure as a Code) to manage my infrastructure easily and fastly.

What can this do?

Creating a secure load balancer with SSL and turns the HTTP requests to HTTPS with 301 redirection code.
Creating an Instance Template and set everything about network, subnet, startup script.
Creating an Instance Group and manage them. If your instance group CPU is more or less than a value, the instance group manager will automatically scale up or scale down the VMs.
VM instances are running with private IP and, project-wide SSH access is not allowed on default. Managing the firewall rule for load balancer health check.

Requirements

  • Enable Compute Engine API if doesn’t enable.
  • Create Service account with `Editor role (or whatever you want) and export the key file.
  • Create Cloud Google Storage Bucket for keep the terraform state. If you want, you can add your service account as a member to bucket.
  • Create Compute Engine Image You can create a Debian 10 based VM and install nginx via sudo apt install -y nginx and test this terraform code. Don’t forget to sudo systemctl enable nginx command before turn your VM to image.

Usage

Here is the steps…

  1. Clone the repository


    git clone [email protected]:flightlesstux/gcp-instance-group-and-loadbalancer-w-terraform.git

  2. Authenticate to Google Platform if even google-sdk is not installed. It’s really useful for CI/CD pipelines! If you already logged in to your Google Cloud Platform project like gcloud auth login, you can skip this step.


    export GOOGLE_APPLICATION_CREDENTIALS=service-account-key.json

  3. Project ID is also declared via terraform but if you don’t export the value, you will probable get an error like below.


    export GOOGLE_PROJECT="0123456789012"

    │ Error: project: required field is not set
    │
    │ with google_compute_instance_group_manager.this,
    │ on group_manager.tf line 1, in resource "google_compute_instance_group_manager" "this":
    │ 1: resource "google_compute_instance_group_manager" "this" {



  4. Edit variable values


    variables.tf file.

  5. SSL Certificate Settings


    Paste your SSL certificate and the key file under certs folder . Loadbalancer needs to be secure! You can paste your Public key certificate and Certificate chain into certificate.crt.


  6. You can follow-up your production environment version easily with this variable. And also, Instance group need the new version for make a rolling update.


    export TF_VAR_deploy_version=v1

  7. Initilaze


    terraform init

  8. Let’s Deploy


    terraform apply or terraform apply -auto-approve


Repository Address

https://github.com/flightlesstux/gcp-instance-group-and-loadbalancer-w-terraform

I hope this article will help you.

Share on Social Media
x facebook linkedin reddit
GCP automationci/cdgcpgoogle cloud platformpipelineterraform

Post navigation

Previous post
Next post
  • AWS (45)
    • Serverless (4)
  • Best (9)
  • DevOps (16)
  • Docker (10)
  • GCP (3)
  • Kubernetes (3)
  • Linux (13)
  • Uncategorized (6)

Recent Posts

  • Automating AWS CloudWatch Log Group Tagging with Python and Boto3
  • Automating AWS ECR Tagging with Python and Boto3
  • Automating ECR Image Cleanup with Bash
  • Update ECR Repositories with Bash Script
  • Why Automated Tests Are Essential in Your CI/CD Pipeline and Development Flow
  • Streamline Your AWS ECR Management with This Powerful Bash Script
  • Setting up DKIM for Google Workspace (Gmail) using Terraform and AWS Route 53
  • Automate AWS Site-to-Site VPN Monitoring
  • Optimizing Docker Images: Tips for Reducing Image Size and Build Time
  • Monitoring EC2 Disk Space with a Simple Bash Script and Slack Alerts
  • Securing Docker Containers: Best Practices for Container Security
  • Mastering Dockerfile: Writing Efficient, Scalable Container Builds
  • Migrating a Git Repository from GitLab to GitHub with GPG-Signed Commits
  • Accessing AWS Services in Private Subnets Without 0.0.0.0/0
  • Understanding AWS Regions, Availability Zones, and VPCs: A Comprehensive Guide
©2025 Ercan Ermis | WordPress Theme by SuperbThemes