tenongene / quotegen_ecs

0 stars 0 forks source link

Writing Terraform manifests for deployment on AWS via an Autoscaling group associated ECS Cluster #1

Open tenongene opened 10 months ago

tenongene commented 10 months ago

To deploy the application via ECS, I had to modify the autoscaling group to be a capacity provider for a new ECS cluster and write new resources for the creation of a cluster, a service and a task definition to run the containers.

The load balancer and target groups had to be modified as well

Image

I decided to go with an application load balancer this time around, since I garnered several insights from the first phase of the project. I changed the application port in the application to serve at port 80, and added a health check route at "/health" in the application, and that is how the load balancer would report instances as healthy.

Image

=========================================================================

Created a launch template resource with the required EC2 configuration. The ami used thi time was an ECS optimized Amazon Linux ami. Saved the cluster name to the environment variable and inserted into the ECS agent config file. Created and IAM instance profile for the instances for authorization.

Image

Image

=====================================================================

Created the cluster, capacity provider and attached the capacity provider to the cluster.

Image

======================================================================

Wrote the service definition file, referencing the capacity provider strategy for autoscaling, the load balancer, the task definition (below) to use and I attached an IAM role that I created on the console to be used by the service load balancer.

Image

Image

========================================================================

The task definition for the container parameters with the container name, the image to pull form docker hub, the port mapping, and attached the execution role created on the console for authorization. The task is to use the "bridge" network mode.

Image

Image

==========================================================================

I created the lambda function for the CI/CD process, whose URL will be used as a Webhook in docker hub.

Image

Image

The idea is to use the "Update Service Command" of the ECS client Nodejs SDK. It has a method called "ForceNewDeployment", and when set to "true" and executes on an event, it simply instructs the service to create a new deployment. The service will then create new deployment of the same quantity, but in a ROLLING UPDATE strategy where one container at a time is terminated then updated. This prevents downtime for the service and creates a smoother transition to any image updates. The new containers will then have to pull the newest image in docker hub.

Image

The function URL was:

Image

Image