How to run AWS Serverless in local?

AWS Lambda is a serverless application.

That means you don’t have control over where to deploy your app on AWS.

AWS will take care of that.

But where and how do you deploy the app locally for your testing.

You can do this in two ways:

  1. Using serverless invoke local command
  2. Using serverless-offline plugin

Using Serverless invoke local command:

Using this command you can directly invoke your serverless function.

For example if you have a function named “hello” in your serverless app, you can invoke it using the below command:

serverless invoke local --function hello

hello is the function name configured under “functions” variables in serverless.yml file.

Here is the content of the handler class(node js app):

handler.js:

'use strict';

module.exports.hello = async (event) => {
  return {
    statusCode: 200,
    body: JSON.stringify(
      {
        message: 'Go Serverless v1.0! Your function executed successfully!',
        input: event,
      },
      null,
      2
    ),
  };


};

And here is the output:

You can also pass data to your function using the below command:

serverless invoke local --function functionName --data '{ "key": "value" }'

You can also pass data from a file using the below command:

serverless invoke local --function functionName --path path/to/data.json

You can monitor the logs locally using the below command:

serverless invoke local --function functionName --log

If you have exposed your function as an API then you need to use the next strategy:

Advertisements

Using Serverless offline plugin

To install the serverless offline plugin, navigate to the root directory of your serverless application and run the following command:

npm install serverless-offline --save-dev

This will install the serverless-offline plugin as a development dependency in your project.

Also make sure you have the latest version of node js installed in your machine. Or else you may face dependency compatibility issues like “Cannot import node:url” etc.

Next, you will need to update the serverless.yml file to enable the plugin. To do this, add the following block to the plugins section of the file:

plugins:
  - serverless-offline

With the plugin installed and enabled, you can start the local development server using the following command:

serverless offline start

Or merely the command :

serverless offline

This will start the serverless application in “offline” mode, which means that it will simulate the AWS Lambda and API Gateway services on your local machine. You can then access your functions and endpoints using the URLs printed to the console.

For example, if you have an HTTP endpoint defined like this:

functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: /hello
          method: get

And the sample code defined like this:

handler.js

"use strict";

module.exports.hello = async (event) => {
  return {
    statusCode: 200,
    body: JSON.stringify({
      message: "Hello Serverless World!",
    }),
  };
};

You can access the endpoint at http://localhost:3000/hello (assuming the default port of 3000).

Here is the output:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s