Mastodon on AWS: Host your own instance
While Twitter seems to be in chaos, the free and decentralized alternative, Mastodon, is rising. At first glance, Twitter and Mastodon are similar. A toot is to Mastodon what a tweet is to Twitter. However, the decentralized nature of Mastodon makes a big difference. If you want to register with Mastodon, you first have to choose an instance (see instances.social). And it’s even possible to host your own Mastodon instance. That’s what we are up to: social.cloudonaut.io.
In the following, I will share details on how to deploy a Mastodon instance on AWS in case you want to host your own server as well.
The following figure shows our architecture to deploy Mastodon on AWS.
- Route 53
- Application Load Balancer (ALB)
- ECS + Fargate
- RDS for Postgres
- ElastiCache for Redis
What’s missing? So far, I did not deploy Elasticsearch yet. Elasticsearch provides optional search capabilities for Mastodon, for example, to search through your toots. I also haven’t deployed CloudFront as a CDN yet in front of the static files hosted on S3 and maybe even for some files delivered by the web application itself.
Looking for a new challenge?
Estimating costs for AWS is not trivial. My estimation assumes a small Mastodon instance for 1-50 users. The architecture’s monthly charges are about $60 per month. The following table lists the details.
|Service||Configuration||Monthly Costs (USD)|
|ECS + Fargate||3 Spot Tasks x (0.25 CPU + 0.5 GB)||$8.66|
|RDS for Postgres||t4g.micro (Multi-AZ)||$23.61|
|ElastiCache for Redis||t4g.micro (Single-AZ)||$11.52|
|ALB||Load Balancer Hours||$16.20|
|S3||25 GB + requests||$0.58|
|Route 53||Hosted Zone||$0.50|
Please note that my cost estimation is not complete. For example, the estimation does not include network traffic, CloudWatch, SES, or domain.
Isn’t there a cheaper way to host Mastodon on AWS?
Of course, you could also run Mastodon, the Postgres database, the Redis in-memory database, and even Elasticsearch on a virtual machine. Assuming all that fits on an EC2 instance of type
t4g.medium, you are paying about $25 per month. That’s a simple and cost-effective way to run Mastodon on AWS. However, you get the following benefits by outsourcing data storage to services like RDS, ElastiCache, and S3:
- You are able to scale the application layer horizontally by adding virtual machines or containers.
- You reduce maintenance effort. For example, managed services like RDS come with automated patching and backups.
- You increase the resilience and availability as the managed services spread the storage layer among multiple availability zones out of the box.
- You enable 0-downtime deployments, as you can spin up virtual machines or containers with the new version before terminating the old ones.
Luckily, Mastodon maintains a container image tootsuite/mastodon that we can use to deploy the three services a Mastodon instance consists of:
webdelivers the web application consisting of a frontend and backend (Ruby on Rails).
streamingallows clients to subscribe to server-sent events for real-time updates via a long-lived HTTP connection or WebSocket.
sidekiqorchestrates and executes background jobs.
As all three services are bundled into a single container image, it is necessary to use different commands when starting the containers.
bundle exec rails s -p 3000to start the
node ./streamingto start the streaming API.
bundle exec sidekiqto start
Configuring Mastodon is simple. All parameters are configurable via environment variables. See Configuring your environment for a list of all parameters.
- Aurora Serverless v1/v2 is not a good choice for Mastodon, as there is load on the database even if no users are actively using the instance. So Aurora Serverless v1 cannot pause. In general, Aurora Serverless is quite expensive, a minimal RDS for Postgres instance reduces costs.
- Mastodon stores the
Hometimeline in Redis. I replaced an ElastiCache instance which resulted in an empty
Hometimeline that Mastodon backfills asynchronously.
- The following configuration files from the Mastodon repository help understand how to deploy Mastodon containers: Dockerfile, docker-compose.yml, and nginx.conf.
- As we are currently running a minimal Mastodon instance, I could not verify it, but it should be possible to scale Mastodon horizontally (except the Postgres database).
- Updating from version
v4.0.2worked by replacing the container image tags in my CloudFormation template.
We started working on an Infrastructure as Code project about a week ago: widdix/mastodon-on-aws. The project allows you to deploy Mastodon on AWS with a few clicks in about 30 minutes.
First, you need an AWS account.
Second, a top-level or subdomain where you can configure an NS record to delegate to the Route 53 nameservers is required. For example, you could register a domain with Route 53 or use an existing domain and add an NS record to the hosted zone.
Third, click here to deploy Mastodon on AWS to your AWS account.
To generate the required secrets and keys, use the following commands.
# Start Docker container locally
I’m curious to see if Mastodon catches on as an alternative to Twitter for us. Running your own Mastodon instance under your domain is a plus. And with AWS and our Infrastructure as Code project widdix/mastodon-on-aws, it’s not hard at all.