π Automating Twitter Data Fetching and Image Generation using AWS Fargate, Python, and S3

A step-by-step guide to building a serverless pipeline that retrieves tweets, converts them into images, and uploads them to Amazon S3 β all automatically!
π§ Introduction
In this project, weβll build a serverless workflow that automatically:
Fetches tweets using the Twitter (X) API
Converts tweet text into a quote-style image
Uploads the image to an Amazon S3 bucket
Runs the entire process in the cloud using AWS Fargate (ECS) β no servers, no EC2s!
This project is great for:
Developers exploring AWS Fargate
Beginners learning Docker + ECS
Anyone curious about automating APIs and cloud storage
π§© Architecture Overview
Hereβs the high-level flow:
Twitter API β Python App β Docker β ECR β ECS Fargate β CloudWatch Logs β S3Components:
| Component | Purpose |
| Twitter API (v2) | Fetch recent tweets |
| Python (Tweepy + Pillow + Boto3) | Retrieve, generate image, upload |
| Amazon S3 | Store the generated images |
| Amazon ECR | Host Docker container |
| AWS Fargate (ECS) | Run the app serverlessly |
| CloudWatch Logs | Capture logs and task output |
βοΈ Step 1 β Get a Twitter Developer Account
Go to developer.x.com
Create a free developer project
In the use-case description, write something like:
βEducational project to fetch tweets and create images stored in S3 using AWS.β
Once approved, go to Projects & Apps β Keys and Tokens
Copy your Bearer Token
πͺ£ Step 2 β Create an S3 Bucket
In AWS Console β S3 β Create Bucket
Name:
tweet-images-yourname-demoRegion:
us-east-1Keep all defaults (Block Public Access = ON)
Click Create Bucket
π Step 3 β Build the Python App
Project structure:
twitter-python-aws/
βββ main.py
βββ requirements.txt
requirements.txt
boto3
tweepy
Pillow
main.py
import os, io, textwrap, datetime
import boto3
from PIL import Image, ImageDraw, ImageFont
import tweepy
# Environment variables
BEARER = os.environ["X_BEARER_TOKEN"]
QUERY = os.environ.get("TWEET_QUERY", "aws fargate -is:retweet lang:en")
BUCKET = os.environ["S3_BUCKET"]
REGION = os.environ.get("AWS_REGION", "us-east-1")
# Fetch recent tweets
client = tweepy.Client(bearer_token=BEARER, wait_on_rate_limit=True)
resp = client.search_recent_tweets(query=QUERY, max_results=10, tweet_fields=["created_at","lang"])
tweets = resp.data or []
text = tweets[0].text if tweets else "No tweets found."
# Create image
W, H = 1200, 630
img = Image.new("RGB", (W, H), (245, 245, 255))
draw = ImageDraw.Draw(img)
font = ImageFont.load_default()
wrapped = textwrap.fill(text, width=50)
draw.text((50, 200), wrapped, font=font, fill=(0, 0, 0))
draw.text((50, 50), "Tweet Snapshot", font=font, fill=(90, 90, 90))
timestamp = datetime.datetime.now(datetime.UTC).strftime("%Y-%m-%d %H:%M UTC")
draw.text((50, 550), f"{timestamp}", font=font, fill=(120, 120, 120))
# Upload to S3
buf = io.BytesIO()
img.save(buf, format="PNG")
buf.seek(0)
key = f"tweets/{datetime.datetime.now(datetime.UTC).strftime('%Y%m%d_%H%M%S')}.png"
s3 = boto3.client("s3", region_name=REGION)
s3.put_object(Bucket=BUCKET, Key=key, Body=buf, ContentType="image/png")
print(f"β
Uploaded s3://{BUCKET}/{key}")
π§± Step 4 β Test Locally
Set your environment variables:
export X_BEARER_TOKEN="your-twitter-bearer-token"
export S3_BUCKET="tweet-images-yourname-demo"
export AWS_REGION="us-east-1"
Run it:
pip install -r requirements.txt
python3 main.py
β Output:
β
Uploaded s3://tweet-images-yourname-demo/tweets/20251028_113456.png
π³ Step 5 β Containerize with Docker
Dockerfile
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "main.py"]
Build and test locally:
docker buildx build --platform linux/amd64 -t twitter-fargate-demo .
πͺ£ Step 6 β Push to Amazon ECR
aws ecr create-repository --repository-name twitter-fargate-demo
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <account-id>.dkr.ecr.us-east-1.amazonaws.com
docker tag twitter-fargate-demo:latest <account-id>.dkr.ecr.us-east-1.amazonaws.com/twitter-fargate-demo:latest
docker push <account-id>.dkr.ecr.us-east-1.amazonaws.com/twitter-fargate-demo:latest
π§Ύ Step 7 β Create IAM Roles
1οΈβ£ Execution Role
Name:
ecsTaskExecutionRolePolicy:
AmazonECSTaskExecutionRolePolicy
2οΈβ£ Task Role
Name:
twitter-fargate-task-roleAdd this inline policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["s3:PutObject", "s3:PutObjectAcl"],
"Resource": "arn:aws:s3:::tweet-images-yourname-demo/*"
}
]
}
βοΈ Step 8 β Create ECS Task Definition
Launch type: Fargate
Task size: 0.25 vCPU / 512 MB
Task Role:
twitter-fargate-task-roleExecution Role:
ecsTaskExecutionRole
Add Container
| Field | Value |
| Name | twitter-fargate-container |
| Image URI | <account-id>.dkr.ecr.us-east-1.amazonaws.com/twitter-fargate-demo:latest |
| Essential | Yes |
| Port | (none) |
| Environment Variables | X_BEARER_TOKEN, S3_BUCKET, AWS_REGION |
| Log Driver | awslogs (/ecs/twitter-fargate-demo) |
Click Add container β Create β
π Step 9 β Run the Task
Go to ECS β Clusters β Create Cluster (Fargate)
Name it
twitter-clusterClick Run new task
Cluster:
twitter-clusterLaunch type: Fargate
Task definition:
twitter-fargate-demoSubnet: default public
Public IP: ENABLED
Run Task β
πͺΆ Step 10 β Verify CloudWatch Logs
Open CloudWatch β Log groups β /ecs/twitter-fargate-demo
Expected output:
β
Uploaded s3://tweet-images-yourname-demo/tweets/20251028_113456.png
Your image will be visible inside the S3 bucket folder /tweets/.
β° Bonus β Automate with EventBridge Scheduler
Go to Amazon EventBridge β Scheduler β Create schedule
Frequency:
rate(1 hour)Target: ECS RunTask
Choose:
Cluster:
twitter-clusterTask Definition:
twitter-fargate-demo
Save!
Now AWS Fargate runs your container automatically every hour π
π§© Final Architecture Recap

π― Conclusion
With just a few components, we built a fully serverless pipeline that:
Runs Python in AWS Fargate
Fetches live tweets via API
Generates styled quote images
Uploads them to S3 automatically
Can scale and schedule without managing servers!
This is a perfect example of combining APIs, Docker, and AWS Cloud to build automation pipelines.
You can easily extend it to:
Generate dynamic social media graphics
Archive trending tweets
Power a dashboard or newsletter
π§ Tech Stack Summary
AWS Fargate (ECS)
Amazon S3
CloudWatch Logs
Docker
Python 3.11
Tweepy + Pillow + Boto3
π¬ Final Thoughts
Building this end-to-end pipeline was a fantastic way to explore how AWS Fargate lets developers run any containerized workload β no servers, no maintenance, just deploy and forget.
βοΈ Author: Shikhar Shukla
A full-stack developer & cloud enthusiast passionate about automating cool ideas.



