How bbacker/taglightswitch works
Project type and purpose
• This is a standalone CLI‐based Python tool (“taglightswitch”) that inspects EC2 instance tags, computes a desired on/off schedule, and then optionally powers instances on or off via AWS APIs. It is not a web or full-stack app, but an open-source utility focused on cost control for EC2 instances.
a. CLI entrypoint
• Script: check_lightswitches.py
• Parses command-line arguments (advise vs correct, target time/day, dry-run)
b. Tag-parsing layer (module: controltags.py)
• Reads AWS “lightswitch:*” tags to extract offhours/offdays/offmode
c. Schedule engine (module: lightswitch.py)
• Applies the tag data to the current (or target) time/day to decide desired state
d. Instance abstraction (module: switchableitem.py)
• Wraps an EC2 instance’s metadata, current state, and provides “power_on” / “power_off” methods
e. AWS integration layer
• Boto3 client calls: describe_instances, start_instances, stop_instances
f. External scheduler
• Cron, Jenkins, or Lambda that triggers the CLI on a fixed schedule
Relationships and data flow
• External scheduler triggers the CLI entrypoint
→ CLI parses options & invokes tag-parsing layer
→ Tag layer calls AWS EC2 describe_instances (filtered by lightswitch tag)
→ Controltags creates SwitchableItem objects
→ Schedule engine determines desired state for each item
→ For “advise” mode: builds a report
→ For “correct” mode: calls start_instances/stop_instances via AWS integration
→ AWS EC2 service receives power on/off API calls
Important architectural patterns / principles
• Single codebase, pluggable modules for tag parsing, scheduling logic, and AWS calls
• No separate data store: schedules are stored in EC2 tags
• Dependency inversion: business logic (lightswitch.py) is decoupled from AWS calls (boto3), enabling dry-run or test mocks
• Test layer sits outside in “tests/” to validate each component