In the ever-evolving landscape of cloud computing, serverless containers offer an efficient and scalable way to deploy modern applications. Combining Laravel, a popular PHP framework, with AWS Fargate, a serverless container computing engine, can lead to a robust, cost-effective, and maintainable solution. This guide will walk you through deploying a serverless Laravel app on AWS Fargate using Infrastructure as Code (IaC) with AWS Cloud Development Kit (CDK).
Introduction: Serverless Containers with Laravel and AWS Fargate
Laravel is renowned for its elegance and simplicity in building robust web applications. When paired with AWS Fargate, developers can focus on writing code rather than managing infrastructure. Fargate abstracts away the need to provision or manage servers, making it an ideal choice for running large-scale containerized applications. This serverless approach ensures that you only pay for the computing resources you use, enhancing cost efficiency.
Infrastructure Deployment with CDK: Network Setup and Asset Provisioning
To kickstart our deployment, we use AWS CDK, which allows us to define cloud infrastructure in code. The first step involves setting up the network infrastructure, including a Virtual Private Cloud (VPC) with subnets, route tables, and security groups.
const vpc = new ec2.Vpc(this, ‘MyVpc’, {
maxAzs: 3, // Default is all AZs in the region
});
const cluster = new ecs.Cluster(this, ‘MyCluster’, {
vpc: vpc
});
Next, we provision other necessary resources like IAM roles, ECS clusters, and security groups. The CDK enables us to define and deploy these resources in a repeatable and version-controlled manner, ensuring consistency across environments.
Laravel Application Development: Building and Dockerizing the App
With the infrastructure set, it’s time to focus on the Laravel application. Start by building your Laravel app locally. Once your app is ready, the next step is to create a Dockerfile to containerize the application.
Here’s a simple Dockerfile for a Laravel app:
FROM php:8.1-fpm
# Install system dependencies
RUN apt-get update && apt-get install -y \
build-essential \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
vim \
unzip \
git \
curl
# Install PHP extensions
RUN docker-php-ext-install pdo pdo_mysql mbstring exif pcntl bcmath gd
# Set working directory
WORKDIR /var/www
# Copy existing application directory contents
COPY . /var/www
# Copy existing application directory permissions
COPY –chown=www-data:www-data . /var/www
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD [“php-fpm”]
This Dockerfile sets up the environment, installs necessary dependencies, and configures the PHP-FPM server to handle requests.
Container Image Management: Uploading to AWS ECR
Once the Docker image is built, it needs to be uploaded to Amazon Elastic Container Registry (ECR) for deployment. Start by creating a repository in ECR:
aws ecr create-repository –repository-name my-laravel-app
Next, tag your Docker image and push it to the ECR repository:
docker tag my-laravel-app:latest <aws_account_id>.dkr.ecr.<region>.amazonaws.com/my-laravel-app:latest
docker push <aws_account_id>.dkr.ecr.<region>.amazonaws.com/my-laravel-app:latest
AWS ECR securely stores your Docker images and integrates seamlessly with AWS Fargate, ensuring a smooth deployment process.
Fargate Deployment: Launching the Application in the Cloud
With the Docker image stored in ECR, we can deploy the Laravel application on AWS Fargate. Using AWS CDK, define the ECS task definition, specify the container details, and set up the Fargate service:
const taskDefinition = new ecs.FargateTaskDefinition(this, ‘TaskDef’);
const container = taskDefinition.addContainer(‘LaravelContainer’, {
image: ecs.ContainerImage.fromEcrRepository(repository),
memoryLimitMiB: 512,
cpu: 256,
});
container.addPortMappings({
containerPort: 9000,
protocol: ecs.Protocol.TCP,
});
new ecs.FargateService(this, ‘Service’, {
cluster,
taskDefinition,
desiredCount: 1,
});
This configuration will deploy your Laravel application on Fargate, scaling it as needed based on demand.
Conclusion: Success with Serverless Laravel on AWS Fargate
Deploying a serverless Laravel application on AWS Fargate using IaC offers a highly scalable, secure, and maintainable solution. With the flexibility of AWS CDK and the power of Fargate, you can easily manage your infrastructure while focusing on delivering high-quality software.
References
The serverless LAMP stack part 4: Building a serverless Laravel application