Application Development

Cloud Functions serverless platform is generally available

It was a busy week for serverless computing at Google Cloud Next ‘18 in San Francisco. In addition to the updates we shared from the show, we covered our Cloud Functions technology in more than 15 technical sessions. There are a lot of things to unpack, ranging from new feature releases to best practices. This post is your one-stop summary of Cloud Functions: what’s new, sign-up instructions for early access features, and some tips on where you can find more information on useful technical sessions and insights from Next ‘18.

Cloud Functions is Google Cloud’s event-driven serverless platform that offers flexible, fast cloud app development. We launched Cloud Functions to beta last year at Google Cloud Next ‘17 with Node.js runtime and important features like HTTP/S in the box, allowing you to deploy a function and call it directly with no additional services required. Cloud Functions beta also came with fine-grained pricing, so you only pay while your code is running, billed to the nearest 100 milliseconds. Cloud Functions was also designed to work well with existing Google Cloud Platform (GCP) services like Cloud Storage, Cloud Pub/Sub, and Stackdriver. In the time between then and now, we have been doing a lot of work adding new capabilities to the platform and improving its reliability and performance.

During Next ’18 we announced that Cloud Functions is now generally available and ready for production use, with predictable service guaranteed by an SLA and a global footprint with new regions in Europe and Asia. Here’s what else is new.

New language runtimes: Node 8, Python 3.7

python node.js

You can now write Cloud Functions using Node 8, which brings in support for async/await and a new function signature. Cloud Functions now provides data and context as opposed to an event, and you can use the await keyword to await the results of asynchronous operations. This makes a much better mental model for dealing with multiple asynchronous operations. Here’s a look at the code differences between Node 6 and Node 8 behavior:

Current (Node 6) behavior

  exports.helloCallback = (event, callback) => {
  const payload = event.data.data;
  doSomething(payload, callback);
};

exports.helloPromise = (event) => {
  const payload = event.data.data;
  return doSomethingWithAPromise(payload)
};

New (Node 8+) behavior

  // Note the new (optional) context object
exports.helloPubsub = async (data, context) => {
  return await doSomethingAsynchronous(data)
};

// HTTP Functions are unchanged, but also support async/await
exports.helloHTTP = async (req, res) => {
  return await doSomethingAsynchronous(req)
};

You can also now write Cloud Functions using Python 3.7. Just like with Node, you get data and context for your background functions, and request for HTTP. Python HTTP functions are based on the popular Flask microframework, which allows you to get up and running really fast. Here’s a look at a Flask request:

  def hello_pubsub(data, context):
  return do_something(data)
  
def hello_http(request):
  return do_something(request)

The requests are based on flask.Request and the responses just need to be compatible with flask.make_response. As with Node, you get data (dict) with Python background functions and context (google.cloud.functions.Context). To signal completion, you just need to return from your function or raise an exception and Stackdriver error handling will kick in. And, similarly to Node (package.json), Cloud Functions will automatically do the installation of all of your Python dependencies (requirements.txt) and build in the cloud.

Cloud Functions for Firebase

Cloud Function Firebase

Cloud Functions for Firebase is also generally available and has full support for Node 8, including ECMAScript 2017 and async/await. This release also comes with additional granular controls for runtime configuration options, including region, memory, and timeout, allowing you to fine-tune the behavior of your applications. You can find more details here.

Firebase events (Analytics, Firestore, Realtime Database, Authentication) are also directly available in the Cloud Functions Console on GCP. This provides you with an additional degree of flexibility with your application stack by allowing you to trigger your functions in response to the Firebase events directly from your GCP project.

Support for headless Chrome and additional system libraries

ubuntu

We are also rebasing the underlying Cloud Functions operating system onto Ubuntu 18.04 LTS to broaden the scope of libraries available. In addition to imagemagick, available since beta launch, you now get access to additional system libraries such as ffmpeg and libcairo2, as well as everything required to run headless Chrome. For example, you can now process videos and take web page screenshots in Chrome from within Cloud Functions.

Set environment variables

Set Environment Variables

You can now pass configuration to your functions by specifying key-value pairs that are bound to a function but don’t exist in your source code. Environment variables are set at the deploy time using the --set-env-vars argument, and are injected into the environment during execution time. You can find more details here.

Cloud SQL direct connect

SQL

You can now connect Cloud Functions to Cloud SQL instances using a fully managed secure direct connection. Learn more about connecting to Cloud SQL here.

Coming soon to Cloud Functions

We’ve also got lots of new features coming soon to Cloud Functions. Here’s what you can expect:

Scaling controls

Scaling Controls

With the addition of the  --max-instances scaling control, you can limit the number of instances on a per-function basis to limit the traffic. This can help to deal with sudden traffic spike scenarios when Cloud Functions rapidly scales up and overloads a database or general prioritization between more and less important parts of your system. Sign up for the alpha here.

Serverless scheduling

Severless Scheduling

With Cloud Scheduler, you’ll be able to schedule Cloud Functions down to one-minute intervals invoked via HTTP(S) or Pub/Sub. This allows you to execute Cloud Functions on a repeating schedule, which is particularly useful for things like daily report generation or regularly processing dead letter queues. Sign up for the alpha here.

Compute Engine VM access

Compute Engine VM Access

You can now connect to Compute Engine VMs running on a private network using --connected-vpc option. This provides a direct connection to compute resources on an internal IP address range. Sign up for the alpha here.

IAM security controls

IAM Security Control

Cloud Functions makes it really easy to expose an HTTP(s) endpoint: just deploy your function with an HTTP trigger and we'll provide a dedicated HTTPS URL. The new Cloud Functions Invoker IAM role allows you to add IAM security to this URL so you can control who can invoke the function using the same security controls you use elsewhere in Cloud Platform. Sign up for the alpha here.

Serverless containers

Severless Containers

With serverless containers, we are providing the same underlying infrastructure that powers Cloud Functions, but you’ll be able to simply provide a Docker image as input. This will allow you to deploy arbitrary runtimes and arbitrary system libraries on arbitrary Linux distributions while still retaining the same serverless characteristics as Cloud Functions. Sign up for the alpha here.

Our favorite breakouts from Next ‘18, all on-demand

Here is our handcrafted shortlist of the must-see Cloud Functions technical talks from Google Cloud Next ‘18 San Francisco. (See this playlist for the entire application development track.)

What’s new:

Getting started:

Tips and tricks:

Working with Google services outside of GCP:

Running in production:

Building with microservices:

We’ll keep you updated with Cloud Functions news and features. If you would like to start trying out Cloud Functions, Google Cloud offers a free tier so you can try using Cloud Functions today. TIll next time, happy building.