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:
A Google Cloud Platform Account: With an active billing account.
Google Cloud CLI (
gcloud): Installed and authenticated on your local machine.Docker: Installed and running on your local machine.
Step 1: Set Up Your GCP Project
First, create and configure a new GCP project.
Create a Project:
Go to the GCP Console and create a new project (e.g.,
my-compiler-app).Make a note of your Project ID.
Set Project in gcloud CLI:
Bash
gcloud config set project YOUR_PROJECT_IDEnable 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.
Create the Cloud SQL Instance: Choose a name for your instance (e.g.,
compiler-db-instance) and set a strong password.Bash
Create the Database: Inside your new instance, create the specific database your application will use.
Bash
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.
Create a Docker Repository:
Bash
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.
Build and Push the Backend (Rust) Image:
Navigate to the root of your project directory.
Define your image name. Replace
YOUR_PROJECT_IDwith your actual GCP Project ID.Bash
Build and push the image using your existing
Dockerfile.Bash
Build and Push the Frontend (Next.js) Image:
Navigate to your
webdirectory.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
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)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_URLformat is specific to Cloud SQL's proxy connection.When prompted, allow unauthenticated invocations for now (for public access).
Note the Service URL: After deployment,
gcloudwill 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
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 guide for a secure connection, then run:
Bash
Access Your Application: Go to the URL of your
next-frontend-serviceprovided by Cloud Run. Your application should now be live, fully deployed on GCP!s
Last updated