A CDK TypeScript project that creates a Lambda-based Redis API supporting both string and hash operations with GET, SET, and DELETE methods.
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
- 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
- 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
- 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
- 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
- Install dependencies:
npm install
cd lambda && npm install
- Deploy the stack:
npx cdk deploy
After deployment, you'll get a Function URL. Use it to make API calls:
curl https://your-function-url.lambda-url.region.on.aws/mykey
curl -X POST https://your-function-url.lambda-url.region.on.aws/mykey \
-H "Content-Type: application/json" \
-d '{"value": "my-value"}'
curl -X DELETE https://your-function-url.lambda-url.region.on.aws/mykey
curl "https://your-function-url.lambda-url.region.on.aws/myhash?type=hash&field=myfield"
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"}'
curl -X DELETE "https://your-function-url.lambda-url.region.on.aws/myhash?type=hash&field=myfield"
{
"key": "mykey",
"value": "my-value"
}
{
"message": "Value set successfully",
"key": "mykey",
"value": "my-value"
}
{
"message": "Key deleted successfully",
"key": "mykey"
}
{
"key": "myhash",
"field": "myfield",
"value": "my-hash-value"
}
{
"message": "Hash field set successfully",
"key": "myhash",
"field": "myfield",
"value": "my-hash-value"
}
{
"message": "Hash field deleted successfully",
"key": "myhash",
"field": "myfield"
}
{
"error": "Key not found",
"key": "nonexistent"
}
{
"error": "Field not found in hash",
"key": "myhash",
"field": "nonexistent"
}
{
"error": "Field is required for hash GET operation"
}
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