Serverless LocalStack Lambda API Gateway
Tags: Localstack, AWS Lambda, Localstack Apigateway, Aws Api Gateway, Onexlab

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.

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.

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

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

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.

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

Lambda S3 Cloudformation STS

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

Run the following command to run the docker container

docker-compose up

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

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

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

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

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

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

API Gateway Route53 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

Update localstack_endpoints.json file

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

“APIGateway”: “http://localhost:4566" “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

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

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

Github

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