0%

Introduction to Google Cloud Functions

Serverless

What is Serverless?

No server, right?

  • No need to manage servers
  • No need to even think about servers
  • No need to provision infrasturcture
  • Pay only for what you use
  • It’s not about billing, it’s about developer experience
  • Deploy functions not apps (FaaS)
  • Event-oriented
  • Only applies to compute services
  • Also applies non-compute services

Serverless lets you write and deploy code without the hassle of managing the underlying infrastructure. It enhances developer productivity by helping focus on what matters the most - building great applications and abstracting away the rest. Zero server management, no upfront provisioning, auto-scaling to meet traffic demands, and paying only for the resource used are just some of the benefits of serverless computing.

Why use serverless computing?

Building serverless applications means that your developers can focus on their core product instead of worrying about managing and operating servers or runtimes, either in the cloud or on-premises. This reduced overhead lets deverlopers reclaim time and energy that can be spent on developing great products which scale and that are reliable.

References

Google Cloud Functions

A serverless environment to build and connect cloud services with code.

What are Google Cloud Functions?

Cloud Functions, a faster way to build in the cloud. You focus on writing your code and Cloud Functions makes sure your application runs resiliently, cost effectively, and at planet-scale allowing you to move faster than ever before.

Google Cloud Functions can be summarized as a serverless compute solution for creating event-driven applications. An event could be something like a change to data in a database or files added to a storage system. You can create a response to that event with a trigger. A trigger is an indication that you are interested in a certain event or set of events. Adding a Cloud Function to the trigger allows you to response to that event. Whether you’re building applications for the enterprise, e-commerce, consumers, or anyone else, the Cloud Functions makes it easy. It connects and extends your cloud services with code so that you can treat them as building blocks, move fast, and adjust as your needs change.

Because the software and infrastructure are fully managed by Google, you only need to provide the code. No provisioning virtual machines, no installing operating system updates, and no installing OS security patches. A function can scale from a few requests to millions per day without any work from you. With Cloud Functions, you only pay when your code is running. If your function isn’t being invoked, you aren’t billed anything. This is especially great for services with uneven or unpredictable traffic patterns.

Function Types

Cloud Functions are event-driven, which means they are executed when cloud events occur. Cloud Functions come in two flavors, HTTP functions and Background functions. HTTP functions are triggered by HTTP requests and can be used for things like webhooks and creating APIs. Background functions are triggered when certain cloud events occur.

HTTP Functions

Synchronous invocation over HTTPS. Cloud function will return you a URL and a TLS certificate.

An HTTP function is a function you deploy with an HTTP trigger, and will give you back immediately a URL with a TLS certificate. So, it’s secure and you can curl that straightaway. That would be for synchronous use cases. The webhook callback will work out-of-the-box for this.

helloWorld.js
1
2
3
4
// Function triggered via HTTPS
exports.helloWorld = function helloWorld (req, res) {
res.send('Hello, world!');
}

The above is an example of HTTP function in Node. The request and response is literally an Express.js request and response. So, if you have ever used Express.js, then you know how to do this.

helloHttps.js
1
2
3
4
5
// Function triggered via HTTPS
exports.helloHttps = function helloHttps (request, response) {
let message = 'Hello, ' + request.body.name;
response.status(200).send(message);
}

It’s just a regular request response and you have the full access to the response object. You don’t need to do any sort of crazy translations on the way in or the way out. It’s just regular HTTP.

Because Cloud Functions abstracts away the infrastructure, you don’t need much code and essentially no environment configuration. Cloud Functions automatically parses the body of the incoming JSON request, so we can access the name property and greet our user.

Background Functions

Asynchronously invoked by cloud events.

This is where it happens asynchronously. There’s an event bus in between, typically Cloud Pub/Sub. This is where you don’t need to know the outcome of the function in real time. You’re happy for it to be processed in the background. And typically, this will be as a result of an event being emitted from a cloud service.

helloBg.js
1
2
3
4
5
// Function triggered via event
exports.helloBg = function helloBg (event, callback) {
// Do something
callback();
}

Cloud Functions currently responds to Pub/Sub, Cloud Storage, and Firebase events. This opens up use cases like lightweight data transformation, the processing of IoT message coming through Pub/Sub. Or Cloud Functions could watch your cloud project logs to take some actions, like do a deploy when a container is finished building or notify you when a virtual machine is deleted.

References