How I Deployed My #100DaysOfCloud Twitter Bot on AWS Fargate

Deploying a Twitter bot on AWS

The #100DaysOfCloud is a fun challenge that encourages people to spend 100 days learning about and building projects on the cloud. Created by Forrest Brazeal, author of The Read Aloud Cloud, the challenge has gained popularity in the cloud community as a great way to commit to leveling up your cloud skills.

As a cloud enthusiast myself, I loved the idea of #100DaysOfCloud. But I noticed the challenge was missing something that its predecessor, #100DaysOfCode, had – a Twitter bot to engage with people posting about their progress.

So I decided to put my cloud skills to the test and develop a Twitter bot for #100DaysOfCloud. The bot would retweet posts using the challenge hashtag, encouraging people along their journey. For deploying the bot, I chose to use containers on AWS Fargate for a scalable yet hands-off infrastructure.

In this post, I‘ll walk through how I built the #100DaysOfCloud Twitter bot and deployed it on AWS Fargate. I‘ll share code snippets, configuration steps, and lessons learned along the way. Whether you‘re looking to deploy your own Twitter bot or just learn more about Fargate, this post will provide an end-to-end guide. Let‘s dive in!

Prerequisites

Before we get started with building and deploying the Twitter bot, there are a few prerequisites you‘ll need:

1. AWS account – You‘ll need an AWS account to deploy the bot on Fargate. If you don‘t have one, you can sign up for the AWS Free Tier which provides free access to many services for 12 months.

2. Twitter Developer account – To interact with the Twitter API, you need to apply for a developer account. Once approved, you can create an app and obtain the API key, API secret key, Access token, and Access token secret. Keep these handy as you‘ll need them to authenticate your bot.

3. Docker – The bot will run in a Docker container on AWS Fargate. Therefore, you need to have Docker installed on your local machine to build the container image. You can download Docker Desktop from the official website.

4. Code repository – The complete code for this project is available on my GitHub repo. Clone or fork this repo to get started quickly.

With these prerequisites in place, we‘re ready to start building the Twitter bot!

Developing the Twitter Bot

The #100DaysOfCloud Twitter bot is a Python application that uses the Tweepy library to interact with the Twitter API. Here‘s a high-level overview of how the bot works:

  1. Authenticate with the Twitter API using the consumer key, consumer secret, access token, and access token secret.

  2. Search for tweets containing the #100DaysOfCloud hashtag.

  3. Retweet the tweets and like them.

  4. Wait for 15 minutes before repeating steps 2-4. This is to avoid hitting Twitter API rate limits.

Here‘s the key part of the code that achieves this functionality:

def retweet_tweets(self):
    for tweet in tweepy.Cursor(self.api.search, q=‘#100DaysOfCloud‘).items(100):
        try:
            tweet.retweet()
            tweet.favorite() 
            print(f"Retweeted tweet {tweet.id} successfully!")
            sleep(15*60)
        except tweepy.TweepError as e:
            print(e)
            sleep(15*60)

This code uses the Tweepy Cursor to search for the 100 most recent tweets with #100DaysOfCloud. It then retweets and likes each tweet, with a 15 minute pause in between to avoid exceeding rate limits. Any errors are caught and printed.

I implemented the bot as a Docker container to make it portable across environments. The Dockerfile to build the container looks like:

FROM python:3.8

WORKDIR /app
COPY . /app

RUN pip install tweepy

CMD ["python", "main.py"]

It uses the official Python base image, copies the application code, installs the Tweepy dependency, and specifies the command to run the main bot script.

One challenge I ran into was with Twitter API rate limits. The bot would often exceed the allowed requests and get an error. To solve this, I added the 15 minute wait time between retweets. This kept the bot under the rate limits while still allowing it to engage with plenty of #100DaysOfCloud tweets.

With the bot developed and dockerized, I was ready to deploy it on AWS Fargate for a scalable, low-maintenance solution.

Deploying on AWS Fargate

AWS Fargate is a serverless compute engine for containers. It allows you to run containers without having to manage the underlying infrastructure. You simply specify the container image, CPU and memory requirements, and networking configuration, and Fargate takes care of provisioning and scaling the infrastructure to run it.

I chose to deploy my #100DaysOfCloud Twitter bot on Fargate for a few key reasons:

  1. Scalability – Fargate can automatically scale the number of containers based on the specified CPU and memory requirements. So if my bot starts getting a lot of traffic, Fargate can spin up more containers to handle the load.

  2. Managed infrastructure – With Fargate, I don‘t need to worry about patching, scaling, or securing the underlying servers. AWS takes care of all that heavy lifting, letting me focus on the application.

  3. Cost – Fargate has a pay-as-you-go pricing model where you only pay for the resources your containers consume. For a small application like this bot, the costs would be minimal, likely only a few dollars per month.

Here are the steps I followed to deploy my Twitter bot on AWS Fargate:

Step 1: Create an ECS Cluster

First, I created an Amazon ECS cluster to deploy my Fargate tasks. An ECS cluster is a logical grouping of tasks or services. You can create a cluster in the ECS web console under Clusters > Create Cluster.

Create ECS Cluster

I used the cluster creation wizard to create a Networking only cluster powered by AWS Fargate. I specified a name for my cluster and kept the other default settings. Fargate will leverage this networking configuration to VPC, subnets, and security groups to run my container.

Step 2: Create Task Definition

Next, I created a task definition for my Twitter bot container. A task definition specifies the container image, resource requirements, networking, logging, and other configuration details. Think of it as a blueprint for running a container.

In the ECS web console under Task Definitions, I clicked Create new Task Definition and selected the Fargate launch type. Then I configured the task as follows:

  • Task definition name: twitter-bot-td
  • Task role: ecsTaskExecutionRole
  • Task execution role: ecsTaskExecutionRole
  • Task memory: 1GB
  • Task CPU: 0.5 vCPU
  • Container name: twitter-bot
  • Image: .dkr.ecr.us-east-1.amazonaws.com/twitter-bot:latest
  • Port mappings: 80
  • Environment variables:
    • API_KEY
    • API_SECRET_KEY
    • ACCESS_TOKEN
    • ACCESS_TOKEN_SECRET

The container image refers to where I pushed my Docker image in Amazon ECR. And the environment variables are the Twitter API credentials.

Configure task definition

Step 3: Configure the Service

With the task definition created, I was ready to run it as a service. An ECS service allows you to specify the number of simultaneous task instances to run, auto scaling, VPC, load balancing, and more.

In the ECS web console under Amazon ECS > Clusters > twitter-bot-cluster, I clicked Services > Create. I configured the service with the following details:

  • Launch type: Fargate
  • Task Definition: twitter-bot-td
  • Service name: twitter-bot-svc
  • Number of tasks: 1

Under Deployments, I chose the Rolling update deployment type to ensure my bot would be updated with zero downtime. I used the default VPC and subnets, and for security group I allowed inbound traffic on port 80.

Configure service

Once I clicked Create Service, Fargate began provisioning the service and within seconds my Twitter bot task was running.

Service running

My Twitter bot was now live! It was happily retweeting and liking #100DaysOfCloud tweets. Reflecting on the process, I was surprised how quick and easy it was to get a containerized application deployed on Fargate. The wizard-like interface made configuring the cluster, task, and service straightforward. AWS has done a great job making containers accessible for all developers.

If you plan on deploying your own bot or application on Fargate, here are a few tips to keep in mind:

  • Fargate pricing is based on the vCPU and memory resources your task uses. Be sure to monitor your task‘s resource utilization and adjust the configuration if needed to optimize costs.

  • Fargate tasks use CloudWatch for logging by default. Check CloudWatch logs if you need to debug any issues with your task.

  • For production deployments, be sure to configure auto scaling on your service to handle variable traffic.

  • Fargate can integrate with a variety of other AWS services like ALB, CloudWatch, X-Ray, and more. Take advantage of these integrations for a robust, full-stack application.

Conclusion

In this post, I shared my experience building and deploying a Twitter bot on AWS Fargate. It was a fun challenge and a great way to practice my cloud, containers, and serverless skills.

To recap, the key steps were:

  1. Set up a Python Docker container for the bot and push the image to Amazon ECR
  2. Create an ECS task definition specifying the container image, resource requirements, and environment variables
  3. Configure an ECS service to run the bot task in a serverless Fargate cluster

Along the way, I learned how to work with the Twitter API, debug container issues, and configure the various Fargate resources like VPC and security groups. The process gave me a deeper understanding of containers and an appreciation for how accessible they‘ve become with managed services like Fargate.

I‘ve made the code for my Twitter bot available on GitHub, so feel free to check it out and deploy your own bot! I encourage you to customize it to engage with a community you‘re passionate about.

And if you haven‘t yet, consider joining the #100DaysOfCloud challenge. It‘s a rewarding way to grow your skills, build your portfolio, and connect with the vibrant cloud community. My bot and I will happily cheer you along the way!

Have you deployed your own application on AWS Fargate? Let me know about your experience in the comments below! And if you have any questions, don‘t hesitate to reach out. Happy coding!

Similar Posts