A Slack bot that allows you to control EC2 instances directly from Slack using slash commands. The bot supports starting, stopping, and checking the status of EC2 instances using either instance names or instance IDs.
- π Start/Stop EC2 instances via Slack commands
- π Check instance status with detailed information
- π·οΈ Support for instance names (using Name tags) and instance IDs
- π― Interactive buttons and menus for better user experience
- π List all instances with visual state indicators
- π Smart suggestions based on instance states
- π Regional support (currently configured for ap-southeast-1)
- π IAM-based security with minimal required permissions
Slack Slash Command (/ec2)
β
API Gateway
β
Lambda Function
β
EC2 API (ap-southeast-1)
- AWS CLI configured with appropriate credentials
- AWS account with EC2 instances in ap-southeast-1 region
- Slack workspace with admin permissions to create apps
- Python 3.13+ (for local development/testing)
cd ~/Projects
git clone <your-repo> ec2-controller-bot
cd ec2-controller-bot
# Deploy the complete stack
./deploy.sh
- Go to https://api.slack.com/apps
- Create new app β "From scratch"
- Add slash command with the API Gateway URL from deployment output
- Configure Interactivity: Go to "Interactivity & Shortcuts" β Enable β Use same API Gateway URL
- Install app to your workspace
Important: Interactive features require both slash command AND interactivity URL to be configured.
/ec2 # Shows interactive menu with buttons
/ec2 list # List all instances
/ec2 start web-server # Start instance
/ec2 stop web-server # Stop instance
/ec2 status web-server # Get status
Interactive Features: Click buttons and menus for one-click actions!
Command | Description | Example |
---|---|---|
/ec2 |
Show interactive menu with buttons | /ec2 |
/ec2 list |
List all instances | /ec2 list |
/ec2 start <name> |
Start instance | /ec2 start web-server |
/ec2 stop <name> |
Stop instance | /ec2 stop web-server |
/ec2 status <name> |
Get status | /ec2 status web-server |
All invalid commands now return clear error messages:
Command Type | Example | Response |
---|---|---|
Invalid actions | /ec2 help , /ec2 delete |
β Invalid action 'help'. Supported actions are: start, stop, status, list |
Invalid with instance | /ec2 delete myserver |
β Invalid action 'delete'. Supported actions are: start, stop, status |
Wrong format | /ec2 start my server name |
β Invalid command format. Use: /ec2 <action> <instance> or /ec2 for interactive menu |
The bot provides interactive buttons and menus for better user experience:
- Interactive Menu: Type
/ec2
to get clickable buttons for all actions - Quick Action Buttons: One-click start/stop for common instances
- Instance Lists: Visual indicators with emojis (π’ Running, π΄ Stopped, etc.)
- Overflow Menus: Three-dot menus with available actions per instance
- Smart Suggestions: Only show relevant actions based on instance state
Note: Interactive features require the updated Lambda function. Run ./deploy.sh
to deploy the latest version with interactive buttons.
ec2-controller-bot/
βββ README.md # This file
βββ deploy.sh # Deployment script
βββ src/
β βββ lambda_function.py # Main Lambda function
βββ config/
β βββ iam-trust-policy.json # IAM trust policy for Lambda
β βββ iam-permissions.json # IAM permissions policy
βββ docs/
β βββ DEPLOYMENT.md # Detailed deployment guide
β βββ SLACK_SETUP.md # Slack app configuration guide
β βββ TROUBLESHOOTING.md # Common issues and solutions
βββ examples/
βββ slack-responses.json # Example Slack response formats
The bot follows AWS security best practices:
- Minimal IAM permissions: Only EC2 start/stop/describe permissions
- Regional restrictions: Limited to ap-southeast-1 region
- Instance validation: Validates instance existence before actions
- Error handling: Graceful error handling with user-friendly messages
- Audit logging: All actions are logged to CloudWatch
- IAM Role:
SlackEC2ControlRole
with minimal EC2 permissions - Lambda Function:
slack-ec2-control
in ap-southeast-1 - API Gateway: REST API with
/slack
endpoint - CloudWatch Logs: Automatic logging for debugging
The Lambda function uses these configurations:
- Region:
ap-southeast-1
(hardcoded in function) - Timeout: 30 seconds
- Memory: 128 MB
- Runtime: Python 3.13
To deploy in a different region, update:
src/lambda_function.py
- Change theregion_name
parameterdeploy.sh
- Update the AWS CLI region parameter
To restrict which instances can be controlled, modify the resolve_instance_identifier
function to add tag-based filtering.
The bot uses the Name
tag for instance identification. To use different tags, modify the get_instance_name
function.
Monitor the Lambda function logs:
aws logs tail /aws/lambda/slack-ec2-control --region ap-southeast-1 --follow
- "Instance not found": Check if instance exists and has Name tag
- "Permission denied": Verify IAM role has correct permissions
- "Timeout": Check if Lambda function has network access to EC2 API
See docs/TROUBLESHOOTING.md
for detailed solutions.
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
MIT License - see LICENSE file for details.
For issues and questions:
- Check the troubleshooting guide
- Review CloudWatch logs
- Open an issue in the repository
See CHANGELOG.md for detailed version history and release notes.
Note: This bot is designed for ap-southeast-1 region. Modify the configuration to use in other regions.