Deploy a Dockerized Laravel Application to AWS ECS with EC2 Instance Launch type using GitHub Actions
In these Article , will see about how to deploy a docker-ized Laravel applications to AWS ECS EC2 launch type. My application has multiple images , say php as the app server , nginx as the web server and mysql as the db server. All these services are in docker images and will deploy using GHA
I already have my code in the 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
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 wide-spread 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 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 launch in the cluster( i have given 1)
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)
Here my nginx (web server) 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
In the network settings of the nginx container definition , we need to add the network settings , so that communication between two containers will happen.
3. Create the Service of type EC2 in the cluster with Application load balancer and attach the Task definition. Set the number of task and deployment type as Rolling update.
Before Creating load balancer , create target group for type instance and protocol as http and port 8.Leave the registered targets section and click on Create target group .
Attach these target group to the load balancer of Type Application with internet facing scheme . If you have 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.
Attach these load balancer when creating the service in the cluster.
4. Now push the Images to ECR through GHA as part of CICD.Just push / Merge your code to Master branch.
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.
And that’s it! You have successfully deployed a Laravel application on Amazon ECS. You can view the application using DNS of the Load balancer.Our Laravel docker container is now running.
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 update the ECS service. Finally, we setup Elastic Container Service to run the image on Amazon EC2.
To save cost, make sure you shutdown everything related to ECS. This includes: services, task, instances, and load-balancer
ECS provide a 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 Repo : https://github.com/karthickcse05/docker-php-laravel-ecs