Skip to main content

Infrastructure Control Slack Bot

Slack bot for controlling infrastructure operations via slash commands. Uses the Bolt framework with Socket Mode for secure, firewall-friendly communication.

Why This Exists

Infrastructure management shouldn't require:

  • SSH access to jump boxes
  • AWS Console navigation
  • Remembering CLI commands
  • Context switching from Slack

The Slack bot provides ChatOps - manage infrastructure where your team already communicates:

  • /infra status dev - Check what's running
  • /infra stop dev - Stop services for the night
  • /infra whitelist add 1.2.3.4 - Grant yourself access

Commands Reference

Main Command

/infra <command> [environment] [options]

Available Commands

CommandDescriptionExample
status [env]Show infrastructure state/infra status dev
stop [env]Stop ECS/RDS (Tier 1)/infra stop staging
start [env]Start ECS/RDS (Tier 1)/infra start dev
teardown [env] --level=<level>Teardown infrastructure (Tier 2)/infra teardown dev --level=full
spinup [env]Restore from teardown (Tier 2)/infra spinup dev
whitelist add <ip> [options]Add IP to whitelist/infra whitelist add 1.2.3.4 --ttl=7d
whitelist remove <ip>Remove IP/infra whitelist remove 1.2.3.4
whitelist list [env]List whitelisted IPs/infra whitelist list dev
whitelist refresh [env]Sync to security groups/infra whitelist refresh dev

Environments

EnvironmentDescriptionRestrictions
devDevelopment (default)None
stagingStagingNone
prodProductionStop/teardown blocked

Teardown Levels

LevelDescription
servicesStop ECS/RDS only (default, fast)
fullFull infrastructure teardown (requires confirmation)

IP Whitelist Commands

Add IP

/infra whitelist add <ip> [--ttl=<duration>] [--description='<text>']

TTL Options:

  • Minutes: 1m to 1440m (e.g., 5m, 30m, 60m)
  • Days: 1d to 365d (e.g., 7d, 30d, 90d)
  • Default: 30d

Examples:

/infra whitelist add 1.2.3.4
/infra whitelist add 1.2.3.4 --ttl=5m --description='Quick test'
/infra whitelist add 1.2.3.4/32 --ttl=7d --description='Home office'

Automatic Features

  • Auto-sync: Security groups update immediately
  • Auto-expiration: IPs removed when TTL expires
  • Notifications: Channel notified when IPs expire
  • Ports managed: 80 (HTTP), 443 (HTTPS), 7233 (gRPC)

Message Formats

Status Response

Infrastructure Status: DEV
─────────────────────────
ECS Services:
:white_check_mark: api - RUNNING (desired: 2)
:white_check_mark: worker - RUNNING (desired: 1)

RDS Instances:
:white_check_mark: docustack-dev-db - available

Action Response

:white_check_mark: STOP - DEV
─────────────────────────
Operation completed successfully.

Details:
- ECS services stopped: 2
- RDS instances stopped: 1

Confirmation Modal (Full Teardown)

Confirm Teardown
─────────────────────────
:warning: You are about to perform a FULL teardown of the `dev` environment.

This will destroy:
- All ECS services and tasks
- RDS instances (data preserved in snapshots)
- NAT Gateways
- Load Balancers

Critical resources (VPC, S3, Secrets, IAM) will be preserved.

[Type 'dev' to confirm]
[Cancel] [Confirm Teardown]

Slack App Setup

1. Create Slack App

  1. Go to api.slack.com/apps
  2. Click Create New App > From scratch
  3. Name: Infrastructure Control Bot
  4. Select your workspace

2. Configure OAuth & Permissions

Add these Bot Token Scopes:

ScopePurpose
chat:writeSend messages
commandsHandle slash commands
app_mentions:readRespond to mentions
users:readGet user info for whitelist

3. Enable Socket Mode

  1. Navigate to Socket Mode
  2. Enable Socket Mode
  3. Create App-Level Token with connections:write scope
  4. Save the token (starts with xapp-)

4. Create Slash Command

  1. Navigate to Slash Commands
  2. Click Create New Command
  3. Configure:
    • Command: /infra
    • Description: Control infrastructure (status, stop, start, teardown, spinup)
    • Usage Hint: <command> [environment] [options]

5. Enable Interactivity

  1. Navigate to Interactivity & Shortcuts
  2. Enable Interactivity
  3. (No Request URL needed for Socket Mode)

6. Install App

  1. Navigate to Install App
  2. Click Install to Workspace
  3. Save the Bot Token (starts with xoxb-)

Environment Variables

VariableDescriptionRequired
SLACK_BOT_TOKENBot token (xoxb-...)Yes
SLACK_APP_TOKENApp token for Socket Mode (xapp-...)Yes
ORCHESTRATOR_LAMBDA_ARNARN of orchestrator LambdaYes
IP_WHITELIST_LAMBDA_ARNARN of IP whitelist LambdaYes

Deployment

ECS Fargate (Production)

The Slack bot runs as a long-running service on ECS Fargate:

module "slack_bot" {
source = "../../modules/slack-bot"

name = "docustack-infra-bot"
environment = var.environment

vpc_id = module.vpc.vpc_id
private_subnets = module.vpc.private_subnets

slack_bot_token_secret_arn = aws_secretsmanager_secret.slack_bot_token.arn
slack_app_token_secret_arn = aws_secretsmanager_secret.slack_app_token.arn
orchestrator_lambda_arn = module.infra_orchestrator.lambda_arn
}

Docker Image

Multistage build with UV for fast, reproducible dependencies:

# Build stage
FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim AS builder
WORKDIR /app
COPY pyproject.toml .
RUN uv sync --no-dev --frozen

# Runtime stage
FROM python:3.12-slim
WORKDIR /app
COPY --from=builder /app/.venv /app/.venv
COPY app.py .
ENV PATH="/app/.venv/bin:$PATH"
CMD ["python", "app.py"]

Development Workflow

Prerequisites

Install UV for Python package management:

# macOS/Linux
curl -LsSf https://astral.sh/uv/install.sh | sh

# Or via Homebrew
brew install uv

Install Dependencies

cd docustack-mono/services/slack-bot
uv sync

Set Environment Variables

export SLACK_BOT_TOKEN="xoxb-your-bot-token"
export SLACK_APP_TOKEN="xapp-your-app-token"
export ORCHESTRATOR_LAMBDA_ARN="arn:aws:lambda:us-east-1:123456789:function:infra-orchestrator"
export IP_WHITELIST_LAMBDA_ARN="arn:aws:lambda:us-east-1:123456789:function:ip-whitelist-manager"

Run Locally

uv run python app.py

The bot connects via Socket Mode and starts listening for commands.

Testing Commands

  1. Open Slack
  2. Type /infra to see help
  3. Try /infra status dev to check infrastructure

Security Considerations

Socket Mode Benefits

FeatureBenefit
No public endpointWorks behind firewalls
Encrypted WebSocketSecure communication
No SSL managementSlack handles certificates

Token Security

  • Store tokens in AWS Secrets Manager
  • Use IAM roles for Lambda/ECS access
  • Rotate tokens periodically

Production Protection

  • Production teardown/stop blocked at bot level
  • Additional protection in orchestrator Lambda
  • Audit logging for all actions

Troubleshooting

Bot Not Responding

  1. Check Socket Mode is enabled in Slack App settings
  2. Verify SLACK_APP_TOKEN is correct (starts with xapp-)
  3. Check CloudWatch logs for connection errors
  4. Verify ECS service is running

Permission Errors

  1. Verify bot has required OAuth scopes
  2. Re-install app to workspace after scope changes
  3. Check IAM role has Lambda invoke permissions

Command Not Found

  1. Verify slash command is configured in Slack App
  2. Check command name matches exactly (/infra)
  3. Ensure app is installed to the workspace

Files

FileDescription
app.pyMain Slack bot application
pyproject.tomlPython dependencies (UV)
DockerfileMultistage build for ECR

Code Location

docustack-mono/services/slack-bot/
├── app.py # Main application
├── pyproject.toml # Dependencies
├── Dockerfile # Container build
└── README.md