Skip to main content

Stateful AWS Lambda with Redis REST

· 2 min read
Enes Akar

AWS Lambda pioneered serverless space. Many developers think that serverless is the future of development. It gives you the true pay-per-use model, relieves you from the maintenance and scaling of the backend infrastructure. But it also comes with challenges. One of those is its statelessness. You need to keep the state in an external data store. Unfortunately most of the popular data stores are connection based. But as we explained in this post, managing connections can become painful in serverless. That’s why, we have developed a high performance REST API on top of Upstash Redis. In this blog post, I will implement a very basic stateful api (page counter) on AWS Lambda and Upstash Redis using the REST API.

See the live demo:

See the code:

Project Setup#

We will use the serverless framework to deploy our application.

git:(master) ✗ serverless
Serverless: No project detected. Do you want to create a new one? Yes
Serverless: What do you want to make? AWS Node.js
Serverless: What do you want to call this project? aws-lambda-redis-rest
Project successfully created in 'aws-lambda-redis-rest' folder.

The Code#

Install fetch via npm install node-fetch. Then create a database from the Upstash console. Click the REST API button and copy the url and token. Update handler.js as below:

const fetch = require("node-fetch");
module.exports.hello = async (event) => {   const url = "";   let data = await fetch(url)   let result = (await data.text());   return {'statusCode': 200,  'body': result}};

Add API endpoint to the serverless.yml as below:

service: aws-lambda-redis-restframeworkVersion: '2'
provider: name: aws runtime: nodejs12.x lambdaHashingVersion: 20201221
functions: hello:   handler: handler.hello   events:     - httpApi:         path: /count         method: get

Run and Deploy#

You can the functions locally:

serverless invoke local -f hello

You can deploy with:

serverless deploy

The command will output the url which you can check in your browser.


  • For the best performance, your database and AWS Lambda function should be in the same region.
  • Thanks to the REST API, we do not need to manage database connections and we do not need any Redis client.
  • Do not share your API token publicly. If leaked mistakenly, you can revoke it by resetting your database’s password.
  • If you want to run multiple commands in a single function, use pipeline API for the best performance.