Using AWS RDS with Laravel on Forge

James Fairhurst
4 min readMar 17, 2016

--

A man looking where to start with AWS — https://unsplash.com/photos/Ixp4YhCKZkI

In my last post I went through how-to setup an AWS server using Laravel Forge, I mentioned that MySQL is really memory hungry and a possible solution for low memory servers would be to disable it altogether and use RDS. Forge does now come with MariaDB by default but I don’t know if it’s as memory hungry as MySQL, I guess a moot point considering it’s also available in RDS.

Click the Launch DB Instance on the RDS dashboard and you can see the various options. Aurora is Amazon’s own MySQL-compatible offering and supposed to be cheaper & faster however I was unable to select a small cheap instance so I didn’t go for it. I usually use MySQL so that’s what I’ll go for here.

On the next step I went for the Dev/Test option, if you’re on the free tier and want to play around you can use this free of charge.

I then selected a t2.micro instance which will be more than enough for a simple app. Standard 5GB then added the settings so that I can identify the instance and give it some credentials so that I can login. We’ll also use these in Forge when changed the site’s environment variables.

Use the default Network & Security options on the next step and enter a database e.g. forge so that it’s created on setup. You can also set your backup preferences here too. Once done your database should be up & running (it may take a little while). One of the immediate major benefits is that AWS will backup everything on a nightly basis for you. One massive thing that you don’t have to worry about. These are stored in the Snapshots area and allow you to restore them to a brand new instance and then you’ll be able to swap to it in your site’s env variables. Pretty great although it seems only InnoDB tables are supported due to technical limitations of MyISAM so be sure to check what you have and whether you’re able to change to reap the benefits.

If you expand the instance by clicking on the arrow in the second column you’ll see the Endpoint which is a full hostname to your database. In Forge head to the site’s Environment tab and Edit the file. As I setup the RDS instance using the MySQL details that forge gave me I only needed to update the DB_HOST so it looks like this (I’ve replaced the actual hostname & password with xxx):

DB_HOST=forge.xxx.eu-west-1.rds.amazonaws.com
DB_PORT=3306
DB_DATABASE=forge
DB_USERNAME=forge
DB_PASSWORD=xxx

The last thing we need to-do is allow the EC2 instance to connect to the RDS database. By default it’s locked down so nothing can connect to it. Admittedly I don’t yet know enough about AWS VPC to easily allow access from an EC2 instance, a brief search led me to this and looks like you create a private subnet and give EC2 access to RDS that way. I’m going to use a simpler way and just add a rule to the security group that RDS automatically creates which is called rds-launch-wizard. Click on that from the expanded instance details, click on the Inbound tab and then the Edit button. Choose MySQL/Aurora as the type and the port will be automatically filled in. Enter the custom IP address (e.g. xxx.xxx.xxx.xxx/32) of the EC2 instance (found in either Forge or on the EC2 dashboard) plus /32 and finally press the Save button. Hopefully that should do it.

To test that everything works SSH into your forge server, cd into your site’s dir and run:

php artisan migrate

Hopefully it will connect and run, either creating those migrations if it’s the first time or inform you that there’s nothing to run. If you get an error here double check your Laravel .env file and ensure those MySQL connection details are correct and if that doesn’t work double check the AWS rules for your EC2 instance IP.

You can also connect independently by using an app such as Sequel Pro. It has an SSH option which really comes in handy and saves you from messing about with SSH tunnels.

Once you’re happy it’s working we can stop the mysql service on the EC2 instance, you can either do that in Forge at the bottom of the server page under Stop Services or manually on the command line if you’re still logged in via SSH:

sudo service mysql stop

Success

Using the “free -m” command the amount of free memory shot up from around 80 to 500 which is a massive saving. Another utility I used was htop to see what processes were using memory, it’s quite handy to see and an improved version of the standard top command. You can install it via the usual apt-get:

sudo apt-get install htop

You can then sort processes by memory or cpu for example. Hopefully that helps some folk!

--

--