Deploy A Dockerized Laravel Application To AWS ECS With EC2 Instance Launch Type Using GitHub Actions

In this article, will see about how to deploy a dockerized Laravel application to AWS ECS EC2 launch type. My application has multiple images, say PHP as the app server, nginx as the webserver, and MySQL as the database server. All these services are in docker images and will deploy using GHA.

I already have my code on Github.

If you want to create a fresh Laravel application using composer by running the following command.

composer create-project — prefer-dist laravel/laravel docker-php-laravel-ecs

or using docker through the below command,

docker run — rm -v $(pwd):/app composer create-project — prefer-dist laravel/laravel docker-php-laravel-ecs

Usage of Containers

Containers have gradually become a widespread and widely accepted technology. They are a standard unit of software that encapsulates your code, together with the dependencies required to make them function. With containers, you can be certain your system will function in a similar manner even in different environments.

As cloud services have become an integral part of application deployment, the need to effectively build, deploy and maintain containers in the cloud has become imperative.

There are a number of services designed to help developers achieve this, one of which is AWS Elastic Container Service — A highly available and high performance container orchestration service. It allows you to run, scale and secure docker containers in AWS.

Let’s try to deploy a simple Laravel application to ECS. We will need,

  • Docker images of our app(Nginx, app, Mysql)
  • ECS Cluster
  • ECS Task Definition
  • ECS Service
  • Elastic Container Repository
  • Application Load Balancer

Create a Cluster of type EC2 Linux + Networking. Choose instance type as per your wish ( I have chosen t2.micro) and number of instances to be launched in the cluster( I have given 1)

Select Cluster Template

Select Cluster Template

2. Create Task definition - it is simply a configuration file that will be launched by ECS. Choose type as EC2. Task size is required for tasks using the Fargate launch type and is optional for the EC2. Add the container ( I am adding my nginx(web) and app server images)

Create Task definition

Create Task definition

Create Task definition

Here my Nginx (webserver) depends on app server. So we need to create a dependency link between both the container. For that click on Nginx, container definition and navigate to startup dependency ordering and select the app container and the condition as START, because my app container should run before the web container.

Nginx container definition

In the network settings of the Nginx container definition, we need to add the network settings, so that communication between two containers will happen.

Nginx container definition

3. Create the Service of type EC2 in the cluster with the Application load balancer and attach the Task definition. Set the number of task and deployment type as Rolling update.

Configure EC2 Service

Before creating load balancer, create target group for target type as instance with protocol as HTTP and port 80. Leave the registered targets section blank and click on Create target group.

Configure EC2 Service

Attach these target group to the load balancer of Type Application with internet facing scheme. If you have a valid SSL certificate, then you can add listeners as HTTPS with port 443, otherwise, let the listeners be HTTP with port 80. Choose the VPC and Subnet ( Subnet must attach the internet gateway, then only it should be publicly accessible). Add the security group and add the respective port in the inbound rules, otherwise for testing open for all traffic in the inbound rules.

Configure EC2 Service

Attach these load balancer when creating the service in the cluster.

Configure EC2 Service

4. Now push the Images to ECR through GHA as part of CICD.Just push / Merge your code to Master branch.

Configure EC2 Service

After pushing the images to ECR, as part of Continuous deployment, GHA will retrieve the task definition and update the image in that file and update the service and cluster.

Configure EC2 Service

Configure EC2 Service

Configure EC2 Service

Deploy a Dockerized Laravel Application

And that’s it !!! You have successfully deployed a dockerized Laravel application on Amazon ECS. You can view the application using DNS of the Load balancer. Our Laravel docker container is now running.

dockerized Laravel application on Amazon ECS

In this article, we set up docker for a Laravel application that contains multiple images like Nginx, Redis, Mysql, PHP. We also implemented CICD using GHA to build the docker image, pushing it to Elastic Container Registry and updating the ECS service. Finally, we setup Elastic Container Service to run the image on Amazon EC2.

Add-Ons

To save cost, make sure you shut down everything related to ECS. This includes services, task, instances, and load-balancer.

ECS provides very useful CPU/Memory Utilisation metrics. Pay attention to how much you actually need to save more cost.

Configure Autoscaling .We can set auto-scaling policy for our Task. We can set the minimum, desired, and maximum number of tasks.

Github Repohttps://github.com/karthickcse05/docker-php-laravel-ecs