Serverless LocalStack Lambda

Serverless LocalStack Lambda.

In this article, we will help you to cover the Serverless framework with LocalStack to create Lambda function locally.

Image for post
Serverless LocalStack Lambda

We hope you are familiar with the Serverless framework and LocalStack If you are not please read our previous article.

Video

What is LocalStack?

LocalStack provides an easy-to-use test/mocking framework for developing Cloud applications. This means you can test AWS cloud resources locally on your machine.

Note: LocalStack supporting only AWS cloud stack.

LocalStack spins up the following core Cloud APIs on your local machine.

  • ACM, API Gateway, CloudFormation, CloudWatch
  • CloudWatch Logs, DynamoDB, DynamoDB Streams
  • EC2, Elasticsearch Service, EventBridge (CloudWatch Events)
  • Firehose, IAM, Kinesis, KMS, Lambda, Redshift
  • Route53, S3, SecretsManager, SES, SNS
  • SQS, SSM, StepFunctions, STS

Benefits of Using LocalStack

  • Reduce Cost
  • Test AWS Cloud Resource Locally
  • Learn AWS Cloud Resource Locally
  • Debug Locally

Let’s Start

We hope you have installed Docker on your system else you can check the following article to install Docker.

Make Sure You have installed Node JS or NVM on your system or follow the following article.

  1. Install a serverless framework using the following command
npm install -g serverless

2. Install a serverless-localstack plugin using the following command

npm install --save-dev serverless-localstack

3. Create a new Serverless Service/Project

serverless create — template aws-nodejs — path localstack-lambda
Image for post

Once the project created successfully. You will see the following files as shown in the screenshot.

Image for post
  1. handler.js

2. serverless.yml

4. Install AWS Client

Make sure you have installed AWS client on your system if not you can follow the following link to install

5. Docker Compose file

Create a new file docker-compose.yml file under the root directory of the project as shown below.

Image for post

In the above file, we are creating a LocalStack docker image with the following services

  1. Lambda
  2. S3
  3. Cloudformation
  4. STS

All services will be accessed using http://localhost:4566 URL

Run the following command to run the docker container

docker-compose up
Image for post

Once the container started successfully you will see the logs of the LocalStack services as shown below

Image for post

To check all the services type the following URL in the browser

http://localhost:4566/

You will see the defined services running fine as shown in the below screenshot

Image for post

6. Now update the serverless.yml file to add the LocalStack plugin as shown below

In the above file, we have added the local/dev stage as well as endpointFile localstack_endpoints.json as shown below

Let’s deploy the serverless stack locally

To deploy the hello function in the LocalStack run the following command

serverless deploy --stage local

To check the service deployed successfully. Run the following command

serverless info --stage local

Then you will see the following output as shown below

Image for post

To invoke the function locally. Run the following command

serverless invoke local -f hello -l

Then you will see the following output as shown below which means the function run successfully locally using LocalStack

Image for post

Github

Thank you! 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀

We teach & develop for startups

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store