Tech Stack
- Google Cloud Platform (GCP)
- Terraform (v1.5+)
- Google Cloud SDK (for authentication)
- Container-Optimized OS (COS)
- Docker
Introduction
In today’s cloud-native world, automation is a game-changer. In this tutorial, we'll guide you through the process of deploying a containerized Nginx server on Google Cloud, all with Terraform—the go-to tool for Infrastructure as Code (IaC). This step-by-step guide is perfect for DevOps engineers looking to streamline their cloud infrastructure setup and improve their automation skills.
By the end of this tutorial, you’ll have a fully containerized Nginx server running on GCP, ready for further customization or deployment in production.
Skills You'll Gain in This Tutorial
- How to provision GCP VM instances using Terraform
- Deploying a Dockerized Nginx server on GCP
- Setting up firewall rules for secure access
- Writing production-grade, reusable Terraform configurations
- Automating cloud infrastructure from scratch
Complete Walkthrough: Deploying Nginx on GCP
1. 📂 Project Structure
First, create the project folder and the required files:
terraform-gcp-nginx/
├── main.tf
├── variables.tf
├── output.tf
2. ✍️ Define Variables in variables.tf
In variables.tf
, specify essential configuration values like your project ID, region, and zone:
variable "project" {
description = "The GCP project ID"
type = string
}
variable "region" {
description = "The GCP region"
type = string
default = "us-central1"
}
variable "zone" {
description = "The GCP zone"
type = string
default = "us-central1-a"
}
3. ✍️ Create Output File (output.tf
)
Add an output to show the public IP of the deployed VM:
output "instance_ip" {
description = "Public IP of the VM instance"
value = google_compute_instance.vm_instance.network_interface[0].access_config[0].nat_ip
}
4. ✍️ Terraform Configuration in main.tf
Your main Terraform configuration should include VM provisioning, firewall setup, and the Nginx container initialization:
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "6.8.0"
}
}
}
provider "google" {
project = var.project
region = var.region
zone = var.zone
}
resource "google_compute_network" "vpc_network" {
name = "terraform-network"
}
resource "google_compute_instance" "vm_instance" {
name = "terraform-instance"
machine_type = "f1-micro" # Free tier eligible
tags = ["web", "dev"]
boot_disk {
initialize_params {
image = "cos-cloud/cos-stable" # Container-Optimized OS
}
}
network_interface {
network = google_compute_network.vpc_network.name
access_config {}
}
metadata = {
startup-script = "docker run -d -p 80:80 nginx"
}
}
resource "google_compute_firewall" "default-allow-http" {
name = "allow-http"
network = google_compute_network.vpc_network.name
allow {
protocol = "tcp"
ports = ["80"]
}
direction = "INGRESS"
source_ranges = ["0.0.0.0/0"]
target_tags = ["web"]
}
resource "google_compute_firewall" "allow-ssh-rdp-iap" {
name = "allow-ssh-rdp-iap"
network = google_compute_network.vpc_network.name
allow {
protocol = "tcp"
ports = ["22", "3389"]
}
direction = "INGRESS"
source_ranges = ["35.235.240.0/20"]
target_tags = ["web"]
}
5. 🔐 Authenticate to Google Cloud
Before running Terraform, authenticate with Google Cloud:
gcloud auth application-default login
6. ⚙️ Initialize Terraform
In your project directory, run:
terraform init
7. 📋 Review Plan
Generate a plan to preview the infrastructure Terraform will create:
terraform plan
8. 🚀 Apply Terraform Configuration
Provision the infrastructure by applying the configuration:
terraform apply
You’ll be prompted to confirm the changes—type yes to proceed.
9. 🌐 Access Your Nginx Server
Once Terraform finishes applying, it will output the public IP of your VM. Open a browser and navigate to:
http://
You should see the default Nginx welcome page! 🎉
10. 🌐 Clean Up Resources
When done, tear down the resources to prevent unnecessary costs:
terraform destroy
Review the plan and confirm by typing yes. This will remove the VM, firewall rules, and VPC network.
Conclusion
By following this guide, you’ve successfully deployed a containerized Nginx server on Google Cloud using Terraform. You now know how to:
- Provision GCP resources with Terraform
- Set up Docker containers on GCP VMs
- Manage access with firewall rules
- Clean up cloud resources efficiently
This approach can easily scale with load balancing and autoscaling features, making it a great starting point for production workloads.
Bonus Tip
To add resilience to your infrastructure, consider:
- Implementing a Google Cloud Load Balancer in front of your Nginx server
- Adding auto-scaling policies to ensure high availability and efficient resource usage
Resources Created
1. Firewall
2. VPC Network
3. VM Compute
✨ Thank you for reading! ✨
I hope this article helped simplify the process and gave you valuable insights. As I continue to explore the ever-evolving world of technology, I’m excited to share more guides, tips, and updates with you.
Stay tuned for more advanced Terraform guides and cloud automation tips!
Let’s keep learning and growing together! 💡
🔧 #Terraform #GCP #CloudComputing #DevOps #InfrastructureAsCode #Docker #Nginx #Automation #CloudEngineering #GoogleCloud