Skip to content

FazlOmar9/Serverless-Redis-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

:shipit: Redis API Lambda

CDK Lambda Redis TypeScript License

A CDK TypeScript project that creates a Lambda-based Redis API supporting both string and hash operations with GET, SET, and DELETE methods.

Architecture

graph TD
    %% Main architecture flow
    A[Client Request] --> B[Function URL]
    B --> C[AWS Lambda<br>Node.js 20.x<br>256MB RAM]
    C --> D[Redis Client]
    D --> E[Redis Server]
    C --> F[Response]
    F --> A

    %% API Operations as subgraph
    subgraph API_Operations ["API Operations"]
        G[GET /key]
        H[POST /key]
        I[DELETE /key]
    end

    %% Dashed arrows from client to API ops
    A -.-> G
    A -.-> H
    A -.-> I
Loading

Features

String Operations

  • GET /{key} - Retrieve a string value by key
  • POST /{key} - Set a string value for a key (value in request body)
  • DELETE /{key} - Delete a key-value pair

Hash Operations

  • GET /{key}?type=hash&field={field} - Retrieve a specific field from a hash
  • POST /{key}?type=hash - Set a field-value pair in a hash (field and value in request body)
  • DELETE /{key}?type=hash&field={field} - Delete a specific field from a hash

Additional Features

  • Lambda Function URL with CORS enabled
  • Node.js 20.x runtime with 256MB memory
  • Environment variables loaded from .env file
  • Redis connection with username/password authentication support
  • Support for both Redis strings and hash data types

Setup

  1. Configure your Redis connection in .env:
REDIS_HOST=your-redis-host
REDIS_PORT=6379
REDIS_USERNAME=your-username
REDIS_PASSWORD=your-password
REDIS_DB=0
  1. Install dependencies:
npm install
cd lambda && npm install
  1. Deploy the stack:
npx cdk deploy

Usage

After deployment, you'll get a Function URL. Use it to make API calls:

String Operations

GET a string value

curl https://your-function-url.lambda-url.region.on.aws/mykey

SET a string value

curl -X POST https://your-function-url.lambda-url.region.on.aws/mykey \
  -H "Content-Type: application/json" \
  -d '{"value": "my-value"}'

DELETE a string key

curl -X DELETE https://your-function-url.lambda-url.region.on.aws/mykey

Hash Operations

GET a hash field

curl "https://your-function-url.lambda-url.region.on.aws/myhash?type=hash&field=myfield"

SET a hash field

curl -X POST "https://your-function-url.lambda-url.region.on.aws/myhash?type=hash" \
  -H "Content-Type: application/json" \
  -d '{"field": "myfield", "value": "my-hash-value"}'

DELETE a hash field

curl -X DELETE "https://your-function-url.lambda-url.region.on.aws/myhash?type=hash&field=myfield"

API Response Examples

String Operations

Successful GET

{
  "key": "mykey",
  "value": "my-value"
}

Successful SET

{
  "message": "Value set successfully",
  "key": "mykey",
  "value": "my-value"
}

Successful DELETE

{
  "message": "Key deleted successfully",
  "key": "mykey"
}

Hash Operations

Successful Hash GET

{
  "key": "myhash",
  "field": "myfield",
  "value": "my-hash-value"
}

Successful Hash SET

{
  "message": "Hash field set successfully",
  "key": "myhash",
  "field": "myfield",
  "value": "my-hash-value"
}

Successful Hash DELETE

{
  "message": "Hash field deleted successfully",
  "key": "myhash",
  "field": "myfield"
}

Error Responses

Key/Field Not Found

{
  "error": "Key not found",
  "key": "nonexistent"
}
{
  "error": "Field not found in hash",
  "key": "myhash",
  "field": "nonexistent"
}

Missing Parameters

{
  "error": "Field is required for hash GET operation"
}

Development Commands

  • npm run build - Compile TypeScript to JS
  • npm run watch - Watch for changes and compile
  • npm run test - Perform the Jest unit tests
  • npx cdk deploy - Deploy this stack to your default AWS account/region
  • npx cdk diff - Compare deployed stack with current state
  • npx cdk synth - Emits the synthesized CloudFormation template

About

A serverless redis http API built with AWS Lambda on CDK

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published