0
0
GCPcloud~5 mins

Why serverless functions matter in GCP - Why It Works

Choose your learning style9 modes available
Introduction
Serverless functions let you run small pieces of code without managing servers. They automatically handle scaling and only charge you when your code runs, saving time and money.
When you want to run code in response to events like file uploads or database changes without managing servers.
When you need to quickly deploy small features or APIs without setting up infrastructure.
When your app traffic changes a lot and you want automatic scaling without manual work.
When you want to pay only for the exact time your code runs, not for idle servers.
When you want to focus on writing code and not on server maintenance or updates.
Commands
This command deploys a serverless function named 'helloWorld' using Python 3.9. It triggers on HTTP requests and allows anyone to call it without login.
Terminal
gcloud functions deploy helloWorld --runtime python39 --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 name: projects/PROJECT_ID/locations/REGION/functions/helloWorld runtime: python39 status: ACTIVE updateTime: '2024-06-01T12:00:00Z'
--runtime - Specifies the language runtime version
--trigger-http - Sets the function to run on HTTP requests
--allow-unauthenticated - Allows public access without login
This command calls the deployed serverless function via its HTTP URL to test if it works.
Terminal
curl https://REGION-PROJECT_ID.cloudfunctions.net/helloWorld
Expected OutputExpected
Hello, World!
This command fetches the last 5 log entries for the 'helloWorld' function to check its activity and debug if needed.
Terminal
gcloud functions logs read helloWorld --limit 5
Expected OutputExpected
2024-06-01 12:01:00 helloWorld: Function execution started 2024-06-01 12:01:01 helloWorld: Hello, World! response sent 2024-06-01 12:01:01 helloWorld: Function execution finished
--limit - Limits the number of log entries returned
Key Concept

If you remember nothing else from this pattern, remember: serverless functions let you run code without managing servers, automatically scaling and charging only when your code runs.

Common Mistakes
Deploying a function without specifying the trigger type
The function won't know when to run and deployment will fail or the function won't be callable.
Always include a trigger flag like --trigger-http or --trigger-topic when deploying.
Not allowing unauthenticated access when testing public HTTP functions
You will get permission denied errors when trying to call the function publicly.
Use --allow-unauthenticated flag during deployment for public HTTP functions.
Calling the function URL before deployment finishes
The function won't respond because it is not ready yet.
Wait for the deployment command to complete successfully before calling the URL.
Summary
Deploy a serverless function with gcloud specifying runtime and trigger.
Test the function by calling its HTTP URL with curl.
Check function logs to monitor execution and troubleshoot.