What is Google Cloud Functions?
Cloud Functions is a serverless environment for deploying Node.js applications. It is completely no-ops; you don’t have to provision or manage servers. It is inexpensive; it costs 40 cents per million requests, but the first 2 million are free.
Cloud functions run in response to triggers. A trigger could be a change to a Cloud Storage bucket, a message sent to a Pub/Sub topic, or a request to a URL.
There are many examples. Monitor a bucket and when a picture is uploaded to the bucket, use the Vision API to label the picture. Or, monitor a Pub/Sub topic and when a message comes in, process it and send it to BigQuery.
Cloud Functions are Microservices
A microservice is a service that does one thing. So I got to thinking, why can’t my micro-service be a web application? That’s just one thing.
If I were going to build a Node.js web app, I would use a framework called Express. Node.js Express is similar to Python Flask or ASP.NET MVC.
Express allows you to define routes. Routes are URLs that point to functions that return data. One route could be a page request which returns HTML. Other routes could define API calls that return JSON data. In the end, it’s the same thing. In response to an HTTP request, run some code and return a string.
There are just a couple tricks you need to know to deploy your Express app to Cloud Functions.
Be Careful Though
When your app goes viral, after 2 million requests, you will have to pay 40 cents every time another million requests come in!