Install Discourse on Amazon Web Services (AWS)

:warning: This installation method is not officially endorsed. Use at your own risk.

The Discourse team only recommend installation using the official install guide

This guide originally included information about cloudflare, but @pfaffman thinks that conflating AWS and cloudflare is a bad idea. If you’re an AWS user, AWS Cloudfront makes much more sense than Cloudflare. @pfaffman edited this a good bit on 2022.11.21; if something seems broken, blame me. If it works, thank the original author!

Discourse is a pretty awesome piece of software and it come with plenty of options for hosting. We wanted to setup our on AWS with dedicated servers for each services for maximum scalability and performance

If you want an hassle free easy deployment I recommend using the default Docker install. If you want a fancier setup then read on.

For this tutorial let’s assure you have an Amazon Web Services account with a registered domain name you control.

Before we start

Make sure you create all of your instances in the same region since there are not fees for bandwidth in the same region. You will also get better performance.

VPC

The first thing we need to create is the VPC. It’s your virtual network infrastructure and it’s a bit of a pain to get it working properly.

In the VPC dashboard create a VPC and a subnets within the VPC. The database need a second subnet in a different zone but within the same region. For example if your first subnet is in region us-west-1a then the second must be in us-west-1b or 1c. So go ahead and create another one.

To make things easy to setup we will create a “allow all” security group. Click on the “Security Groups” link and then “Create Security Group”. Make sure to select your VPC in the option. Once it’s created click on “Inbound Rules” and choose “All Traffic”, “All” protocols, “ALL” port and “0.0.0.0/0” as the source.

Make sure you also have a working Internet Gateway and Routing Table. Still not sure how those two should be setup up properly. I added 0.0.0.0/0 to my Routing Table to get my servers to talk to each other correctly.

EC2

In the EC2 Dashboard go to Instance and Launch Instance. Select Ubuntu. As the “Instance Type” you are free to choose whatever you need but t2.micro meet the minimum requirements and is eligible for free hosting.

Later you might want to take a look at reserved instances to reduce your costs.

Once you selected your instance type click “Next”. Make sure you create your instance in your VPC and your first subnet. In the storage pane hit next. The 8GB default is way enough since we’re not going to host images and database on the server. Chose the “Allow All” security group and start your instance.

Create a new key pair and save it somewhere safe.

ElasticIP

Create and allocate an elastic ip to your EC2 instance. This will allow you to create new EC2 instances (to upgrade the OS or switch to a different size instance) without needing to change your DNS.

RDS

Before we setup the database we need to find what PostgreSQL version Discourse use. You can look into this Docker postgres template. Search for “chown postgres:postgres /shared/postgres_run/” and you should see the version on this line.

As of 2022.11.21 Discourse uses 13.8

Once you have the pgSQL version create your DB instance. Use whatever configs you want but make sure you set the same database version or you will run into problems while running backups.

If you do end up with a different Postgres than Discourse supports, you can use discourse_docker/templates/postgres.12.template.yml at main · discourse/discourse_docker · GitHub as a model to see how to remove the wrong Postgres client and install the new one.

t2.micro instances will work fine.

Elasticache

Next go the Elasticache dashboard and create a Redis cache cluster. t2.micro will once again work fine.

Elasticache is pretty expensive, and @pfaffman thinks that it doesn’t offer a ton of benefits. You might consider using the included redis in docker on your EC2 as you might do with a two-container installation.

S3

Follow the first part of this guide, but be sure to enter your S3 configuration in ENV variables as described in Configure an S3 compatible object storage provider for uploads.

SES

Amazon allow any account to send 10,000 free e-mails from EC2 hosted servers. That’s way enough for starting Discourse community.

In the SES dashboard add and verify your domain. Do not check “Generate DKIM Settings”. Add the verification TXT record to your DNS. Your domain should get verified in about 15 minutes.

When it does, return to SES dashboard, visit “SMTP Settings” and hit “Create My SMTP Credentials”. Add a user and note the “Access Key ID” and the secret key. You can’t get the secret key back so save it in a safe place.

@pfaffman adds that configuring SES requires a few more steps than that and you would be better served finding an SES Howto anywhere else on the internet.

Installing Discourse

Now that our servers are ready to roll we can install Discourse. Read and follow the install Discourse in the Cloud under 30 minutes guide.

If you use

 ./discourse-setup --two-container

You can then choose to remove postgres template from the data.yml or just ignore data.yml altogether (or delete it). It will also give you a web_only.yml that includes the ENV variables for the database settings.

Of your EC2 is a t2.micro you will need to setup swap. Running ./discourse-setup will create it for you.

Edit the web_only.yml to enter credentials for the database and redist/elasticache

  • For database use your RDS endpoint, db user/pass/name.
  • If your database is not named discourse add a DISCOURSE_DB_NAME environment variable.
  • For Redis use your Elasticache endpoint (or use the one in data.yml).

If your build fails or you need to rebuild, make sure your run “./launcher cleanup” or your server will fill up fast.

Security Groups

What we will do here is setup our servers to only allow required connections so we can secure things up. In order for this to happen you’ll need to create a security group for EC2, Elasticache and RDS.

Elastichache and RDS should only accept incoming connections from your EC2 VPC private ip.

If you need to connect to SSH, add your ip to the security group then remove when done.

Backups

Discourse can save your backups to Amazon S3 and Glacier. Saving them to S3 is dead simple but to Glacier can be quite confusing. Make sure you read those two guiides:

Finishing touches

Then you can follow all the optional guides to enable additional features and launch your community!

If you activate backup uploads to your S3 bucket you can even configure the bucket to store backups on Amazon Glacier which is even cheaper.

27 Likes