SHARE
How To Deploy Serverless Apps Across Locations Using IBM Cloud Functions

Today we’ll explore how we can use IBM Cloud Functions to be able to deploy serverless apps. Serverless platforms are used to allow developers to rapidly build APIs without the need of actual server machines. It’s mainly used for microservices apps to allow secure API authentication & actions. Highly useful for IoT, Cognitive, API Backends, Mobile Apps backend etc

To reduce network latency, the backend can be deployed in multiple locations and the developers will be able to use a single entry point with distributed traffic to the closest end. So, for this, we’ll use our public web app backend & deploy it on two locations.

We’ll also need, a custom domain to be able to use IBM Cloud Internet Services name servers and, install the necessary CLI tools.

How To Deploy Serverless Apps Across Locations Using IBM Cloud Functions

Get NS for Domain Name

To use the name servers, go to the Internet Service under IBM Cloud catalog. Create a service instance by choosing the plan and the name. After it’s provisioned – add your domain name. Then pick your assigned NS and use it for your domain through the registrar’s dashboard, such as GoDaddy.

Get SSL Certificate

It is essential to use an SSL certificate to create an end to end encrypted traffic flow. Get it for the root domain and the subdomains you want to use. You can also grab a free SSL from Let’s Encrypt project if you just want to test but, it’s always better to purchase on your own.

Once you have got the keys – convert them to a PEM format and import them using a Certificate Manager.

Defining Actions

There are two actions needs to be defined to manage your APIs. One is “doWork” that implements API operations & the second is, “healthz” that checks the health status of the APIs.

  • Go to Cloud Functions. Change to your target location and then create an action by setting the name to ‘doWork.
  • Set the ‘enclosing package’ to default. Set Runtime to the recent Node.js & then click ‘Create’
  • Change the action code to the following & save.
function main(params) {

  msg = "Hello, " + params.name + " from " + params.place;

  return { greeting:  msg, host: params.__ow_headers.host };

}

Similarly, create another set of action & name it ‘healthz’ with the same configuration as above. Then use this code & click save.

function main(params) {

  return { ok: true };

}

How To Deploy Serverless Apps Across Locations Using IBM Cloud Functions

Creating managed API

  • Again go to Cloud Functions & create a new managed API. Set the API name to “App API” and set base path to “/api”
  • Now, create an operation; set Path to “/do” and set verb to “GET”. Then set the package to “default” and action to “doWork” & click create.
  • For the next operation, set path to “/healthz” and set the action to “healthz” & create. Keep other settings as shown above and then save the API.

Use custom domain for managed API

  • We need to configure our subdomain to be able to handle managed API requests.
  • Go to APIs Custom Domains, change your target location. From custom domain link. click “Change Settings” under the actions menu. Note the domain alias value.
  • Click apply custom domain & set the domain name like ‘myapi.yourapp.com’. After that, select the certificate manager instance & choose the certs for the domain we added using PEM files in steps at first.
  • Go to Cloud Internet Services. Under DNS, create a new DNS TXT. Set the name of your subdomain like; ‘myapi’. Under content set the domain alias to what we noted on above step & then save everything.

We need to do this for all the different target locations.

Create an entry point, health check, load balancer & redirect

Now we need to create an entry point & the load balancer. That way, it can check for health & redirect users to the closest healthy location.

IBM Cloud Internet Services does a regular check on the endpoints we create to determine healthy backend. So, we need to first create a health check under IBM Cloud Internet Services.

  • Under Reliability > Global Load Balancers, set monitor type to “HTTPS” and set the path to “/api/healthz” and apply this provision.

Now we’ll create a pool with all target locations for use.

  • For every location, create the main pool. Set the name like “myapi-dallas” and select the health check we created above.
  • Set health check region closest to the location for which we created Cloud Functions for.
  • Set origin name to “app-dallas” and set origin address to default provisioned alias such as “7f3eed2df5.us-south.apiconnect.appdomain”

Create a load balancer & set its hostname to “myapi.yourapp.com”. Add regional pool & apply this provision.

Shortly after this, if you go to https://myapi.yourapp.com/api/do?name=Tushar&place=TechZoom, it will respond with the function running.