Redis API Lambda
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
Node.js 20.x
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
%% Main architecture flow
A[Client Request] --> B[Function URL]
B --> C[AWS Lambda
Node.js 20.x
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
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
.envfile - Redis connection with username/password authentication support
- Support for both Redis strings and hash data types
Setup
- 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
REDIS_PORT=6379
REDIS_USERNAME=your-username
REDIS_PASSWORD=your-password
REDIS_DB=0
- Install dependencies:
npm install
cd lambda && npm install
cd lambda && npm install
- 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"}'
-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"}'
-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"
}
"key": "mykey",
"value": "my-value"
}
Successful SET
{
"message": "Value set successfully",
"key": "mykey",
"value": "my-value"
}
"message": "Value set successfully",
"key": "mykey",
"value": "my-value"
}
Successful DELETE
{
"message": "Key deleted successfully",
"key": "mykey"
}
"message": "Key deleted successfully",
"key": "mykey"
}
Hash Operations
Successful Hash GET
{
"key": "myhash",
"field": "myfield",
"value": "my-hash-value"
}
"key": "myhash",
"field": "myfield",
"value": "my-hash-value"
}
Successful Hash SET
{
"message": "Hash field set successfully",
"key": "myhash",
"field": "myfield",
"value": "my-hash-value"
}
"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"
}
"message": "Hash field deleted successfully",
"key": "myhash",
"field": "myfield"
}
Error Responses
Key/Field Not Found
{
"error": "Key not found",
"key": "nonexistent"
}
"error": "Key not found",
"key": "nonexistent"
}
{
"error": "Field not found in hash",
"key": "myhash",
"field": "nonexistent"
}
"error": "Field not found in hash",
"key": "myhash",
"field": "nonexistent"
}
Missing Parameters
{
"error": "Field is required for hash GET operation"
}
"error": "Field is required for hash GET operation"
}
Development Commands
npm run build- Compile TypeScript to JSnpm run watch- Watch for changes and compilenpm run test- Perform the Jest unit testsnpx cdk deploy- Deploy this stack to your default AWS account/regionnpx cdk diff- Compare deployed stack with current statenpx cdk synth- Emits the synthesized CloudFormation template