Use case 

In this walk through, I’ll be showing you how to build and deploy REST APIs using Serverless Framework and Amazon Web Services. 

In this use case we will be building REST APIs to get temperature information of the city requested by client. These APIs will fetch data from free weather APIs provided by ‘openweathermap.org’. These weather APIs provide lots of data about weather based on location, area etc. We will modify the data as per our requirements and return the output to the user. 

We will build these APIs with AWS Lambda and Amazon API Gateway.This service will not be continuously running, so the physical / virtual server setup will be a wastage of resources. That’s why Serverless Architecture will be best suited for our use case. 

Let’s talk about what Serverless Architecture is and how it’ll help us.

 

What is Serverless Architecture? 

It is a way with which you can build and deploy applications which are hosted by a third-party service. This eliminates the need for software and hardware management by the developer. 

The advantage of such architecture lies in the decoupling of individual functionalities, enabling small distributed teams using different programming languages to develop their services. 

 

Why Serverless Architecture?

An application requires managing of either virtual or physical servers along with the Operating System and the Web Hosting process. 

Cloud service providers like AWS and Google provide virtual servers which eliminates concern about physical maintenance of the server but still require management of OS and Web Hosting process.

Following are some key features which makes Serverless Architecture unique –

Zero administration :

You can deploy the code without having to provide or manage anything before or after. There is no concept of having to own a server an OS or anything else that you usually would need to buy.

Auto Scaling : 

Let service providers manage the scaling challenges. No need to fire alerts or write scripts to scale up and down. Handle quick bursts of traffic and weekend lulls.

Pay Per Use : 

It is very cost effective also as you are charges as per your usage of the service. You can completely utilize all the resources you need without having to pay for your idle time.

 

Our Serverless Architecture 

In the Architecture diagram above, 

Client makes a request to the API Gateway. Then, the API Gateway will enrich the request (validation for mandatory query parameters/headers) and forward that request to the Lambda Function. The function definition is stored in S3 Artifact which is dynamically loaded whenever Lambda is invoked. After Lambda is invoked the function code will request weather APIs for weather information. After receiving data from APIs, the data will be transformed to meet our requirements (temperature of the city asked by user).

Lets dive deep into each component of the architecture

AWS API Gateway :

Amazon API Gateway is an AWS service for creating, publishing, maintaining, monitoring, and securing REST and WebSocket APIs at any scale. API developers can create APIs that access AWS or other web services as well as data stored in the AWS Cloud.

AWS api gateway supports both stateful (websocket) and stateless (rest) apis, but for our use case REST apis are best suited. Since aws lambda can be easily integrated with api gateway we can invoke aws lambda with request from api gateway.

AWS Lambda : 

AWS Lambda is provided by Amazon as a part of the Amazon Web Services which is an event-driven, serverless computing platform. It is a computing service which automatically manages all the resources required to compute that code and eventually runs it.

We will be using Java 8 but AWS Lambda also supports the following runtime environments.

  1. Java 8
  2. Go
  3. Node js
  4. Python
  5. C#

AWS S3 :

AWS S3 is a short form for Amazon Simple Storage Service. This is an Amazon Web Service that uses a web service interface to provide object storage. Objects can be anything HTML, ZIP, json, binary files. With proper roles and authorization, the object can be accessed by url which is generated using path of the object in a bucket along with the region of the bucket. The code for aws lambda will be stored in a bucket.

 

Implementation 

Development tools Used: 

  1. Java 8
  2. Spring Tool Suite / Eclipse

Step 1: 

We have to use Java SDKs for Lambda function implementation. 

We will import two Maven dependencies for these SDKs. 

The first dependency will provide classes and interfaces for events which will invoke Lambda functions (like S3 event when an object is modified, API Gateway events).

Second dependency will provide core classes and interfaces for AWS Lambda.

 

Step 2 :

Create a class which will implement ‘RequestHandler’ interface provided by AWS Java SDK. 

This interface provides a signature method which is the starting point of the Lambda function execution. 

This handler gives APIGatewayProxyRequestEvent as input to the Lambda and APIGatewayProxyResponseEvent as response.

 

Step 3 :

Extract query parameters from the request if further processing needs those query parameters (like start date and end date, which might be useful to query third party api). We will extract the city name from query parameters, it will be used to fetch the temperature of the city. 

Create POJOs (Plain Old Java Object) to map response of the APIs. Use RestTemplate provided by Java library to communicate with REST APIs from Java code. The rest-template will return response entity with body of type ‘weather info’.

WeatherInfo.java

 

MainInfo.java

 

Handler.java (function-1)

 

Handler.java (function-2)

 

Deployment

For deploying Lambda functions we have multiple options like

  1. Serverless framework
  2. Terraform v0.11.7
  3. SBT 1.0.4
  4. Scala 2.12.6

We will use Serverless Framework to deploy Lambda on AWS Lambda. Because the Serverless Framework is the simplest way to develop scalable APIs.  A single configuration file allows to list all functions and define the endpoints that they’re subscribed to. Also all stages of the deployment can also be defined using single file.

Create a yml file named ‘serverless.yml’ with following content.

After creating the file hit command ‘serverless deploy’, to deploy code to the aws lambda.

Conclusion

By following the above steps you can build and deploy RESTful services on AWS without having to deal with Virtual Machines or any Operating System.

Hope this Blog helps you at every stage from Build to Deploy. If you have any questions or want us to work on something like this for you, feel free to reach out to us!

 

 

Visuals Credits:
Banner Image: LINK
Architecture Diagram: Meghna Sonie

Akshay Patil