Using Cloud Code functions as an AWS Lambda alternative

Using Cloud Code functions as an AWS Lambda alternative

With no shortage of application frameworks and cloud services, deciding how to launch your app involves many factors. Speed, price, and community support all play an essential role. Two increasingly important points you should consider are ease of development and deployment. In this article, I look at two solutions- Back4App Cloud Code functions, and AWS’s Lambda cloud service- and how they fare for these cases.

Say you want to launch an application from scratch. Very likely, you’ll need to consider the following issues:

  1. Authentication
    1. Password Reset
    2. Email Verification
  2. Object persistence (saving information in the database)
  3. File Storage (uploading profile pictures)
  4. Permissions
  5. Managing Backend
    1. Deploying server
    2. Maintaining server
    3. Integrating changes
    4. Examining data/analytics
  6. Data Permissions

 

Serverless Computing, a backend approach, has helped tremendously with the infrastructure related problems (see underlined). It’s made deployment and integration fast and straightforward. Furthermore, it’s lowered the price for computing as you pay for just what you use.

AWS Lambda

AWS Lambda is a particularly well-known serverless service. Developers can build full backends with Lambda using the Serverless Framework. This library manages deployment to different cloud providers. For example, you can deploy an Express application to run on a Lambda.

Lambda- and other equivalent cloud solutions- certainly make things easier for developers. But there’s still more that’s missing out of the box. We often end up reinventing the wheel on the non-infrastructure related components (see items in list not underlined). Why should you have to integrate an authentication provider for every new app you create?

In fact, a Lambda is not sufficient on its own to build an application. As I’ll explain in this article, you will need to provision and integrate a separate database as well as a file storage system.

To handle HTTP requests with a Lambda, you would need to configure an API Gateway. Since the Serverless Framework manages this for you, I won’t discuss this.

Back4App

Enter Parse. Parse is an open-source platform that can be integrated into any type of application. You simply add the SDK corresponding to whatever front-end framework you are programming with. The library couples with a Parse Server, providing robust solutions for the rest of the list. Parse presents the flexibility of creating dynamic queries from the frontend, along with the dependability of a standardized API.

 Back4App marries these two concepts- the Parse SDK and Serverless Computing. You end up with the best of both worlds and an ideal development pipeline. In this article, I’ll discuss some of the key ways that employing Back4App’s infrastructure will save you time. Most importantly, it allows you to focus on solving interesting and unique problems, rather than redoing boilerplate work.

Boilerplate Server Configuration

Configuring endpoints is supposed to be easy with modern server frameworks. Take an example from express, one of the simplest to set up (taken mostly from an app generated by express generator).

/**
* boilerplate imports
*/
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var app = express();
var users = express.Router();
/**
* Not boiler plate
*/
users.get('/api/route', function(req, res) {
res.send({success : true})
})
users.post("/api/route", function(req, res) {
// more code here
})
/**
* More boilerplate
*/
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/users', users);

 

Express makes defining routes simple. But imports can be redundant and annoying, especially if all you want to do is create a JSON REST API. Here is the equivalent of the above in a Parse Cloud Code Function

Parse.Cloud.define("users:get", async (request) => {
return {success : true}
}) 
Parse.Cloud.define("users:create", async (request) => {
 // More code here
})

That’s it. The Parse SDK is already imported for you, and you can return an object OR Promise which will be transmitted in JSON.

CORS

If you’re building a REST API, you probably want to have your HTML served separately from your REST API. The Express snippet above would fail in that case; you would also need to configure CORS.

The NPM ‘cors’ module works great for this. Regardless, it’s more configuration and dependencies that come out of the box with Parse Cloud Code Functions.

Data persistence

Virtually all useful backends require some sort of data persistence. This means a database that your server will interact with. For a Lambda application, you would have to do the following:

  • Provision a database using some Cloud provider (will usually cost money)
  • Configure some sort of secure connection string
  • Install a database driver into your server code
  • Connect to the database using the driver.

Obviously, this hassle detracts from the “easy deployment” part of serverless.

With Back4App, you have to do exactly zero database integration. The Parse SDK wraps a database layer, so creating, retrieving, updating, and deleting objects comes for free. This typically will save you hundreds of line of code.

Here’s a quick comparison of an Express app (running on a lambda) vs a Back4App application.

Express

Dependency: Mongoose


const mongoose = require('mongoose');
// Remember: you’ll have to set up an external mongo database too!
mongoose.connect('your_data_base_string);
const postSchema = new mongoose.Schema(
  {
    text: String
  },
  { timestamps: { createdAt: 'created_at' } }
);
const PostModel = mongoose.model('Post', postSchema);
router.get('/posts', (req, res) => {
  // Would typically add error handling as well
  PostModel.findOne({
    /* Query information */
  })
    .exec()
    .then(results => {
      res.send(results);
    });
});

 

Back4App


Parse.Cloud.define('posts:get', async (req, res) => {
 	 return new Parse.Query('Post').first();
});

 

Parse isn’t quite an ORM so you may have to monitor your schema more closely than with the Mongoose solution. Regardless, it becomes clear that the second example with Back4App involves far less overhead.

Authentication, Authorization, Permissions

If you’re writing code for a Lambda, authentication will consume a substantial portion of your time. In addition to the database setup needed to store users (see above), you also will likely need to: 

  • protect routes such that people can only make authorized requests successfully.
  • Create a signup process
    • An email verification service is an even more work
    • Probably needs a password reset too
  • Implement a login route
  • Design some way so that the server knows who the user is- for example, session storage or JSON web tokens

Parse, on the other hand, ships with all these things plus a robust permission system. The platform diminishes the divide between the frontend and the backend which helps tremendously with authentication.

File Storage

Many applications require a way to upload pictures or attachments. On say, a Lambda function, you’ll need to:

  • Provision a file server for storage (like AWS S3)
  • Create a couple routes that handle uploads.

Considering how difficult file uploads can be on the web, I have a deep appreciation for the following Parse interface, taken from the documentation

var base64 = "V29ya2luZyBhdCBQYXJzZSBpcyBncmVhdCE=";
var file = new Parse.File("myfile.txt", { base64: base64 });

OR


var bytes = [ 0xBE, 0xEF, 0xCA, 0xFE ];
var file = new Parse.File("myfile.txt", bytes);

Additionally, referencing files in other objects is also supported.

Browsing Data

As mentioned throughout this guide, you can browse and manipulate data out of the box with Back4App. The platform exposes a Dashboard, an elegant UI for managing many facets of your application. This includes:

  • Manually adding objects
  • Viewing server logs
  • App Analytics
    • Number of requests
    • Performance
    • Adjustable by date range

With a Lambda Function, you also can access logs and analytics. But browsing data is not supported. Replicating this functionality would involve creating an admin user role, and exposing a special API to this type of user. Designing an interface on the frontend would definitely be the most intensive part.

Deployment

Lambda configurations boast of an easy deployment system, especially with the Serverless Framework. However, the Back4App Cli has a few advantages. Specifically:

  • No need to actually deploy your Parse Server. Back4App just uploads the Cloud Code.
    • Serverless uploads usually span 20MB to 200MB, as it archives all your dependencies and then transfers the zip
    • A Back4App upload will take one or two seconds
  • You can manage configuration settings dynamically from your Back4App Dashboard instead of redeploying

Conclusion

The combination of the Parse SDK with a Serverless infrastructure makes Back4App an ideal choice for a backend. Lambda Functions can also be powerful but provide way less out of the box. Taking advantage of solutions that have already been implemented allowing you to do more real developing, faster.


Leave a reply

Your email address will not be published.