Skip to content

Latest commit

 

History

History
187 lines (142 loc) · 5.31 KB

File metadata and controls

187 lines (142 loc) · 5.31 KB

Deployment Guide

Complete guide for deploying the Twitter Repost Agent to AWS Lambda.

Prerequisites

  • AWS CLI configured with appropriate credentials
  • Twitter Developer account with app created
  • DeepSeek API key
  • Node.js 18.x or higher installed locally

Step 1: Twitter App Setup

  1. Go to Twitter Developer Portal
  2. Navigate to your app → SettingsUser authentication settings
  3. Set App permissions to "Read and Write"
  4. Save and go to Keys and tokens tab
  5. Generate/regenerate these credentials:
    • API Key (Consumer Key)
    • API Secret (Consumer Secret)
    • Bearer Token
    • Access Token (generate AFTER setting Read/Write permissions)
    • Access Token Secret

Important: Access tokens inherit permissions at generation time. Generate them only after setting "Read and Write" permissions.

Step 2: AWS Secrets Manager

  1. Go to AWS Secrets ManagerStore a new secret
  2. Select "Other type of secret"
  3. Add key-value pairs:
    {
      "twitter_api_key": "your_api_key",
      "twitter_api_secret": "your_api_secret",
      "twitter_access_token": "your_access_token",
      "twitter_access_token_secret": "your_access_token_secret",
      "twitter_bearer": "your_bearer_token",
      "deepseek_api_key": "your_deepseek_api_key"
    }
  4. Secret name: TwitterRepostSecrets
  5. Leave other settings as default and create

Step 3: DynamoDB Table

  1. Go to DynamoDBCreate table
  2. Table name: RepostedTweets
  3. Partition key: tweetId (Type: String)
  4. Table settings: On-demand capacity mode
  5. Click Create table
  6. After creation, go to table → Additional settingsTime to Live (TTL)
  7. Click Enable TTL
  8. TTL attribute name: ttl
  9. Click Enable

Step 4: Create Deployment Package

In your project directory:

# Install dependencies
npm install

# Create deployment zip
zip -r function.zip index.js package.json package-lock.json node_modules/

Step 5: Create Lambda Function

  1. Go to AWS LambdaCreate function
  2. Select "Author from scratch"
  3. Function name: TwitterRepostAgent
  4. Runtime: Node.js 22.x
  5. Click Create function

Step 6: Upload Code

  1. In the Lambda function page, click "Upload from"".zip file"
  2. Select your function.zip
  3. Click Save

Step 7: Configure Environment Variables

  1. Go to Configuration tab → Environment variablesEdit
  2. Add variables:
    • Key: SECRET_NAME → Value: TwitterRepostSecrets
    • Key: POSTED_TWEETS_TABLE → Value: RepostedTweets
  3. Click Save

Step 8: Adjust Timeout

  1. Go to ConfigurationGeneral configurationEdit
  2. Change Timeout to 120 seconds (2 minutes)
  3. Click Save

Step 9: IAM Permissions

  1. Go to ConfigurationPermissions
  2. Click on the Role name (opens IAM in new tab)
  3. Click Add permissionsAttach policies
  4. Search and attach these policies:
    • SecretsManagerReadWrite
    • AmazonDynamoDBFullAccess
  5. Click Add permissions

Step 10: Test the Function

  1. Go to Test tab
  2. Click Create new event
  3. Event name: TestRepost
  4. Leave JSON as default {}
  5. Click Save
  6. Click Test to execute

Expected success response:

{
  "statusCode": 200,
  "body": "{\"message\":\"Quoted tweet https://twitter.com/...\",\"comment\":\"...\",\"result\":{...}}"
}

Common errors:

  • 429: Rate limit - wait 5-10 minutes before testing again
  • 403/401: Check Twitter app permissions are "Read and Write"
  • 500: Check CloudWatch logs for detailed error

Step 11: Set Up Daily Trigger

  1. Go to ConfigurationTriggersAdd trigger
  2. Select "EventBridge (CloudWatch Events)"
  3. Create a new rule
  4. Rule name: DailyRepostTrigger
  5. Rule type: Schedule expression
  6. Schedule expression: cron(0 9 * * ? *) (daily at 9 AM UTC)
  7. Click Add

Other schedule examples:

  • Every 12 hours: cron(0 0,12 * * ? *)
  • Twice daily (9 AM & 9 PM UTC): cron(0 9,21 * * ? *)
  • Every 6 hours: cron(0 */6 * * ? *)

Step 12: Verify Deployment

  1. Check the test execution succeeded
  2. Verify the quote tweet was posted on Twitter
  3. Check DynamoDB table has a new entry with the posted tweet ID
  4. Go to Monitor tab → View CloudWatch logs to see execution details

Monitoring

Access logs: Lambda → MonitorView CloudWatch logs

Key log messages:

  • Successfully posted quote tweet: [comment]
  • ⚠️ AI comment generation failed - Using fallback comment
  • ℹ️ No new tweets to quote - All recent tweets already reposted

Updating the Function

When you make code changes:

# Update code locally
# Recreate zip
zip -r function.zip index.js package.json package-lock.json node_modules/

# Upload via Lambda console or AWS CLI
aws lambda update-function-code \
  --function-name TwitterRepostAgent \
  --zip-file fileb://function.zip

Cleanup (If Needed)

To remove all resources:

  1. Delete Lambda function
  2. Delete EventBridge rule
  3. Delete DynamoDB table
  4. Delete Secrets Manager secret
  5. Delete IAM role (auto-created by Lambda)

Deployment complete! Your bot will now automatically repost the best content from your target account daily.