Going Serverless with GCP - YAML to JSON App

 Let's do some Google Cloud Platform

Feels like a big change for me as I am usually in the AWS platform for clients, but found some time to have a play with the GCP suite for making serverless Apps. Nothing fancy, let's do a basic app that will turn JSON to YAML in a language I don't usually dabble in... Why not try Node?! Ok Here we go!

Getting all signed up!

Firstly things first, we will need to grab ourselves an account with Google and be on the look out for any developer/trainer accounts as they sometimes get given to us for free!

Deployment

How should this be deployed? Well of course if we are doing things properly, we can do this with Terraform, works across the Cloud platforms and gets us to where we need to be fast without all that clicking around. We surely had enough of that getting our developer accounts to start with right?

On to the Magic!

First, create a new Terraform configuration file and define the provider for GCP, I like to name mine providers.tf to break out the code base and direct future developers to where certain Terraform elements are located:

provider "google" {
  project = "my-project"
  region  = "us-central1"
}
Our credentials should be configured from the command line and kept outside of our providers.tf file to allow versatility in deployment, before running any Terraform commands we can authenticate with Google using:

gcloud auth application-default login --no-launch-browser

If we are not in a place to use a web browser for authentication we can copy and paste the returned link to where we can, gain an authorisation code to complete the login process. Because of the above commands a number of environment variables will be set that allow us access to the Google Cloud Platform with Terraform. 

Next we will need to create create a Google Cloud Function resource and specify the runtime as Node.js:

resource "google_cloud_function" "json_to_yaml" {
  name     = "json_to_yaml"
  runtime  = "nodejs12"
  trigger_http = true
}

Set the function's code by specifying the location of the code file and the entry point for the function:

resource "google_cloud_function" "json_to_yaml" {
  source_archive_bucket = "my-bucket"
  source_archive_object = "source.zip"
  entry_point           = "jsonToYaml"
}

And now the NodeJS...

To transform the JSON input into YAML, you can use a Node.js library called js-yaml. Include this library in your package dependencies by adding it to the package.json file:

{
  "dependencies": {
    "js-yaml": "^3.14.0"
  }
}

Now, you can create the function that will transform the JSON input into YAML. In the code file, import the js-yaml library and create the function that will handle the transformation:

const yaml = require('js-yaml');


exports.jsonToYaml = (req, res) => {
  const json = req.body;
  const yamlOutput = yaml.safeDump(json);

  res.set('Content-Type', 'text/yaml');
  res.send(yamlOutput);
};

Finally, to deploy the function, run the following command:

terraform apply

This will create a new Cloud Function that can be triggered by an HTTP request, and will transform the JSON body of the request into YAML output. We can test this with the following command.

curl --header "Content-Type: application/json" --request POST --data '{"username":"xyz","password":"xyz"}' \ http://<End Point Address>

Wrap Up!

And there you have it a small Serverless self contained application that will allow us to turn our JSON strings into YAML. Not the most advanced example but illustrates the basic Serverless concept, in future blogs we can have a look at the more advanced examples and growing out our Infrastructure in to fully grown Applications.