This guide provides instructions for configuring and deploying the Myflix application to a production environment using Google Cloud Run and Firestore.
- Frontend: A React application that is served by the backend.
- Backend: A Node.js/Express server that serves the frontend and provides a content API.
- Database: Google Firestore is used as a persistent NoSQL database to store movie and category information.
- Deployment: The entire application is containerized using Docker and deployed as a stateless service on Google Cloud Run.
- Google Cloud Platform (GCP) Account: You need a GCP account with billing enabled.
- gcloud CLI: The Google Cloud SDK installed and authenticated on your local machine.
- Docker: Docker Desktop installed on your local machine.
-
Create or Select a GCP Project:
- Go to the GCP Console and create a new project or select an existing one.
- Note your Project ID.
-
Enable Required APIs:
- In your project, enable the following APIs:
- Cloud Run Admin API
- Cloud Build API
- Cloud Firestore API
- You can do this by navigating to "APIs & Services" > "Library" in the console.
- In your project, enable the following APIs:
-
Create a Firestore Database:
- Navigate to "Firestore" in the GCP console.
- Click "Create database".
- Choose Native Mode.
- Select a location (e.g.,
us-central1). - Click "Create".
To run the server locally while connected to your live Firestore database, you need to authenticate using a service account.
-
Create a Service Account:
- In the GCP console, go to "IAM & Admin" > "Service Accounts".
- Click "+ Create Service Account".
- Give it a name (e.g.,
myflix-server-dev). - Grant it the "Cloud Datastore User" role (this role provides permissions for Firestore).
- Click "Done".
-
Download the Key File:
- Find the service account you just created in the list.
- Click on the three-dot menu under "Actions" and select "Manage keys".
- Click "Add Key" > "Create new key".
- Choose JSON as the key type and click "Create".
- A JSON file will be downloaded to your computer. Keep this file secure and do not commit it to version control.
-
Set Environment Variable:
- Move the downloaded JSON file to the root of this project and rename it to
gcp-credentials.json. - Set an environment variable that points to this file. The Node.js client library will automatically find and use it.
- In your terminal (for macOS/Linux):
export GOOGLE_APPLICATION_CREDENTIALS="[PATH_TO_YOUR_PROJECT]/gcp-credentials.json"
- For Windows (Command Prompt):
set GOOGLE_APPLICATION_CREDENTIALS="[PATH_TO_YOUR_PROJECT]\gcp-credentials.json"
- Move the downloaded JSON file to the root of this project and rename it to
-
Install Dependencies:
npm install
-
Start the Server:
npm start
- The server will start, connect to your Firestore database, and seed it with initial data if it's empty.
-
Configure gcloud CLI:
- Set your project:
gcloud config set project [YOUR_PROJECT_ID] - Set your region:
gcloud config set run/region [YOUR_CHOSEN_REGION](e.g.,us-central1)
- Set your project:
-
Deploy the Service:
- Run the following command from the root of your project directory:
gcloud run deploy myflix-service --source . --allow-unauthenticated- This command will:
- Use the Cloud Build API to build a container image from your
Dockerfile. - Push the image to the Artifact Registry.
- Deploy the image to a new Cloud Run service named
myflix-service. - The
--allow-unauthenticatedflag makes the service publicly accessible.
- Use the Cloud Build API to build a container image from your
-
Access Your Application:
- After the deployment is complete, the
gcloudcommand will output a Service URL. You can access your fully deployed application at this URL. - Note: On Cloud Run, you do not need the
gcp-credentials.jsonfile. The service runs with the identity of the default Compute Engine service account, which has sufficient permissions to access other services like Firestore within the same project.
- After the deployment is complete, the