Deploying a Scalable Laravel Application with AWS CDK: A Step-by-Step Guide

Devon
3 min readFeb 19, 2024

--

Let’s dive into a fun and enlightening adventure together where we’ll give our Laravel app a cloud-powered supercharge. Picture this: we’re going to wrap our app snugly in Docker’s container magic, making it a breeze to move and run anywhere we like. Then, we’re taking a plunge into the vast ocean of AWS. We’ve got a lot on our plate — setting up a no-sweat, serverless spot for our app with Fargate, getting our data cozy in an Aurora database, tucking away our files in the safe arms of S3, and speeding things up with some quick-thinking ElastiCache.

Prerequisites

Before starting, ensure you have the following prerequisites installed and configured:

  1. AWS Account: Make sure you have an AWS account set up.
  2. AWS CLI: Install and configure the AWS CLI with your credentials.
  3. AWS CDK: Install the AWS Cloud Development Kit.
  4. Docker: Install Docker on your local machine.
  5. Node.js and npm: Required for running the AWS CDK.
  6. PHP and Composer: Ensure you have PHP and Composer installed for Laravel development.

First lets start on the Laravel side

For your Laravel app to run in a production docker environment we will need to create a Dockerfile and an initial sh script to run after the container has started.

I’ve created a GitHub repo here, with all the files needed for the docker setup.

The files under the docker/ directory are needed for various services, make sure to remove what you don’t need and add anything thats missing.

That should be all you need to add to your Laravel project for the time being, you should test that you can build your docker image as well, you can do this by running the following in your terminal:

docker build . -f Dockerfile.prod

On to CDK

For this tutorial we will be setting up the following services:

  • ECS (Fargate Load Balanced Service)
  • ECR (Container Repo)
  • RDS (Using Aurora Serverless and Mysql)
  • S3 with Cloudfront
  • Route53
  • Elasticache (Redis)
  • Cloudfront
  • Secrets Manager
  • EC2 (Jumphost for RDS db connections)

Add some comments if you would like to see anything else deployed added to the template.

There are 2 files in the Git repo, stack.ts (Our stack definition) and def.ts (The main entrypoint for your CDK app.)

To get Started run the following commands:

cdk init app --language=typescript

# install the required dependencies
npm install @aws-cdk/aws-ecs @aws-cdk/aws-ecs-patterns @aws-cdk/aws-ec2 @aws-cdk/aws-rds @aws-cdk/aws-s3 @aws-cdk/aws-elasticache

Copy the values from the git repo, replacing the placeholders as needed.

You will need to manually create a new secret in aws and copy the secret Arn into your definition file.

You will see in the definition file that you can adjust the values for each environment, such as RAM, CPU and Elasticache values.

The evironment setup can be broken down as follows:

  • We create an ECR repository that our image can be deployed to.
  • We create 2 S3 buckets, one for private files and one for public files.
  • We setup a cloudfront distribution for caching of public assets.
  • We setup our serverless database with aurora along with subnets & secrets
  • We add auto scalling to our our database with min and max capacities.
  • We create our Elasticache(Redis) service and connection requirements.
  • We create our public hosted route53 zone, and ssl certificates.
  • We load up our secrets from our arn we defined earlier.
  • We setup our Fargate service along with environment variables that need to be passed to container.
  • We inject secrets into our container.
  • We grant the necessary access for our fargate service to our previosly setup services.
  • We create our loadbalancer link to our services and route53.
  • We add some required policies for our services
  • We setup autoscaling for our fargate service both CPU and Memory at 60% usage.
  • We create a new EC2 instance (JumpHost) and give it access to our database.
  • Finally we output some values to the console and aws panel such as domains and ECS repo url.

Thanks for reading, if you can think of anything else that should be added or any improvements to this tutorial please add a comment.

--

--