Serverless framework with AWS – Example

Published by Vignesh M on

Serverless is the architectural style in which the applications relies heavily on Backend as a Service(Baas) and Function as a Service(Faas). In Baas, the third party remote application services are integrated tightly into the client side of apps, for example Firebase. Whereas in FaaS, the server side logic is written as functions code using the services provided by Cloud, for example AWS Lambda.

What is Serverless framework?

With the Serverless architecture being defined, what is that Serverless framework?

Serverless framework is the tool which helps to build serverless applications and to deploy and manage it effectively. In short, we can focus on our application with out worrying about infrastructure.

Serverless framework with AWS

In this post, let’s see a simple example of using Serverless framework with AWS as Cloud service provider.

Before start using the Serverless framework we need the following tools available.

  1. AWS account
  2. AWS CLI  and configuring the Credentials
  3. NodeJS
  4. Serverless framework

After having the necessary tools, let’s start writing the application.

The source code for this application  is available here. The download link is also provided at the end of this post.

Serverless Application

In this simple serverless example application, we are going to hit the API gateway from the html page. The API gateway will then trigger the Lambda function which will return the response. Then we will handle the response and display the output in html page.

Yes. Probably the simplest example possible. And we are going to use the serverless framework to create the resources needed for this application.

serverless.yml

The serverless framework supports writing the configurations in YAML. So in serverless framework, we can define the resources needed for our application as a configuration. Then the framework will automate the resource creation using CloudFormation internally.

Below listing shows the configuration for our application.

The serverless.yml have the sections for defining the framework version, Cloud service provider, runtime environment, the Lambda function details and it’s event triggers.

Lambda function

Now we can start writing our Lambda function which will execute when the request is received from API gateway. Basically it’s a place to write our business logic for the request received from browser client.

In the serverless.yml, if you notice the line handler: backend/index.handler which indicates that the framework is going to expect a index.js file inside a folder named backend. In the index.js file, there should be a function named handler exported as module.

Below listing shows our code for Lambda function.

Deploy the application

To deploy the application, go to the project folder which contains the serverless.yml file. Then open the command prompt.

Issue the command sls deploy .  Alternatively you can give serverless deploy , both will do the same action of deploying the resources.

The resources are now created and deployed into the AWS Cloud. The framework outputs the API gateway endpoint in the console. This is because we have mentioned API gateway as a event trigger for Lambda function in the configuration file. Make a note of the endpoint URL, which we will use in our example shortly.

Note: The API gateway URL listed above in the image might not be available for you for testing. However you can configure the one for you on following this example.

If you notice the functions name in the console, it’s aws-serverless-dev-main

aws-serverless is the service name mentioned in configuration.  dev is the default staging name for our application. We can configure other names such as prod, pre-prod. main is the function name configured in serverless.yml.

In the cloud formation console, you can view the stack created by serverless framework internally. In the Events, you can also see the entries of resource creation such as Lambda function, API gateway, IAM roles.

A simple client

We are almost done. Now to test our application, we need a simple client which will run on browser and invoke the API gateway.

Below listing shows our index.html file.

Note: Replace the YOUR_API_GATEWAY_URL string with the URL that you will get when configuring the API gateway.

Test the application

Finally it’s all done. Now we can test our application. Open the index.html in a web browser. You should see the below screen rendered.

Click on the Get Welcome Message button. You should get the welcome message and the welcome image displayed as below.

Clean up the resources

With serverless framework, it’s very simple to cleanup the resources we have created for the application. Just issue the command sls remove. The CloudFormation stack will be deleted and you are all done cleaning up.

Conclusion

We have seen a simple example of using serverless framework. While developing this application, we haven’t used the AWS management console to get the things done. That is one of the key advantage of serverless framework. Because it means the creation , deployment and management of resources are automated.

What’s next? We can start building the applications using serverless architectures and automate the resource management using serverless framework.

Thank you for reading this post. If you have any questions, please share it in the comments section.


Download source code : aws-serverless-example.zip



Vignesh M

Java developer , AWS Certified Solutions Architect Associate and Cloud technology enthusiast. Currently working for Hexaware technologies. He believes that knowledge increases by sharing not by saving.

1 Comment

Subramanian · November 5, 2017 at 5:06 pm

Keep going machi ☺️
Love your dedication 👌

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.