Serverless LocalStack Lambda API Gateway

Serverless LocalStack Lambda API Gateway.

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

Image for post

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

API Gateway

Earlier we have created the function that only invokes using the following command

serverless invoke local -f hello -l

Now we are adding API Gateway using LocalStack. So that the hello function can be accessed using URL to do that we need to update the docker-compose file to support more service such as

  1. API Gateway
  2. Route53
  3. IAM

Update Docker Compose

In the above file, If you see line no. 9 we have added 3 new services to work with API Gateway i.e apigateway, iam, route53

- SERVICES=lambda,s3,cloudformation,sts,apigateway,iam,route53

If you are already running the docker container using the following command to stop.

docker-compose down

Then start again the container using the following command

docker-compose up

then you will see the following logs

Image for post

Update localstack_endpoints.json file

In the above file, We have added 2 more URL’s

  1. “APIGateway”: “http://localhost:4566"
  2. “Route53”: “http://localhost:4566"

Update serverless.yml file

In the above file, we have added the events for HTTP endpoint that support the GET method

To execute the updated code Run the following command

serverless deploy --stage local
Image for post

In the above screenshot, you will see we have deployed the serverless function to LocalStack which returns the endpoints

endpoints:
http://localhost:4566/restapis/uq1bwhx9ls/local/_user_request_

To invoke the hello function use the following URL in the Browser

http://localhost:4566/restapis/uq1bwhx9ls/local/_user_request_/hello
Image for post

Github

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

Written by

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