Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Easily Write Your First Serverless Application

DZone's Guide to

How to Easily Write Your First Serverless Application

If you've never used a serverless application before, use this tutorial to build, deploy, and test a simple project using AWS.

· Cloud Zone ·
Free Resource

Insight into the right steps to take for migrating workloads to public cloud and successfully reducing cost as a result. Read the Guide.

The serverless paradigm is currently one of the trending topics in tech news. I strongly believe it will be the future of application development, including modern systems and applications such as cloud, mobile, and IoT, as well as the disruption of traditional business and enterprise applications.

However, the learning curve around the environments of cloud platform providers and configuration complexity have become main bottlenecks for the movement towards the serverless paradigm.

Sigma to the Rescue

With the recent release of Sigma IDE from SLAppForge, now serverless application development becomes much easier because it streamlines the design, development, and deployment of serverless applications, concealing the underlying platform-level complexities from developers, which has slowed down the widespread adoption of serverless computing.

Image title

Didn’t try out it yet? I strongly recommend you to give it a try if you are a serverless fan, it’s completely free. I’m pretty sure you will love it due to the intuitiveness and simplicity of the IDE.

If you are a person who loves the serverless concept, but couldn’t try out anything practical yet due to the complexity around that, this blog will be a good starting point for you. I’m going to implement a simple calculator application using AWS Lambda and API Gateway.

Your First Serverless Application

First, you need to register to Sigma IDE (all you need is a valid email address) and create a project  for the calculator application. Once you create your application, you should get an IDE environment like below.

Image title

This is the IDE view for your AWS Lambda function. The left side panel of the IDE contains currently supported AWS resources in Sigma and it contains a frequently used set of resources. For this application, we are going to use API Gateway only.

In our case, we need to get an HTTP request and do the calculating operations based on the received parameters of the input message and the responnse with the resulting value after the calculation operations. Let’s assume that sample input message would be like this,

{
  "Operation": "Add",
  "LeftParameter": 123,
  "RightParameter": 394
}


The sample output message would look like this:

{
  "Operation": "Add",
  "LeftParameter": 123,
  "RightParameter": 394,
  "Result": 517
}


Configure API Gateway as the Input Trigger

As the starting point of our Lambda implementation, we need to configure API Gateway as the input trigger for our Lambda function. For that, you just have to drag the API Gateway resource from the left side panel and drop it on the “event” variable of the handler method declaration (event variable is highlighted in red, once it’s configured, it should become green). Once you drop your API Gateway resource, you should get a popup view to configure your API to access the calculator application. Fill out that as shown below:

Image title


Now we have to implement our calculator logic to facilitate required operations. The complete logic for facilitating basic operations would be like this:

let AWS = require('aws-sdk');
exports.handler = function(event, context, callback) {
    console.log("Received request with payload", event);
    let operation = event.Operation;
    let result = null;
    switch(operation) {
        case "Add":
            result = event.LeftParameter + event.RightParameter;
            break;
        case "Subtract":
            result = event.LeftParameter - event.RightParameter;
            break;
        case "Multiply":
            result = event.LeftParameter + event.RightParameter;
            break;
        case "Devide":
            result = event.LeftParameter / event.RightParameter;
            break;
        case "Mod":
            result = event.LeftParameter % event.RightParameter;
            break;
        default:
            result = "Operation Not Permitted";
            break;
    }
    event.Result = result;
    callback(null, event);
}


Save, Build and Deploy

Now you can click on the deploy button on the top toolbar to deploy your project on AWS. Once you click on the deploy button, it will commit the project to your GitHub account and then build the project for you. Once building is completed, it will automatically deploy your application on AWS services.

Once the deployment is completed, you should get a confirmation popup with the details (publically accessible URL) of your deployed API. You have to copy that URL from that popup to access your deployed calculator API.

Image title

Testing

To test your serverless application, you just have to send a request to the above URL from a tool like Postman.

Voila! We have successfully implemented, built, deployed, and tested our first serverless application within a single IDE UI without a hassle. That’s the magic of Sigma IDE, which seamlessly integrates your serverless application development environment.

TrueSight Cloud Cost Control provides visibility and control over multi-cloud costs including AWS, Azure, Google Cloud, and others.

Topics:
serverless ,aws ,aws lambda ,lambda ,serverless development ,serverless computing ,cloud

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}