How swiggy/ddb-ondemand-protector works
Project Type and Purpose
• This project is an AWS-based pipeline tool designed to serve as a cost guardrail by switching DynamoDB tables from on‑demand to provisioned capacity once a cost threshold is reached.
• Its core functionality is to monitor RCU/WCU usage (and cost) and then automatically trigger a mode switch via an AWS Step Function, which orchestrates Lambda functions and CloudFormation/SAM configuration.
• The design is intended to address cost management for DynamoDB tables by limiting excessive consumption through automated resource mode switching.
Analyzing the File Structure
• Top-level directories such as:
• cmd: Contains the main executable (main.go) serving as the entry point for the DdbSwitch command line tool.
• internal: Contains the business logic (ddb_switch.go and its tests) used by the tool.
• pkg: Houses additional reusable packages for handling billing modes, scaling events, and other DynamoDB utilities.
• config: Provides configuration management (e.g., config.go).
• sam: Contains the AWS SAM template (cu-limiter-ddb.yaml) which defines the AWS resources.
• test: Contains mocks for testing external components.
• The presence of a build/templating script (create_template.py) indicates that part of the system is responsible for generating deployment templates dynamically.
Insights from the README
• The README explains the motivation, detailing the cost implications of on‑demand capacity and the need for table-level throttling.
• The "Architecture" section describes the high-level pipeline using AWS Step Functions that orchestrate the switch between on‑demand and provisioned modes for DynamoDB.
• The deployment is managed via AWS SAM CLI with CloudFormation-defined resources:
• DdbSwitch (Lambda Function): Responsible for switching the table mode.
• DdbStep (Step Function): Orchestrates the switch workflow.
• Ddbalarm (CloudWatch Alarm): Monitors table capacity thresholds.
• DdbSwitchRule (EventBridge Rule): Triggers the step function once thresholds are met.
a. Main Components:
• Front-end/Command-line Component:
• cmd/ddb-switch: Entry point initiating the process.
• Business Logic & Utility Libraries:
• internal/ddb-switch: Core logic for processing switching requests.
• pkg/ddb-switch: Utility functions for billing mode, scaling events, and DynamoDB interaction.
• config: Application configuration.
• Deployment & Automation:
• create_template.py: Script that generates the SAM template.
• sam template (cu-limiter-ddb.yaml): Defines AWS resources required for deploying the pipeline.
• AWS Cloud Resources (as per SAM template):
• Lambda (DdbSwitch)
• Step Function (DdbStep)
• CloudWatch Alarm (Ddbalarm)
• EventBridge Rule (DdbSwitchRule)
• Associated SNS Topics and IAM Roles (as specified in the README parameters)
b. Relationships and Interactions:
• The command-line tool (cmd/ddb-switch) triggers the execution of the internal logic.
• The internal and pkg directories handle business logic and orchestrate interactions with DynamoDB.
• The generated SAM template ties these together with AWS resources:
• CloudWatch Alarm continuously measures RCU/WCU usage.
• When thresholds are breached, the Alarm triggers an EventBridge Rule.
• This rule invokes the Step Function (DdbStep), which executes the DdbSwitch Lambda Function.
• The Lambda function carries out the mode switch on the DynamoDB table and schedules a reversal after 24 hours.
• External services like SNS topics are integrated to send notifications and alerts.
c. Important Architectural Patterns or Design Principles:
• Separation of Concerns: Distinct directories for command entry point, internal business logic, and common functions.
• Infrastructure as Code: Use of AWS SAM and CloudFormation templates for deployment.
• Event-Driven Architecture: AWS CloudWatch, EventBridge, and Step Functions working together to react to predefined capacity thresholds.
• Testability: Presence of a test directory and mocks for unit/integration testing.
d. Relevant Technologies and Frameworks:
• Go for the core application logic.
• AWS Step Functions, Lambda, CloudWatch Alarms, EventBridge for the orchestration of capacity switching.
• AWS SAM for packaging and deploying this serverless stack.
• Python (create_template.py) for dynamic SAM template generation.
Elements
• Each major component (both the internal modules and AWS resources) should have a clear label:
• "cmd/ddb-switch (CLI Entry Point)"
• "internal/ddb-switch (Business Logic)"
• "pkg/ddb-switch (Utilities & Observability)"
• "SAM Template (cu-limiter-ddb.yaml)"
• "AWS Lambda (DdbSwitch)"
• "AWS Step Function (DdbStep)"
• "AWS CloudWatch Alarm (Ddbalarm)"
• "AWS EventBridge Rule (DdbSwitchRule)"
• "SNS Topics & IAM Roles"
• How the CLI triggers the internal logic.
• Data flow from DynamoDB capacity measurements to CloudWatch Alarm.
• Alarm triggering the EventBridge Rule, which then initiates the Step Function.
• Step Function invoking the Lambda function for mode switching.
• Another for cloud resources (Lambda, Step Functions, CloudWatch, etc.).