Save up to 216$/y/domain on your GCP bill by connecting Cloud Run to Firebase
No bs. Εxplanation included.
Note: See at the end of post for savings breakdown.
Let’s connect firebase with Cloud Run!
Requirements
Node.js @ latest
Step 1: Install the firebase CLI
npm i -g firebase-tools
Step 2: Ensure you are logged in to google via browser.
Step 3: Create a Firebase project
Click Add Project
Enter a name and click Continue
Keep Google Analytics enabled and click Continue
Choose or create a Google Analytics account or select an existing one.
Click Create Project
Wait for the process to finish and click Continue. You’ll be redirected to the project dashboard
Step 4: Enable Pay-as-you-go
We need to enable Pay-as-you-go, so our custom domains don’t shut down! See more on the cost breakdown below!
Go to the bottom left corner of the dashboard. Click Upgrade , next to Spark
Select or create a GCP billing account. Its easy to create if you don’t already have one
Click Continue then Purchase
Pay as you go is now enabled!
Step 5: Deploy your apps to Cloud Run
For help, you can refer to a similar tutorial I made, up until step 7
Step 6: Get your firebase project ID
On the firebase dashboard:
click the Gear Icon on the top left, on the right of Project Overview
go to Project Settings > General > Your Project
Copy the Project ID value and store it somewhere.
Step 8: Create hosting sites
On the firebase dashboard:
click Hosting on the top left, under Project Shortcuts
For each of your apps you want to deploy, click the Add another site button on the right of your screen, and give it a Unique Name (unique_name.web.app)
Copy each Unique Name and store it somewhere
Step 8: firebase.json
Go somewhere in your desktop (eg downloads/temp) and create a firebase.json file with the following
{
"$schema": "https://raw.githubusercontent.com/mainrs/vscode-firebase-configuration-schema-plugin/master/schemas/firebase.schema.json",
"hosting": []
}
We’re gonna fill out the hosting array
Step 9: Login to firebase via the CLI
firebase login
Step 10: Create hosting targets for the unique names
For each of the Unique Names you created, apply a hosting target alias by running:
firebase target:apply hosting <alias> <unique_name>
Step 11: Fill the hosting array
Head over to https://console.cloud.google.com/run
Use this page as a reference to the names and regions of your Cloud Run services
The schema for a hosting element
{
"target": "<hosting target alias (step 9)>",
"rewrites": [
{
"source": "/*",
"run": {
"serviceId": "<name of cloud run service>",
"region": "<region of cloud run service>",
"pinTag": true
}
}
]
}
Example
Cloud Run Service Name: api
Cloud Run Service Region: us-central1 (Iowa)
Firebase Hosting Site Unique Name: api-ybbe34t3
I run:
firebase target:apply hosting cr-api api-ybbe34t3
Then the schema:
{
"target": "cr-api",
"rewrites": [
{
"source": "/*",
"run": {
"serviceId": "api",
"region": "us-central1",
"pinTag": true
}
}
]
}
Step 12: Deploy!
firebase deploy --only hosting --project <project ID from step 6>
Step 13: The custom domains.
head over to the firebase console
look top-left on the sidebar > “Hosting”
click “View” on one of your sites
in the middle of the screen, you will see a “Add custom domain” button. Click it
Insert the domain you want to use, click Continue
Change the DNS records of your domain accordingly
That's it!
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Cost Breakdown
Load Balancing on GCP costs:
261$/y for the first 5 rules (0.025$/hr)
86,4$/y for every other one (0.01$/hr)
There is no cost for Networking
Hosting on Firebase costs:
15 cents / GB of Ingress/Egress
Some other costs that don’t apply (BC of Cloud Run)
For a small company with 1TB Networking and 5 Cloud Run services, in a year:
5 * 261$ = 1305$ for GCP
1024 * 0,15$ = 130,6$ for Firebase!
When should I move to GCP?
When your cost of Networking/ hr exceeds your GCP LB costs/hr