<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=1063935717132479&amp;ev=PageView&amp;noscript=1 https://www.facebook.com/tr?id=1063935717132479&amp;ev=PageView&amp;noscript=1 "> Bitovi Blog - UX and UI design, JavaScript and Front-end development
Loading

Backend |

Use LocalStack to Build a Development Environment for a Serverless App

Increase development speed & take advantage of mock AWS services when you create local development environments for Serverless apps with LocalStack!

Austin Kurpuis

Austin Kurpuis

Twitter Reddit

Local development environments allow you to speed up both development and the feedback cycle for your Serverless apps. The LocalStack plugin gives you the tools to build local development environments quickly, accessing mock AWS services without credentials.

In this article, we’re going to explore how you can leverage LocalStack to create a local development environment for a Serverless application.

Prerequisites

In order to follow along with the example, you will need the following items installed:

  • Docker
  • Node.js + npm
  • Serverless

Getting Started

Start by generating a new Serverless application using the Serverless CLI and change directory into the newly created project folder.

serverless create --template aws-nodejs --path serverless-app-example
cd serverless-app-example

At this point, you have a basic Serverless application scaffolded with a hello function. Now you need to set up npm in the project.

Run npm init and follow the prompts to fill out the details in the package.json file.

Next, you’ll need to install the serverless-localstack plugin as a dev dependency. You can install the plugin by running npm i -D serverless-localstack.

Finally, configure the serverless-localstack plugin by adding the following lines to the serverless.yml file:

# Enable localstack plugin
plugins:
  - serverless-localstack
# Configure localstack plugin
custom:
  localstack:
    # Only use localstack for the local stage
    stages:
      - local
    # Setup a docker volume for localstack to run code from to improve performance
    lambda:
      mountCode: true
    # Startup localstack automatically
    autostart: true

While you’re in here, you might as well set up an HTTP endpoint for your handler. You can do so by updating the hello function in the serverless.yml file. It should look something like this:

functions:
  hello:
    handler: handler.hello
    events:
      - http: 'GET /hello'

Developing

With all the setup complete, it’s time to get started developing! Run serverless deploy --stage local, which will automatically deploy your application to LocalStack and print out an endpoint URL that looks like something like http://localhost:4566/restapis/ugpfwxng1p/local/_user_request_. You can append the path of your function, /hello in this example, and use curl or your favorite HTTP client to hit the endpoint.

curl http://localhost:4566/restapis/ugpfwxng1p/local/_user_request_/hello

Since you configured the LocalStack plugin to mount your app code into the LocalStack container, changes to your function code will be reflected immediately, without the need to deploy the stack again or rebuild! Just make changes to the handler.js and hit the endpoint to see your changes take effect. Any changes to the serverless.yml will require running the deploy command again.

To make it even faster to start up the development environment, you can add an npm script to the package.json.

"scripts": {
  "start": "serverless deploy --stage local"
  },

Now you can start up the dev environment by running npm start.

Conclusion

Congratulations, you now have a Serverless local development environment built with LocalStack. This will allow you to speed up the feedback cycle of development and build your application in an environment where you have access to mock AWS services, allowing you to write integration tests without the need to provision real AWS services or have AWS credentials configured.