Laravel apps on AWS using Idealstack

laravel_on_aws (2).svgWe love Laravel at Idealstack, most of our apps are written Laravel. 

AWS is the perfect platform on which to host laravel apps as the laravel framework includes built-in support for a lot of AWS services, such as SQS, SES, S3, DynamoDB and more.

By running Laravel on top of the Idealstack platform, it will automatically be cluster-enabled across multiple servers for maximum reliability and performance.  You will also benefit from unlimited autoscaling and a 'best practice' AWS architecture (it doesn't just slap your app up on a single EC2 instance like other common hosting platforms would).  And you get all this without having to become an AWS expert or make any major changes to your code.  Read more about the features of Idealstack

Here's some tips and tricks to get the best out of laravel on AWS using Idealstack

 

Basic steps to get your laravel app working well on AWS

Sessions using DynamoDB

Idealstack installs a custom session handler that transparently saves sessions into a DynamoDB table (you can read a bit more about this solution here).  This allows for load balancing across multiple different instances/containers.  

By default, laravel apps use their own session handler, which overrides this setting.  The default setting is to use the 'files' handler which stores sessions in files in the web root.  This does work, but doesn't work particularly well as the files will be stored on an NFS share - this is both slow and liable to lead to weird race conditions if two containers try to write sessions at the same time.

Other session handlers like the database handler or redis work, but we recomend that you use PHP  native sessions - this will then use the idealstack session handler.

To use native sessions in idealstack you can use https://github.com/talandis/laravel-native-session-driver

Email using SES

Any of Laravel's mail drivers should work with Idealstack, but provided you enable it in the hosting plan, Idealstack will configure SES to send email. To use this you don't need to use the SES driver in laravel, you can use the 'sendmail' driver and avoid having to deal with any configuration.

Cron/Scheduled Tasks

In your site settings enable a scheduled task like this to run Laravel's scheduled tasks and queued jobs


 

Optional but recomended: Make use of other AWS services

One of the key benefits of the Idealstack platform is that because it runs in your own AWS account, you can plug-in other AWS services.  Laravel has built-in support for a wide variety of AWS services.   You don't need to do this to get up and running but here's some recomendations for services that are useful and easy to plug-in

Caching

Laravel ships with multiple different cache drivers that you can make use of.  The default 'file' driver isn't usually the best choice, as the EFS filesystem that Idealstack uses may not perform as well as some of the other options:

Caching in the database

Using a table in your database is often the easiest option for low-volume apps, as it's something you already have and won't need to pay any extra for.   

Caching using Redis

Redis will typically perform better than a database or file caching.  On the other hand it's another resource you must run and pay for.  Many other  things in Laravel can make use of Redis, eg for Caching, Broadcasting, Queues.  All of these work well with an Elasticache Redis instance.  To set one up create an elasticache instance in your own VPC and then use VPC-peering to connect it to Idealstack

Caching in DynamoDB

Laravel5.8+ includes a dynamodb driver for caching.  For many applications this may be a better solution than caching in Redis as DynamoDB has a 'serverless' and  'on-demand' pricing model.  Compared to Redis where you must pay for a Redis instance 24x7, you can pay for DynamoDB on an as-used basis.

To connect DynamoDB to laravel we recomend you use an IAM policy for your Idealstack site.  This means you don't need to hard-code any AWS connection details anywhere which is much more secure. 

Storing files in S3

You can store uploaded files using Laravel's default 'local' filesystem driver, and this will work fine.  The files will be stored on an AWS EFS filesystem and shared amongst all of your instances automatically.

However there are some benefits to using S3 for storage instead

  • S3 is considerably cheaper than EFS per gigabyte of storage - approximately 1/10th the price (although S3 also has charges based on how often the data is accessed)
  • You can serve up links to data stored in S3 directly, diverting requests away from your website and allowing them to be cached and served by AWS's distributed global architecture
  • S3 is usually the best service if you want to later ingest the uploaded data into other AWS services

You can read how to setup s3 storage in the laravel docs

SQS Queues

Laravel supports SQS as a queue driver (alternatively you can also use Redis or Database drivers as well) to use this from an Idealstack site you need to setup an IAM policy allowing access to the queue and then set that policy on the site. See Connecting AWS Resources using IAM policies 

Testing using AWS codebuild

Laravel has very good built-in support for testing.  We use AWS Codebuild internally to automatically run our tests every time a developer makes a commit, and we'd recomend that you do the same.  Nothing makes a bigger difference to code quality that having good tests and running them automatically.

We've written an article in our blog that talks about how to setup Codebuild to build a PHP project