Google Cloud Functions

Google Cloud Functions are serverless code functions that run without you having to manage or scale the underlying infrastructure. This makes building them really easy. So let’s build an example.

Here’s normal NodeJS function with two parameters – request and response.

const translate = require(@google-cloud/translate')();
exports.translate = function(request, response){
     let language = request.body.language || 'es';
     translate.translate('Hello, there!', language,
     (err, translation) => {
      response.status(200).send(translation);
     });
 }

The incoming requst is automatically parsed for JSON body data, HTML form post data, and URL query string values.

Let’s use the language value from the incoming request and use it to translate “Hello, there” into that language.
Cloud Functions are automatically authenticated to other Google Cloud APIs, so we can call the translation API without managing service account or OAuth.

We deploy using gcloud command line:

gcloud functions deploy translate --trigger-http --stage-bucket 

Cloud Functions automatically create fully qualified domain names and generate an SSL cert so we can do HTTPS without any additional configuration.
Since those functions are Serverless we actually pay only for the time the function is actually running.