Skip to content

Repository files navigation

Serverless Twitter Bot

An AI-powered Twitter bot built with AWS CDK that autonomously generates developer-focused tweets with code snippet images every Monday at 3:30 PM IST.

Features

  • Autonomous Scheduling: Runs serverlessly via AWS EventBridge (Cron)
  • AI Content Generation: Uses GPT-4o-mini for tweet text + relevant code snippets
  • Dynamic Media: Renders code to PNG via SourceCodeShots API
  • Secure Secret Management: Runtime SSM SecureString decryption (no env var exposure)
  • Zero-Cost Infrastructure: Fully within AWS Free Tier limits
  • Type-Safe: Built with TypeScript, esbuild bundling, and strict linting

️ Architecture

Architecture Diagram

Component Flow

  1. EventBridge triggers Lambda every Monday @ 10:00 UTC (3:30 PM IST)
  2. Lambda fetches encrypted secrets from SSM Parameter Store at runtime
  3. OpenAI generates tweet text (<280 chars) + supporting code snippet
  4. SourceCodeShots renders code snippet to PNG image
  5. Twitter API v2 posts tweet with attached media

️ Tech Stack

Category Technology
Language TypeScript (Node.js 20)
Infra as Code AWS CDK v2
AI OpenAI GPT-4o-mini
Media SourceCodeShots API
Social Twitter API v2
Secrets AWS SSM SecureString
Package Mgr pnpm
Bundler esbuild

Local Development

Prerequisites

  • Node.js 20+
  • pnpm
  • AWS CLI configured
  • .env file with required keys (see below)

Setup

pnpm install
pnpm build

Run Locally

# Test full tweet generation flow
pnpm post-tweet

# Watch mode for development
pnpm watch

Environment Variables

Create a .env file:

OPENAI_API_KEY=sk-proj-...
CONSUMER_KEY=...
CONSUMER_KEY_SECRET=...
ACCESS_KEY=...
ACCESS_KEY_SECRET=...

️ AWS Deployment

Bootstrap (One-time)

npx cdk bootstrap aws://ACCOUNT_ID/ap-south-1

Deploy

pnpm cdk:deploy

Destroy

pnpm cdk:destroy

Security & Cost

  • Secrets: Never stored in CFN templates; decrypted only at Lambda runtime
  • IAM: Least-privilege roles (SSM:GetParameter only)
  • Cost: ~$0.00/month AWS + ~$0.0004/tweet OpenAI
  • Free Tier: Lambda (400k GB-s), EventBridge (1M events), SSM (10k params)

Testing

# Linting
pnpm lint

# Formatting
pnpm format

Project Structure

src/
├── bin/                    # CDK app entrypoint
├── handler/
│   ├── schedule-handler.ts # Main orchestration logic
│   ├── services/           # OpenAI, Twitter, CodeSnippet
│   ── util/               # Prompts, Secrets loader
├── lib/
│   ├── components/         # Reusable ScheduledLambda construct
│   └── stacks/             # CloudFormation stack definition
└── index.ts                # Local dev entrypoint

Known Limitations

  • Twitter Free Tier: few tweets/month limit
  • No duplicate prevention (stateless design)
  • Cold start latency (~3s first invocation)

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages