Automated social media content generator that creates and posts AI-generated images with captions to Facebook using AWS Lambda and Hugging Face APIs.
- AI-Generated Content: Uses Hugging Face models for text and image generation
- Automated Posting: Scheduled daily posts via AWS EventBridge
- Serverless Architecture: Runs entirely on AWS Lambda (no servers to manage)
- Infrastructure as Code: Complete Terraform configuration for easy deployment
EventBridge (Cron) β Lambda Function β Hugging Face API β Facebook Graph API
- AWS Lambda: Serverless function execution
- AWS EventBridge: Scheduled triggers (daily at 2 PM UTC)
- Hugging Face API:
- Text generation (GLM-4.7-Flash)
- Image generation (Stable Diffusion XL)
- Facebook Graph API: Posts content to your Facebook page
- AWS Account with CLI configured
- Hugging Face account and API token
- Facebook Page and API credentials
- Terraform installed
- Python 3.13
- PowerShell (for Windows) or Bash (for Linux/Mac)
git clone <your-repo-url>
cd ai-content-auto-poster- Go to Hugging Face Settings
- Create a new token with
readpermissions - Copy the token
- Go to Facebook Developers
- Create an app and add Facebook Login product
- Get a Page Access Token for your Facebook Page
- Get your Page ID from your Facebook Page settings
Create a terraform.tfvars file:
lambda_function_name = "ai-content-auto-poster"
lambda_zip_path = "lambda.zip"
hf_token = "hf_your_token_here"
fb_page_token = "your_facebook_page_token"
fb_page_id = "your_facebook_page_id"terraform.tfvars to .gitignore to protect your secrets!
Windows (PowerShell):
.\build.ps1Linux/Mac (Bash):
chmod +x deploy.sh
./deploy.shterraform init
terraform plan
terraform applyType yes when prompted to confirm deployment.
.
βββ lambda/
β βββ main.py # Main Lambda handler
β βββ text_generator.py # HF text generation
β βββ image_generator.py # HF image generation
βββ main.tf # Terraform infrastructure
βββ variables.tf # Terraform variables
βββ outputs.tf # Terraform outputs
βββ requirements.txt # Python dependencies
βββ build.ps1 # Windows build script
βββ build.sh # Linux/Mac build script
βββ README.md # This file
- Runtime: Python 3.13
- Memory: 512 MB (adjustable in
main.tf) - Timeout: 300 seconds (5 minutes)
- Schedule: Daily at 2 PM UTC (configurable)
Edit the cron expression in main.tf:
resource "aws_cloudwatch_event_rule" "daily_trigger" {
schedule_expression = "cron(0 14 * * ? *)" # 2 PM UTC daily
}Cron format: cron(Minutes Hours Day-of-month Month Day-of-week Year)
Examples:
- Every hour:
cron(0 * * * ? *) - Every day at 9 AM UTC:
cron(0 9 * * ? *) - Every Monday at 10 AM UTC:
cron(0 10 ? * MON *)
# In lambda/main.py, add at the bottom:
if __name__ == "__main__":
import os
os.environ['HF_TOKEN'] = 'your_token'
os.environ['FB_PAGE_TOKEN'] = 'your_token'
os.environ['FB_PAGE_ID'] = 'your_page_id'
lambda_handler({}, None)Run:
cd lambda
python main.py- Go to AWS Lambda Console
- Find your function
- Click "Test" tab
- Create a test event (empty
{}is fine) - Click "Test"
# Using AWS CLI
aws logs tail /aws/lambda/ai-content-auto-poster --followOr view in AWS Console:
- CloudWatch β Log Groups
- Find
/aws/lambda/ai-content-auto-poster
- AWS Lambda Console β Your function β Monitor tab
- CloudWatch β Metrics β Lambda
If you get "Request must be smaller than 70MB" error:
If zipped size exceeds, you may need to:
- Remove unnecessary dependencies
- Use Lambda Layers
- Switch to Container Image deployment
Hugging Face API:
- Check token validity
- Verify model availability
- Check API rate limits
Facebook API:
- Verify Page Access Token hasn't expired
- Check page permissions
- Ensure Page ID is correct
- Never commit secrets: Keep
terraform.tfvarsin.gitignore - Use AWS Secrets Manager: For production, store secrets in AWS Secrets Manager
- Rotate tokens: Regularly rotate API tokens
- Least privilege: IAM role has minimal required permissions
AWS Costs (approximate):
- Lambda: ~$0.20/month (1 execution/day)
- EventBridge: Free tier
- CloudWatch Logs: ~$0.50/month
Total: ~$1/month
Hugging Face: Free tier includes limited API calls
Facebook: Free
# Modify code in lambda/
.\build.ps1
terraform apply# Edit requirements.txt
# Rebuild package
.\build.ps1
terraform applyterraform destroyType yes to confirm deletion of all resources.
Contributions welcome! Please:
- Fork the repository
- Create a feature branch
- Make your changes
- Submit a pull request
- Issues: Open an issue on GitHub
- AWS Documentation: https://docs.aws.amazon.com/lambda/
- Hugging Face Docs: https://huggingface.co/docs
- Terraform Docs: https://registry.terraform.io/providers/hashicorp/aws/
- Hugging Face for API access
- AWS for serverless infrastructure
- Facebook for Graph API
Happy Posting! π