Upstash Documentation

AWS Lambda Rate Limiting with Serverless Redis

1 min read

In this tutorial, we will deploy a AWS Lambda function ratelimited based on the client's IP address using @upstash/ratelimit and Serverless Framework.

Prerequisites#

  1. Install the Serverless Framework with npm i serverless -g

Project Setup#

Create a Serverless Framework application with the following options:

Create package.json with @upstash/ratelimit as a dependency:

package.json

Install the dependencies:

Database Setup#

Create a Redis database using Upstash Console or Upstash CLI and copy the UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN into your .env file.

.env

Ratelimited Function Setup#

Update handler.js:

handler.js

Update serverless.yml to pass the environment variables:

serverless.yml

Developing#

Run the following command to start your dev session.

Deployment#

Run the following command to deploy your service.

Visit the output url.