Skip to content

Lambda Functions Package

Overview

The packages/lambda_functions/ directory contains AWS Lambda functions that provide serverless functionality for the Delta platform. These functions handle specific tasks like message processing and API authorization.

Lambda Functions

AI Hub SQS Processor

Package: @delta/aihub-sqs-processor

Purpose

  • Processes messages from AWS SQS queues
  • Triggers AI Hub processing tasks
  • Manages ECS task execution for AI workloads

Technology Stack

  • Runtime: Node.js with TypeScript
  • Build Tool: esbuild for optimized bundling
  • AWS Services: SQS, ECS
  • Logging: Integration with @delta/common

Key Features

  • SQS message processing
  • ECS task management
  • Error handling and retry logic
  • Optimized bundle size for Lambda

Dependencies

  • AWS SDK for SQS and ECS operations
  • @delta/common for shared utilities
  • No external HTTP client (uses AWS SDK for service communication)

Build Process

# Build optimized bundle
yarn run build

# Start local development
yarn run dev

The build process uses esbuild to create a minified, bundled JavaScript file optimized for Lambda execution.

Configuration

  • Entry Point: src/index.ts
  • Output: dist/index.js (bundled and minified)
  • External Dependencies: AWS SDK modules are marked as external
  • Source Maps: Enabled for debugging

API Gateway Authorizer

Package: @delta/api-gateway-authorizer

Purpose

  • Provides custom authorization for API Gateway endpoints
  • Validates authentication tokens
  • Manages access control policies

Technology Stack

  • Runtime: Node.js with TypeScript
  • Build Tool: esbuild for optimized bundling
  • AWS Services: SSM (Systems Manager)
  • Type Safety: AWS Lambda types

Key Features

  • Token validation and verification
  • Policy generation for API Gateway
  • Integration with AWS SSM for configuration
  • Lightweight and fast execution

Dependencies

  • AWS SDK for SSM operations
  • AWS Lambda types for TypeScript support

Build Process

# Build optimized bundle
yarn run build

Similar to the SQS processor, this uses esbuild for optimal Lambda performance.

Configuration

  • Entry Point: src/index.ts
  • Output: dist/index.js (bundled and minified)
  • Source Maps: Enabled for debugging

Development

Local Development

AI Hub SQS Processor

cd packages/lambda_functions/aiHubSqsProcessor

# Install dependencies
yarn install

# Start local development with debugging
yarn run dev

The development script uses a custom local development setup that simulates SQS message processing.

API Gateway Authorizer

cd packages/lambda_functions/apiGatewayAuthorizer

# Install dependencies
yarn install

# Build the function
yarn run build

Testing

Both Lambda functions can be tested locally using:

  • AWS SAM CLI for local Lambda simulation
  • LocalStack for AWS service mocking
  • Unit tests with mocked AWS services

Environment Variables

AI Hub SQS Processor

  • AWS region and credentials
  • SQS queue URLs
  • ECS cluster and task definitions
  • Logging configuration

API Gateway Authorizer

  • AWS region and credentials
  • SSM parameter paths
  • Authentication service endpoints

Deployment

AWS Lambda Deployment

Both functions are designed for deployment to AWS Lambda:

  1. Build: Create optimized bundles using esbuild
  2. Package: Zip the dist/ directory with dependencies
  3. Deploy: Upload to AWS Lambda using AWS CLI, CDK, or Terraform

Infrastructure as Code

The Lambda functions are typically deployed using:

  • AWS CDK for TypeScript-based infrastructure
  • Terraform for declarative infrastructure
  • AWS SAM for serverless application deployment

Monitoring and Logging

CloudWatch Integration

  • Automatic log streaming to CloudWatch Logs
  • Custom metrics for function performance
  • Error tracking and alerting

Observability

  • Distributed tracing with AWS X-Ray
  • Performance monitoring
  • Cost optimization tracking

Best Practices

Performance Optimization

  • Minimal bundle sizes using esbuild
  • External AWS SDK dependencies
  • Connection reuse and pooling
  • Cold start optimization

Security

  • Least privilege IAM roles
  • Secure environment variable handling
  • Input validation and sanitization
  • Error message sanitization

Reliability

  • Proper error handling and retries
  • Dead letter queue configuration
  • Timeout and memory optimization
  • Graceful degradation

Architecture Integration

AI Hub SQS Processor Flow

  1. SQS message triggers Lambda function
  2. Function processes message content
  3. Initiates ECS task for AI processing
  4. Monitors task completion
  5. Handles success/failure scenarios

API Gateway Authorizer Flow

  1. API Gateway receives request
  2. Authorizer Lambda validates token
  3. Returns authorization policy
  4. API Gateway allows/denies request
  5. Logs authorization decisions

Troubleshooting

Common Issues

  • Cold Starts: Optimize bundle size and initialization
  • Timeouts: Adjust timeout settings and optimize code
  • Memory Issues: Monitor memory usage and adjust allocation
  • Permission Errors: Verify IAM roles and policies

Debugging

  • Enable detailed logging
  • Use AWS X-Ray for tracing
  • Monitor CloudWatch metrics
  • Test locally with SAM CLI