0
0
GCPcloud~5 mins

Cloud Run vs Cloud Functions decision in GCP - CLI Comparison

Choose your learning style9 modes available
Introduction
Sometimes you need to run your code in the cloud without managing servers. Cloud Run and Cloud Functions both do this but work differently. Choosing the right one helps your app run smoothly and saves money.
When you want to run a simple function that responds to events like file uploads or messages.
When you need to run a full containerized app that can handle web requests with custom dependencies.
When you want automatic scaling without worrying about infrastructure.
When you want to pay only for the exact time your code runs.
When you want to deploy code quickly without building a container.
Commands
This command deploys a simple Cloud Function named helloWorld using Node.js 18 runtime. It triggers on HTTP requests and allows public access.
Terminal
gcloud functions deploy helloWorld --runtime nodejs18 --trigger-http --allow-unauthenticated
Expected OutputExpected
Deploying function (may take a while)...done. availableMemoryMb: 256 entryPoint: helloWorld httpsTrigger: url: https://REGION-PROJECT_ID.cloudfunctions.net/helloWorld runtime: nodejs18 serviceAccountEmail: PROJECT_NUMBER-compute@developer.gserviceaccount.com status: ACTIVE updateTime: '2024-06-01T12:00:00Z'
--runtime - Specifies the language runtime environment
--trigger-http - Sets the function to be triggered by HTTP requests
--allow-unauthenticated - Allows public access without authentication
This command deploys a containerized app to Cloud Run named my-app. It uses a container image stored in Google Container Registry, runs on managed Cloud Run in us-central1, and allows public access.
Terminal
gcloud run deploy my-app --image gcr.io/my-project/my-app-image --platform managed --region us-central1 --allow-unauthenticated
Expected OutputExpected
Deploying container to Cloud Run service [my-app] in project [my-project] region [us-central1] ✓ Deploying new service... Done ✓ Creating Revision... ✓ Routing traffic... Done. Service [my-app] revision [my-app-00001-abc] has been deployed and is serving 100 percent of traffic. Service URL: https://my-app-abcdefg-uc.a.run.app
--image - Specifies the container image to deploy
--platform - Specifies Cloud Run platform type
--allow-unauthenticated - Allows public access without authentication
This command calls the deployed Cloud Function helloWorld to test it works.
Terminal
gcloud functions call helloWorld
Expected OutputExpected
result: Hello from Cloud Functions!
This command sends an HTTP request to the Cloud Run service URL to check the app is running.
Terminal
curl https://my-app-abcdefg-uc.a.run.app
Expected OutputExpected
Hello from Cloud Run!
Key Concept

If you remember nothing else from this pattern, remember: Cloud Functions are best for simple event-driven code, while Cloud Run is best for full container apps needing more control.

Common Mistakes
Trying to deploy complex apps as Cloud Functions without containers
Cloud Functions have limited runtime and environment control, causing failures or limitations
Use Cloud Run to deploy containerized apps with full control over dependencies and runtime
Not setting --allow-unauthenticated flag when public access is needed
The service will reject public requests, causing 403 errors
Add --allow-unauthenticated flag during deployment to allow public HTTP access
Using Cloud Run for very simple functions that don't need containers
It adds unnecessary complexity and cost for simple event-driven tasks
Use Cloud Functions for simple, short-lived event-driven code
Summary
Deploy Cloud Functions for simple event-driven code triggered by HTTP or events.
Deploy Cloud Run for containerized apps needing more control and custom dependencies.
Use gcloud commands to deploy and test both services with proper flags for access.