LCP

Building your first app with Lambda? Try using Serverless-first architecture to lower the cost of application ownership.

Below blog outlines how to deploy an Express js app to AWS lambda using the serverless framework.

What is Serverless?
Serverless is a cloud-native development model that allows developers to build and run applications without having to manage servers.

Why should we use Serverless?

Cost EffectiveNo Server ManagementHigh AvailabilityVirtually Limitless Scalability
Cost Effective - Illustration of a graph depicting cost savings and efficiency.
No Server Management - Visual illustration of a server without management to emphasize its absence.
High Availability - Graphic depicting interconnected servers to show redundancy and continuous service provision.
Virtually Unlimited Scalability - Image depicting expanding servers symbolizing their capacity for infinitum expansion.

Let's create a Sample application and deploy it on AWS Lambda.

Prerequisites to be installed in your machine as below:

  • AWS account with access to IAM and Lambda
  • Node.js 8 or later
  • NPM

Create a simple express application

$ mkdir sample-application undefinedundefined cd sample-application $ npm init -y $ npm install express $ touch index.js

Copy the below code to the index.js file

const express = require('express'); const app = express(); app.use(express.urlencoded({ extended: true })); app.use(express.json()); app.get('/api/get-info', (req, res) =undefined { res.send({ application: 'sample-application', version: '1.0', creator: 'John Doe' }); }); app.post('/api/get-post-data', (req, res) =undefined { res.send({ ...req.body }); }); app.listen(3000, () =undefined console.log(`Listening on: 3000`));

Now let’s check if the above express application is working or not.
In the above express app we have 2 apis one is get and one is post.

  • Get /api/get-info which will show the application information
  • Post /api/get-post-data

$ node index.js

By running the above command it should show you the message "Listening on: 3000". That means the app is working fine.

Our app is now ready to deploy on the Lambda environment. Let's continue with deployment.

Let’s Convert the express app to make it ready to deploy on Lambda environment

We need a serverless-http module which will allow us to ‘wrap’ our express application for serverless use

So let's install this module inside the project. Please follow the below command for that

$ npm i serverless-http

Update our index.js as below:

const serverless = require('serverless-http'); const express = require('express'); const app = express(); app.use(express.urlencoded({ extended: true })); app.use(express.json()); app.get('/api/get-info', (req, res) =undefined { res.send({ application: 'sample-application', version: '1.0', creator: 'John Doe' }); }); app.post('/api/get-post-data', (req, res) =undefined { res.send({ ...req.body }); }); // app.listen(3000, () =undefined console.log(`Listening on: 3000`)); module.exports.handler = serverless(app);

Now let’s set up the Serverless Framework and deploy the application to AWS Lambda

Manually deploying a serverless app using an API Gateway and AWS Lambda can be a boring job, instead of doing that we should let the tool do such things for laborious duty like the Serverless Framework.

Below is the step by step guide on how to use serverless to deploy our created application.

To install serverless follow the below commands.

$ npm install -g serverless

Let’s wait to finish the process.
Check if the serverless is installed successfully or not by following command.

$ serverless --version

Allow serverless to access the AWS account with our created credentials. Replace ACCESS_KEY and SECRET_KEY with your AWS access and secret keys.

$ serverless config credentials --provider aws --key ACCESS_KEY --secret SECRET_KEY

Now, let’s create a Serverless framework config file in the application folder using the below command.

$ touch serverless.yml

Add below code inside the serverless configuration file. Mention your runtime node version the exact version that you want to execute on lambda. Also, you need to give high level version with x i.e (nodejs14.x).

service: sample-application disabledDeprecations: '*' configValidationMode: error provider: name: aws runtime: nodejs14.x stage: dev region: us-east-1 memorySize: 128 functions: app: handler: app/app.handler events: - http: path: / method: ANY cors: true - http: path: /{proxy+} method: ANY cors: true

Now we are all set to deploy our application. Run the following command to deploy our application on AWS Lambda.

$ serverless deploy

If everything works fine you will see the below output like

endpoints: ANY - {API_GATEWAY_URL}/dev/ ANY - {API_GATEWAY_URL}/dev/{proxy+}

We've successfully deployed our Express.js application to an AWS Lambda and to do that we have used the Serverless Framework to automate the deployment process.

Congratulations on building your serverless-first modern application using adoption of serverless services strategy. This will help to increase agility and reduce total cost of ownership (TCO) throughout your application stack.

Have questions on how to maximize serverless for your project? Schedule a meeting now to talk with our expert.

Jay Mehta - Director of Engineering
Jay Mehta

Director of Engineering

Claim Your No-Cost Consultation!

Let's Connect