Day_8 : Deploying a Docker Environment on Google Cloud Platform with Terraform

Day_8 : Deploying a Docker Environment on Google Cloud Platform with Terraform

Introduction

In today’s rapidly evolving technological landscape, the ability to quickly deploy and manage infrastructure is crucial. Infrastructure as Code (IaC) tools like Terraform allow developers and system administrators to automate the provisioning and configuration of cloud resources, enabling more efficient and consistent deployments. In this blog post, we’ll explore how to use Terraform to deploy a Docker environment on Google Cloud Platform (GCP).

Code file :- https://github.com/Ingole712521/Terraform_Expert/tree/main/Day_8_GCP


Explanation

Google Cloud Platform (GCP) offers a plethora of services and features for building, deploying, and managing applications in the cloud. Docker, on the other hand, is a popular containerization platform that enables developers to package their applications and dependencies into lightweight, portable containers.

In this tutorial, we will leverage Terraform, a widely-used IaC tool, to automate the deployment of a virtual machine (VM) instance on GCP and install Docker on it. Let's break down the Terraform configuration file step by step:

data "google_compute_image" "demo" {
  family  = "ubuntu-2204-lts"
  project = "ubuntu-os-cloud"
}

Here, we're defining a data source to retrieve the Ubuntu 22.04 LTS image from the Google Cloud Platform project "ubuntu-os-cloud".

locals {
  region            = var.region
  availability_zone = var.zone
}

This block defines local variables for the region and availability zone based on input variables.

resource "tls_private_key" "ssh" {
  algorithm = "RSA"
}

This resource generates an RSA private key that will be used for SSH access to the VM instance.

resource "google_compute_instance" "demo" {
  // Configuration for the Google Compute Engine VM instance
}

This block defines a Google Compute Engine instance resource named "demo".

metadata_startup_script = <<EOT
  // Shell script to be executed on instance startup
EOT

Here, we define a startup script that will be executed when the VM instance starts. This script updates the package list and installs Docker CE.

resource "google_compute_firewall" "demo-ssh-ipv4" {
  // Configuration for the firewall rule allowing SSH traffic
}

This block defines a firewall rule that allows SSH traffic to the VM instance.

resource "local_file" "local_ssh_key" {
  // Configuration for saving the generated private key to a local file
}

This block saves the generated private key to a local file on the machine running Terraform.

output "instance_ip" {
  // Output variable for the instance's public IP address
}

This block defines an output variable for the public IP address of the VM instance.

output "instance_ssh_key" {
  // Output variable for the path to the generated SSH private key file
}

This block defines an output variable for the path to the generated SSH private key file.


Demo

Main.tf file

varible.tf file

provider.tf file

Final Result :-

  1. ssh_key creation

  1. Output of main.tf file

  1. Output from GCP

  2. Deleting


Video

Conclusion

Terraform simplifies the deployment of Docker environments on Google Cloud Platform (GCP), offering efficiency and consistency. This tutorial has walked through the Terraform configuration, showcasing how it orchestrates the provisioning of GCP resources. Additionally, a video demonstration accompanies this guide, providing a visual walkthrough of the deployment process. By leveraging Terraform and GCP, developers can automate infrastructure management, focusing more on building and deploying applications. Happy deploying!

Connect with us: