Skip to content

geoicons/serverless-proxy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

9 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

SERVERLESS PROXY

A serverless HTTP proxy service built with AWS CDK that provides a scalable, secure middleware layer for API routing and request processing. This application creates a Lambda-backed API Gateway that can route requests to different handler modules dynamically. If you find this stack useful you might like to try our fully authenticated, globally distributed API Authentication proxy over at https://requestrocket.com. Early access registrations are available by https://app.requestrocket.com/

πŸ—οΈ Architecture

This application consists of:

  • AWS Lambda Function: Node.js 18.x runtime with Express.js handling HTTP requests
  • API Gateway: RESTful API with proxy integration and CORS support
  • AWS CDK: Infrastructure as Code for deployment and management
  • TypeScript: Strongly typed development with build process

Key Features

  • πŸ”„ Dynamic Module Loading: Routes requests to different handlers based on endpoint paths
  • 🌐 CORS Enabled: Cross-origin request support with configurable policies
  • πŸ“ Request Logging: Comprehensive request/response logging for debugging
  • πŸ”’ Header Sanitization: Automatic filtering of sensitive headers
  • πŸ“Š Multi-format Support: Handles JSON, binary data, and various media types
  • πŸš€ Serverless: Auto-scaling with pay-per-use pricing model

πŸš€ Getting Started

Prerequisites

  • Node.js 18.x or higher
  • Yarn package manager
  • AWS CLI configured with appropriate permissions
  • AWS CDK CLI installed globally

Project Structure

serverless-proxy/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ app.ts                    # CDK app entry point
β”‚   β”œβ”€β”€ cdk_stack.ts             # Main CDK stack definition
β”‚   β”œβ”€β”€ lambda_api/              # Lambda function source
β”‚   β”‚   β”œβ”€β”€ application.ts       # Express.js application setup
β”‚   β”‚   β”œβ”€β”€ service.ts          # Lambda handler entry point
β”‚   β”‚   β”œβ”€β”€ application/        # Route handlers
β”‚   β”‚   β”‚   └── proxy.ts        # Proxy endpoint handlers
β”‚   β”‚   └── utilities/          # Shared interfaces and utilities
β”‚   └── stack/                  # CDK infrastructure modules
└── .github/workflows/          # CI/CD pipeline definitions

Installation

  1. Install all dependencies:

    yarn installall
  2. Build the project:

    yarn buildall

πŸ’» Development

Local Development

The application uses a modular architecture where each endpoint corresponds to a module in the application/ directory.

Adding New Endpoints

  1. Create a new TypeScript file in src/lambda_api/application/
  2. Export get, post, and/or del functions as needed
  3. Each function should return an expressResponseType object

Example endpoint handler:

import { expressResponseType } from "../utilities/interfaces";

export async function get(request: any) {
    const response: expressResponseType = {
        status: 200,
        message: "Success",
        data: { result: "Your data here" },
    };
    return response;
}

export async function post(request: any) {
    // Handle POST requests
    // Access request.body, request.params, request.query
}

Request Routing

Requests to /proxy/:path are automatically routed to the corresponding module:

  • /proxy/myendpoint β†’ application/myendpoint.ts
  • HTTP methods are mapped to exported functions (get, post, del)

Testing

# Run tests for the main project
yarn test

# Run tests for the Lambda API
yarn --cwd src/lambda_api test

Environment Variables

The Lambda function has access to these environment variables:

  • RESOURCEREGION: AWS region where resources are deployed
  • STAGE: Deployment stage (dev/qas/prod)
  • MANAGEMENTAPIKEY: Management API key for secure operations

πŸš€ Deployment

Automated Deployment (Recommended)

The project uses GitHub Actions for CI/CD:

  1. Development: Push to any branch

    git push origin feature/my-feature
  2. Quality Assurance: Push to qas branch

    git push origin qas
  3. Production: Push to main branch

    git push origin main

Manual Deployment

For local deployment or testing:

# Deploy to development environment
cdk deploy --profile your-aws-profile

# Deploy with specific parameters
cdk deploy -c stage=dev -c region=us-east-1

GitHub Actions Workflows

  • PR Checks: Automated build and CDK synth on pull requests to main
  • Auto Deploy: Deployment triggered on merge to main/qas/prod branches
  • Required Secrets:
    • AWS_ACCESS_KEY_ID
    • AWS_SECRET_ACCESS_KEY

πŸ”§ Configuration

CDK Context

Configure deployment settings in cdk.json or via command line:

  • stage: Deployment environment (dev/qas/prod)
  • region: AWS region for deployment
  • managementApiKey: API key for management operations

CORS Configuration

CORS is configured to allow all origins by default. Modify in src/lambda_api/application.ts:

const corsOptions = {
    origin: function (origin: string | undefined, callback: Function) {
        // Add your origin validation logic here
        callback(null, true);
    },
    credentials: true,
    methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
};

πŸ“Š Monitoring and Logging

Request Logging

All requests are automatically logged with:

  • Request method and URL
  • Headers, body, query parameters
  • Response status and timing

AWS CloudWatch

Monitor your application through:

  • Lambda function logs
  • API Gateway access logs
  • CloudWatch metrics and alarms

πŸ” Security

  • Headers are automatically sanitized to remove sensitive AWS-specific headers
  • CORS policies can be customized for production environments
  • API Gateway provides built-in DDoS protection and throttling

πŸ› οΈ Available Scripts

Command Description
yarn installall Install dependencies for all packages
yarn buildall Build all TypeScript projects
yarn test Run tests for the main project
yarn build Build only the CDK project
yarn build_lambda_api Build only the Lambda API

πŸ“š API Reference

Base URL

After deployment, your API will be available at:

https://{api-id}.execute-api.{region}.amazonaws.com/{stage}/

Endpoints

  • GET /proxy/{endpoint} - Route to corresponding handler module
  • POST /proxy/{endpoint} - Route to corresponding handler module
  • DELETE /proxy/{endpoint} - Route to corresponding handler module

Response Format

All responses follow the expressResponseType interface:

{
    status: number;
    message?: string;
    data?: any;
    headers?: any;
    type?: string;
    cache?: string;
}

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.

About RequestRocket

RequestRocket is a universal API proxy platform that simplifies authentication and makes API integration faster, easier, and more secure.

Built for developers, data engineers, and business intelligence teams, RequestRocket transforms the way systems connect by abstracting away the complexity of API authentication protocols β€” from API Keys and OAuth2 to JWT and custom tokens.

With a global serverless infrastructure, RequestRocket offers low-latency, high-availability API endpoints that handle retries, caching, telemetry, and even AI-driven just-in-time prefetching. It’s secure, scalable, and ready to plug into your stack β€” no matter how large or complex.

Whether you're building a new SaaS product, integrating with dozens of enterprise APIs, or trying to securely connect business tools like Power BI to third-party data, RequestRocket is your authentication translation layer. Spend less time debugging tokens and more time building the things that matter.

For Developers: Start Building Without the Headaches Join developers around the world using RequestRocket to simplify their API stack, reduce integration time, and ship faster. Create your free account and start building today.

πŸ‘‰ Sign up now at https://app.requestrocket.com – It’s free to get started.

About

A simple serverless proxy

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published