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.

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’

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”

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

IBM Developer Day event is happening in Bangalore on 14th March 2019. The entire event is designed for developers by developers, combining the top curated technical talks, open source code, hands-on workshops, coding expertise & lots more. Register for IBM Developer Day today.