How to Easily Write Your First Serverless Application

存储架构 2018-02-14

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.

Manjula Piyumal

Feb. 14, 18 ·
Cloud Zone

Free Resource

Join the DZone community and get the full member experience.

Are you joining the containers revolution? Start leveraging container management using Platform9's ultimate guide to Kubernetes deployment .

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.

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.

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:

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.

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.

Using Containers? Read our Kubernetes Comparison eBook to learn the positives and negatives of Kubernetes, Mesos, Docker Swarm and EC2 Container Services.

DZone

责编内容by:DZone (源链)。感谢您的支持!

您可能感兴趣的

使用阿里云函数计算和 Websocket,轻松开发 Serverless 实时应用... 函数计算能够让开发者无需管理服务器(Serverless),只需要编写函数代码就能快速搭建应用。并且函数计算能够弹性地伸缩,用户只需要按使用量进行付费。 如今越来越多的应用(特别是移动端)需...
Here’s everything you need to know about serverles... Here’s everything you need to know about serverless architectures Cloud Computing...
Implementing an event-driven architecture on serve... Part 2 In this article, we’re going to explore how to build an event-driven architecture on serve...
GraphQL and Serverless – Where Cloud Computi... While there’s been a lot of discussion around the impact of microservices at the software architectu...
Twistlock 2.3 Advances Container Security with Ser... Container security vendor Twistlock released version 2.3 of its container security platform on Jan. ...