DEV Community

Cover image for πŸš€ How to Quickly Deploy a Containerized Nginx Server on Google Cloud Using Terraform
Surender Gupta
Surender Gupta

Posted on

πŸš€ How to Quickly Deploy a Containerized Nginx Server on Google Cloud Using Terraform

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
Enter fullscreen mode Exit fullscreen mode

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"
}
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

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"]
}
Enter fullscreen mode Exit fullscreen mode

5. πŸ” Authenticate to Google Cloud

Before running Terraform, authenticate with Google Cloud:

gcloud auth application-default login
Enter fullscreen mode Exit fullscreen mode

6. βš™οΈ Initialize Terraform

In your project directory, run:

terraform init
Enter fullscreen mode Exit fullscreen mode

7. πŸ“‹ Review Plan

Generate a plan to preview the infrastructure Terraform will create:

terraform plan
Enter fullscreen mode Exit fullscreen mode

8. πŸš€ Apply Terraform Configuration

Provision the infrastructure by applying the configuration:

terraform apply
Enter fullscreen mode Exit fullscreen mode

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://<your-instance-ip>
Enter fullscreen mode Exit fullscreen mode

You should see the default Nginx welcome page! πŸŽ‰

Output of Nginx server

10. 🌐 Clean Up Resources

When done, tear down the resources to prevent unnecessary costs:

terraform destroy
Enter fullscreen mode Exit fullscreen mode

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

Firewall Rules

2. VPC Network

VPC Network

3. VM Compute

GCP VM


✨ 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

Neon image

Serverless Postgres in 300ms (!)

10 free databases with autoscaling, scale-to-zero, and read replicas. Start building without infrastructure headaches. No credit card needed.

Try for Free β†’

Top comments (0)

AWS Industries LIVE! Stream

Watch AWS Industries LIVE!

Watch Industries LIVE! to find out how the AWS cloud helps solve real-world business challenges.

Learn More

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay