1. 程式人生 > >Create a multiregional http monitor in less than five minutes with google cloud function

Create a multiregional http monitor in less than five minutes with google cloud function

Create a multiregional http monitor in less than five minutes with google cloud function

These days, everybody is migrating his architectures to microservices and the highest exponent of this paradigm is the Function-as-a-Service (FaaS). But, why is FaaS so popular?

The main benefit is that we can focus on our code and define the requirements to execute it. We don’t need to bother about dealing with infrastructure because it’s provided by the solution vendor.

Resultado de imagen de faas

Other benefits are:

  • Pay only for the execution time of functions (cost-per-use)
  • Scales automatically without human interaction (until the service limits)
  • Fast time-to-market

The way to execute functions is to associate them with triggers that will be fired to response to certain events (for example, a HTTP request, the upload of a file to the storage, etc.)

The cloud functions are suitable for the following use cases:

  • Microservice
  • IOT
  • API backend
  • Web apps
  • Data processing
  • Chatbots
  • Webhooks

In our example, we are going to make use of Google Cloud Functions, the serverless environment on Google Cloud Platform.

The cost associated with the service is based on the following concepts:

  • Invocations: requests made to our function
  • Compute Time: measured as the time your function needs to finish running after it receives a request
  • Networking: outbound data transfer

At the moment, Cloud Functions has a Free Tier that includes up to 2 million free invocations and 1 million seconds of free compute time per month (offer can change in the future, see Pricing). It works with Node.js 6 and, recently, Node.js 8 and Python 3.7 in beta.

That’s enough theory, let’s get on with the practice. We want to create a HTTP monitor in multiple regions and, to that end, we are going to use Python with Cloud Functions and then run a test.

First, click on “Cloud Functions” from the side menu on the right

If it’s the first time that you access Cloud Functions, you need to enable the Cloud Functions API

After that, press “Create function”

Then you have the following edit options:

  • Name: choose a descriptive name for your function
  • Memory allocated: choose one of the memory — CPU pairs made available (remember, the cost is a functions of execution time):
  • Trigger: select what with start the execution of the function. The options are HTTP, Pub/Sub and Cloud Storage (in beta: Cloud Firestore, Google Analytics for Firestore, Firebase authentication and Firebase Realtime Database). If we select “HTTP”, you’ll see the service URL below (syntax: https://<region>-<project>cloudfunctions.net/<function_name>)
  • Source code: you can select how you’re going to deploy your function. The options are: inline editor (it’s the option we are going to use because we want to create a quick test), zip upload, zip from Cloud Storage or Cloud Source repository (the best option for continuous delivery)
  • Runtime: as we mentioned before, Node.js 6 is the only option in Global Availability; Node.js 8 and Python 7.3 are in beta. If you select the HTTP trigger and Python, you have to take into account that Python runtime uses Flask framework to handle incoming requests.
  • Function to execute: function exported by the module to execute

Important: three more options are hidden (click over “More”):

  • Region: we can select from us-central-1, europe-west-1, asia-northeast-1 or us-east-1 (see more info). Remember that the service URL depends on the chosen region.
  • Timeout: the period of time after the execution of the function is considered to be failing (it could be very expensive if there are long requests)
  • Environment variables: you can get the desired environment variables to use inside your function

Let’s review the code (Github). We are going to use Pycurl module (PycURL is a Python interface to libcurl) to measure the navigation time and the status code of the request. To use it inside our function, we have to add it to the requirements.txt

We are going to edit our function inside the GCP console (we maximize the editor by pressing the grey square on the right) and then save it.

After saving, it needs less than 1 minute to be in production. Then we can open a browser and test our function (sorry for the red line, I have to mask the URL to avoid unauthorized access to this service)

In terms of monitoring, we could review the function performance in the “General” tab inside the function home. We can choose to view the “Execution time”, “Invocations” and “Memory usage”

If we have any issue during the deployment or we want to review the exceptions, we could click on “View logs”

Then the Stackdriver Logging page is opened and it shows the function logs in near real-time (you can be filtered by severity, time, etc)

“That’s all folks” . And don’t miss the next episode of the Bluekiri’s team