Cloud Functions is Google Cloud’s event-driven compute platform. It allows
small code segments to perform specific, limited tasks. Cloud Functions is serverless, which means you can create and implement the service without having to provision the underlying cloud infrastructure -- such as storage, servers and other resources. That said, setting up and using Cloud Functions is particularly quick and easy, making it a great way to add a small piece of functionality to your business or project without having to make major changes to your application’s code.
Writing your function
At the moment Cloud Functions only supports: Python 3.7, Node.js 8 and Go 1.11. A few different versions are in beta (Go 1.13 and Node.js 10), but if you don’t know one of those three languages, you may need to brush up.
Adding your function
There are three primary methods to get your code into Cloud Functions. First, the good old reliable ctrl+c, ctrl+v method. This is called source and it is where you have to fully embrace your inner cowboy by manually typing (or copying) the code into Cloud Functions. The other two options are a little less exciting:
2. Use a zip file. You can either upload one directly to Cloud Functions and select one that’s been stored in Google Cloud Storage.
3. Use Cloud Source Repositories (Google’s Git repository system). The added benefit of using this method is that Google will keep everything synced. Any changes you make in Cloud Source Repositories. will reflect in Cloud Functions automatically. There are other ways to keep your functions synced, I invite you to have a look if you are interested - Deploying Cloud Functions
Triggering your function
Right, so you have your code in Cloud Functions, now what? You need to trigger it. This is where Cloud Functions starts to really shine. The three methods available to you are with Cloud Storage, with Cloud Pub/Sub, or via HTTP.
If you set Cloud Storage as your trigger, your function will trigger whenever there are changes to the files in a selected bucket. The function will trigger when files are added, deleted, archived or have their contents or metadata edited. This offers a number of useful applications, especially with automated data processing.
The next option is Cloud Pub/Sub. Cloud Functions can be triggered by messages published to Pub/Sub topics in the same Cloud project as the function.
The last option is the most versatile. When you select HTTP, Google creates a unique URL for your function to which you can send JSON e.g. https://<region>-<project>.cloudfunctions.net/<function-name>. This allows you to easily automate all sorts of things. Need a way to collate your data? Just send it through with a post request. Need a way to create, organise and allocate customer support tickets? Link it up to a form. Want to automatically dislike everything your ex posts on - never mind. Cloud Functions has you covered. Since security by obscurity is no security at all, there is the option for only authenticated users to make requests too. This makes use of service accounts.
Irrespective of what option you choose, Cloud Functions tracks each invocation to give you insightful metrics about how your function is being used. This includes the amount of invocations, how long tasks are taking, the memory usage of each request, and the amount of active instances you have.
Paying for your function
But how much does all this cost, you ask? Like most cloud services, it depends on how much you use. Whenever your Cloud Function is triggered, a new instance of your function is created. Once the instance has done its job, it hangs around for a bit in case a new request comes in. This is called an idle instance and you are not charged for these. To allow for sudden spikes in activity, there is always a set amount of idle instances waiting.
What you do pay for is the invocations - but barely! No matter how long your function is active, and no matter the outcome, your first 2 million invocations per month are free. Yes, 2 million. After that, it skyrockets (not really) to $0.0000004 per invocation, which works out to be $0.40 per million invocations a month.
Yes, you still need to pay for the underlying compute resources being used, but with the right infrastructure setup, you can run a basic Cloud Functions, Compute Engine instance and database for next to nothing. You can read more on the pricing here - Cloud Functions Pricing.