Serverless Functions – What Are They?

Have you ever used an application/software and thought – Wow, how can an application as small as this possibly have so many features integrated into it? The answer you’re looking for probably has something to do with Serverless Functions and the Cloud – the new face of technology! AWS has been able to revolutionize its serverless computing by offering AWS lambda function as service.

From data analysis to cloud storage, from image processing to machine learning, the integration of modern-day applications and software with cloud-hosted serverless functions makes not only the life of a software developer easier, but it also promises increased reliability and security.

If you’ve programmed before, you’ve probably used a serverless function in order to achieve a complex task or a task that you simply couldn’t crack – you may not know it but you most probably have! In my opinion, the ability of a cloud service provider to make everything sound so simple greatly increases the chances of a common developer resorting to the use of serverless functions.

Why Use Serverless Functions?

Having trouble understanding? Let me give you an example –

You’ve been developing a mobile application for the past month and have lived out the dreaded life of a programmer – the coding has tested your capabilities and you’re looking to ease a few things – you just don’t know how. Your client tasks you with implementing a new feature – Image Processing! Your goal is to permit the user of the application to select an image from their phone and this image, in turn, will be tested for cracks and defects (you’re working on an application that will determine the market value of any cellular device, so the image will probably be of a phone). You have two choices –

  • Hard code the entire logic, using nothing but libraries, frameworks and languages you’ve had no experience with (Bear in mind that the concepts of Image Processing and Machine learning can be mind-boggling to novices!)
  • Simply connect your application to a serverless function that provides the same logic and is hosted on a cloud – What Amazon describes as a simple image recognition tool! (Ironic, right?)
READ  Using Redux in a React Native Application

By now, you’ve probably got an idea on what a Serverless Function is and what it does. Read on to find out more on building your own Serverless Function using services provided by Amazon Web Services.

Amazon Web Services – A Wonder!

Having been recognized as one of the Big Four companies, Amazon hasn’t failed to deliver and is continuing to strengthen its influence on major technological sectors and fields. You’ve probably heard of and seen many serverless functions before and today, I’m going to be showing you how create and invoke your own serverless function using AWS Lambda. For the sake of this tutorial, I’ll be using a NodeJS environment – Lambda works well with many languages – they all follow the same concept!

Step 1 – Create your AWS account and navigate to the AWS Lambda Service

Once you’ve set up your AWS account and navigated to the Lambda Service tab, you’ll have to create your Lambda function and customize it according to necessity. Go ahead and select Create Function on the right-hand side. You’ll be navigated to a preferences page.

Step 2 – Customizing your Lambda Function

Once you’ve clicked on Create Function, you’ll be redirected to this page. You’ll have to input a few details in order to proceed. I’ve named my function ‘nodeLambdaExample’ – feel free to name yours accordingly. My runtime is set no NodeJS – again, this depends on your chosen environment. Once you’ve entered the necessary information, click on Create Function.

Step 3 – Configuring/Testing your Lambda Function

Voila, you’ve just created your first Lambda function! Next, we’ll test our function to see that its working. On the top right-hand side, click on Select a test event and select Configure test events.

READ  Why you need an MVP

Step 4 – Configuring your Test Event

I’ve configured my test event to just contain two numbers (stored as JSON). You can modify your test event according to your necessity (This isn’t a mandatory step, it’s just to test the existence of our Lambda function. Don’t forget to give your test an Event name.

Step 5 – Testing your Function

Once you’ve created your test event, hit Save and click on Test. You should see a result being returned (This is a simple result template – you can view this by scrolling down)

Step 6 – Modifying your Lambda Function  

This step will involve you changing your Lambda Function according to your program necessity. You can accept arguments and perform operations etc – In my Lambda Function, I’ve simply created a return result that will verify my function using my NodeJS application. Once you’ve changed your Lambda Function, hit Save.

Step 7 – Getting your application ready

For this tutorial, I’ve using a simple NodeJS application that focuses mainly on the logic and server. I’ll be showing you how I connect the NodeJS server to my created Lambda Function.

First and foremost, install aws-sdk. This dependency is required in order to allow for your application to link with AWS.

Step 8 – Configuring your NodeJS application to run with your Lambda Function

This is a very crucial step – Modify your NodeJS application accordingly. In your AWS account, uder management console, your accessKeyId and secretAccessKey will be shown. Use those, along with your region (mine is us-east-2) and FunctionName (mine is nodeLambdaExample)

READ  Cloud Computing and its influence in modern technology

Step 9 – Running your application

Provided that you’ve configured your endpoints and keys properly, you should be able to see a result. I’ve simply logged my result from my Lambda Function onto my console (Line 31). As you can see, the data from my Lambda Function has been fetched and logged onto my console. You can modify your Lambda Function and allow it to receive data from your application and process information – the concept is practically the same!

Have fun coding!