Deploying Web Applications on AWS ECS Spot Instances

March 10, 2022

Deploying Web Applications on AWS ECS Spot Instances

1.What is AWS ECS?

  • Amazon ECS is a fully managed container orchestration service that makes it easy for you to deploy, manage, and scale containerized applications.
  • It deeply integrates with the rest of the AWS platform to provide a secure and easy-to-use solution for running container workloads in the cloud and now on your infrastructure with Amazon ECS Anywhere.
  • Amazon ECS comes with AWS configuration and operational best practices built-in.
  • It also means that you don’t need to manage control plane, nodes, or add-ons. It’s integrated with tools, such as Amazon Elastic Container Registry and Docker. This integration makes it easier for teams to focus on building the applications, not the environment.

2. What is AWS Spot Instance?

  • A Spot Instance is an instance that uses spare EC2 capacity that is available for less than the On-Demand price.
  • Because Spot Instances enable you to request unused EC2 instances at steep discounts, you can significantly lower your Amazon EC2 costs.

3. What is our use-case?

  • We are going to deploy customer’s dockerized applications to AWS ECS managed container service.
  • We are using Amazon Linux based spot instances to deploy the application on an Autoscaling group to scale up and down the spot instances based on the traffic that is being received to the application deployed on AWS ECS.
  • A Network load balancer will be serving the customer requests from outside, where the application and entire ECS environment will be deployed inside AWS private subnet.

Below is the architecture that explains our use case.

4. Configuring AWS ECS

  • Start by creating the ECS setup on AWS console on ECS page by selecting the OS type required for deploying the application containers in EC2 Linux + Networking.
  • We are setting up an empty cluster with the above configuration.

ASG and launch template configuration:

  • An launch-template is created, which contains the details for launching the necessary EC2 instances to serve the application running.
  • In the current setup, we have selected a custom AMI to launch instances and we have added the necessary AWS role to make sure the instances are added to the ECS cluster.
  • A plane AMI can also be selected based on the type of application that is being deployed on to the container.
  • Add the SSH keys and a role with necessary permissions for the instances to communicate with other AWS services like AWS cloud watch for logging or AWS SNS service to notify teams with instance monitoring metrics.
  • The launch template user data should contain one important detail related to the ECS cluster.
  • Above User data in the launch template version helps AWS Autoscaling group identify the exact AWS ECS cluster in deploying the containers and add the EC2 spot instances to the corresponding cluster.
  • Have the Request Spot Instances box check to No. as we want to provision the spot instances from the Autoscaling group level.
  • User data configuration helps set up the ECS environment for deployable containers on ECS by the Spot instance passing all the health checks.
  • Above details containing the launch template are added to the Autoscaling group for scale-up and scale down purposes.

Creating an AWS Autoscaling Group:

  • While creating the AWS ASG. Instance type requirements need to be checked because these details will help provision the instances by ASG in case there is any shortage of specific instances in the spot restricted to the specific region where the cluster is setup.
  • Check the launch template version on Autoscaling group is set to Latest. This helps whenever there are more versions on a single launch template, as the Autoscaling group will pick the latest version of the launch template to bring up the resources and mentioned.
  • Once the instance requirements are set in place, we need to look into Instance purchase options.
  • Here, the option to spot capacity needs to be justified 100. So the ASG will have complete authority to spin up the spot instances instead of on-demand instances.
  • Finally, set the allocation strategy to Capacity optimized as it is recommended one and check the Capacity rebalance.
  • Create Dynamic scaling policy for the ASG servers to scale in and scale out during the time heavy traffic on the load balancer.
  • These policies check the CPU for every minute and scale-up instances and scale down one instance when the CPU is less than 50%.Once the ASG is created with the above parameter, AWS Spot instances will appear in the AWS ECS console inside the Cluster.
  • In order to deploy the containerized docker image on the spot instances of the ECS cluster, a launch template and ECS service are required.
  • Navigate to the Elastic Container Service in the menu and choose Task Definitions from the left-hand bar.
  • Create new Task Definition.
  • Enter the values as shown below for Task Definition Name and Network mode and click on “Add Container”.
  • Add the container image details from ECR to the Task definition for the service to pull the images onto spot instances.
  • Configure the new service with the above Task definition by setting the number of task to 2.
  • So inside the spot instance, 2 containers will be scaled up.
  • Select the load balancer details followed by the target group details to serve the requests to containers or tasks running inside the service.
  • Set the Autoscaling of service to configure.
  • Setup Automatic Task scaling policies to ensure the Tasks running on the service will consider the policy as a metric to scale in and scale out.
  • Given policy a name and Target value for the metrics.
  • Click on the next step, review the entire Service and click create service, which will start pulling the images from ECR and deploy on the ECS console.

5. Summary

  • Amazon Elastic Container Service (Amazon ECS)is a highly scalable, high-performance container orchestration service that supports Docker containers and allows you to easily run and scale containerized applications on AWS.
  • Amazon ECS eliminates the need for you to install and operate your own container orchestration software, manage and scale a cluster of virtual machines, or schedule containers on those virtual machines.

Written by – Karanam sreenivas

Cloud Computing Insights and Resources

Challenges Faced by Gaming Companies While Adopting Cloud Services

Challenges Faced by Gaming Companies While Adopting Cloud Services 

With the advancement in technology and the progress in cloud infrastructure, the cloud gaming industry is growing at a lightning …

Challenges Faced by Gaming Companies While Adopting Cloud Services  Read More »

5 Trends in Data Analytics Impacting the Cloud World 

5 Trends in Data Analytics Impacting the Cloud World 

What is Cloud data & analytics?  Cloud analytics is the process of analysing data stored in the cloud. It is …

5 Trends in Data Analytics Impacting the Cloud World  Read More »

How Microsoft Azure Cloud Services Different From Competitors

How Microsoft Azure Cloud Services is Different From its Competitors?

Microsoft Azure is one of the eminent cloud computing platforms in the market created by Microsoft for building, deploying, and …

How Microsoft Azure Cloud Services is Different From its Competitors? Read More »