Deployment

1.)Overview

This guide provides a comprehensive walkthrough for deploying your multi-language compiler application—including the Rust backend, Next.js frontend, and PostgreSQL database—to a scalable, production-ready environment on Google Cloud Platform (GCP). We will use modern, serverless technologies to minimize infrastructure management.

Core GCP Services We Will Use:

  • Cloud SQL for PostgreSQL: A fully managed relational database service.

  • Artifact Registry: A private registry to store and manage your Docker container images.

  • Cloud Run: A serverless platform to run your containerized applications.

  • Secret Manager: For securely storing sensitive data like database passwords.

Prerequisites

Before you begin, ensure you have the following:

  1. A Google Cloud Platform Account: With an active billing account.

  2. Google Cloud CLI (gcloud): Installed and authenticated on your local machine.

  3. Docker: Installed and running on your local machine.


Step 1: Set Up Your GCP Project

First, create and configure a new GCP project.

  1. Create a Project:

  2. Set Project in gcloud CLI:

    Bash

    gcloud config set project YOUR_PROJECT_ID
  3. Enable Required APIs: Enable the APIs for the services we will be using.

    Bash

    gcloud services enable \
        sqladmin.googleapis.com \
        artifactregistry.googleapis.com \
        run.googleapis.com \
        secretmanager.googleapis.com

Step 2: Create the PostgreSQL Database

We will create a managed PostgreSQL instance using Cloud SQL.

  1. Create the Cloud SQL Instance: Choose a name for your instance (e.g., compiler-db-instance) and set a strong password.

    Bash

  2. Create the Database: Inside your new instance, create the specific database your application will use.

    Bash

  3. Store the Password in Secret Manager: It's best practice to store the database password securely.

    Bash


Step 3: Set Up Artifact Registry

This is where you will store your Docker images.

  1. Create a Docker Repository:

    Bash

  2. Configure Docker Authentication: This command configures your local Docker client to authenticate with your new Artifact Registry repository.

    Bash


Step 4: Build and Push Your Docker Images

Now, we will build the Docker images for your rust_compiler and web services and push them to the Artifact Registry.

  1. Build and Push the Backend (Rust) Image:

    • Navigate to the root of your project directory.

    • Define your image name. Replace YOUR_PROJECT_ID with your actual GCP Project ID.

      Bash

    • Build and push the image using your existing Dockerfile.

      Bash

  2. Build and Push the Frontend (Next.js) Image:

    • Navigate to your web directory.

    • Define your image name.

      Bash

    • Build and push the image.

      Bash


Step 5: Deploy the Services to Cloud Run

We will deploy the backend first, then the frontend.

5.1. Deploy the Backend Service

  1. Get the Cloud SQL Connection Name: You'll need this to securely connect Cloud Run to Cloud SQL.

    Bash

    (The output will look like YOUR_PROJECT_ID:us-central1:compiler-db-instance)

  2. Deploy to Cloud Run: This command deploys your backend container and securely connects it to the database.

    Bash

    • Replace <YOUR_CONNECTION_NAME> with the value from the previous command.

    • Important: The DATABASE_URL format is specific to Cloud SQL's proxy connection.

    • When prompted, allow unauthenticated invocations for now (for public access).

  3. Note the Service URL: After deployment, gcloud will print the Service URL. Copy this; you will need it for the frontend.

5.2. Deploy the Frontend Service

Now, deploy the Next.js frontend and point it to the backend service.

Bash

  • Replace <YOUR_BACKEND_SERVICE_URL> with the URL you copied after deploying the backend.


Step 6: Final Steps and Verification

  1. Apply Database Migrations: Your database is currently empty. The easiest way to apply Prisma migrations is to temporarily connect to your Cloud SQL instance from your local machine and run the command. Follow the Cloud SQL Proxy guidearrow-up-right for a secure connection, then run:

    Bash

  2. Access Your Application: Go to the URL of your next-frontend-service provided by Cloud Run. Your application should now be live, fully deployed on GCP!s

Last updated