# How to deploy your Backend on Google Cloud Platform (for Newbies).

Google Cloud Platform (GCP) provides user-friendly services such as Cloud Run, which facilitates the deployment of backend applications with serverless scaling. This guide demonstrates using a basic Python Flask "Hello World" application on Cloud Run. Being container-based, it is particularly suitable for beginners working with Node.js, Python, or similar backend technologies.

Now to start with the whole deployment thing that we gonna do , first Install the Google Cloud CLI (gcloud) from the official site and run `gcloud init` to authenticate. after that enable billing on a new or existing GCP project . Dont worry you dont have to pay anything upfront as new users get $300 free credits from google , anyways after that just ensure APIs like Cloud Run and Cloud Build are enabled with `gcloud services enable run.googleapis.com cloudbuild.googleapis.com`.

Grant the Cloud Build service account the `roles/run.builder` role using `gcloud projects add-iam-policy-binding PROJECT_ID --member=serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com --role=roles/run.builder`.

## our sample backend app

before we go more into the gcp thing lets create a sample backend using python which we are going to deploy because ofc we need a app to deploy lol. alright now to do so :

Create a directory `my-backend` and add `main.py` inside it.

inside `main.py` paste this:

```plaintext
import os
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
    return f"Hello from GCP backend! (Port: {os.environ.get('PORT', 8080)})"
if __name__ == "__main__":
    app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))
```

Add `requirements.txt` with `Flask~=3.0` and `gunicorn~=23.0` for production serving.

## **Containerize Your App**

Cloud Run automatically builds from source using buildpacks, eliminating the need for a Dockerfile for Python applications—this is managed by the gcloud run deploy command. However, for Docker users, such as those using Node.js, it is necessary to create a `Dockerfile` that exposes port 8080 and specifies CMD `["gunicorn", "main:app"]`. To test locally, use gcloud run `services deploy --source . --local` after installing the necessary dependencies.

## **Deploy to Cloud Run**

In your app directory, run:

```shell
gcloud run deploy my-backend --source . --region us-central1 --allow-unauthenticated --port 8080

```

Accept prompts for service name, region (e.g., `asia-south1` near users), and public access. Deployment builds the container, pushes to Artifact Registry, and provides a URL like `https://my-backend-xyz.run.app` , cool now visit the url to verify to make sure everything is running fine.

Cloud Run scales to zero when idle, fitting free tier limits (e.g., 2 million requests/month).

## **Configure and Scale**

Set environment variables with `--set-env-vars KEY=VALUE` or CPU/memory via `--cpu 1 --memory 512Mi` during deploy. For production, add authentication (`--no-allow-unauthenticated`), custom domains, or connect to Cloud SQL for databases via VPC connectors. Monitor logs in GCP Console under Cloud Run > Logs; auto-scales based on traffic (up to 1000 instances).

## **Connect Database**

To utilize Cloud SQL with MySQL or PostgreSQL, first create an instance in the Console and obtain the connection string. During deployment, include the `option --add-cloudsql-instances INSTANCE_CONNECTION_NAME`.

Now For serverless applications, consider using Firestore(its very convient for new users), and it like offers like 1 GiB of free storage and 50,000 reads per day. Also ensure your application code is updated to incorporate the SQLAlchemy or `google-cloud-firestore` libraries in the requirements.txt file.Best Practices

Use CI/CD with Cloud Build triggers on Git pushes for auto-deploys. Monitor costs via Billing dashboard—stay under free tier by deleting unused services with `gcloud run services delete my-backend`. For traffic splitting or rollbacks, use GCP Console's versioning; secure with Cloud Armor if needed.​

## **Troubleshooting**

If the build fails, start by checking the logs in the **Cloud Build** section to see what went wrong. In many cases, the issue is simply that the application isn’t exposing `PORT=8080`, which **Cloud Run expects by default**. If you encounter permission errors, try running the required **IAM role grants again** and give it a minute or two for the changes to propagate. If the application deploys but doesn’t respond, double-check the **health checks and startup probes**, making sure the service is actually listening on port `8080`.

It’s also helpful to understand how this differs from **App Engine deployments**. **Cloud Run** provides much more flexibility because it runs containers, but that flexibility means you need to configure things like the listening port yourself. **App Engine**, on the other hand, is more opinionated and simpler for many Python projects you typically just define the configuration in `app.yaml` and deploy with `gcloud app deploy`.
