Containers

AWS ECS Fundamentals for Newbies: A Comprehensive Guide with Labs

Admin
Oct. 16, 2024, 4:32 p.m.

Subscribe to Newsletter

Be first to know about new blogs, training offers, and company news.

AWS ECS Fundamentals for Newbies: A Comprehensive Guide

Amazon Elastic Container Service (ECS) is a highly scalable container orchestration service that makes it easy to run, stop, and manage Docker containers on a cluster of virtual machines. Whether you're building a microservices architecture or just want to manage containerized applications, ECS offers a robust solution. This guide will introduce you to the basics of ECS, walk you through setting up your environment, and provide practical labs to help you get hands-on experience.

What is AWS ECS?

AWS ECS is a managed container service that enables you to run and manage Docker containers on a cluster of EC2 instances or with AWS Fargate, a serverless compute engine for containers. ECS handles the scheduling and running of containers, scaling, and load balancing, making it easier to deploy and manage applications in containers.

Why Use ECS?

  • Managed Service: Simplifies the management of containerized applications.
  • Scalability: Easily scale your applications up or down based on demand.
  • Integration: Seamlessly integrates with other AWS services like IAM, CloudWatch, and ALB.
  • Flexibility: Choose between managing your own EC2 instances or using Fargate for a serverless experience.

Getting Started with AWS ECS

1. Understanding ECS Components

  • Cluster: A logical grouping of EC2 instances or Fargate tasks.
  • Task Definition: A blueprint for your application specifying Docker images, CPU, memory, and other configurations.
  • Service: Ensures that a specified number of task instances are running and provides load balancing.
  • Task: A single running instance of a task definition.

2. Setting Up Your Environment

Before you start working with ECS, make sure you have:

  1. An AWS Account: Sign up if you don’t have one already.
  2. AWS CLI Installed: For interacting with AWS services from the command line.
  3. Docker Installed: To build and manage Docker containers.

Creating Your First ECS Application

1. Prepare a Docker Image

Create a simple Docker container image. For this example, we'll use a basic Nginx container.

  1. Create a Dockerfile

Create a directory named nginx-app and inside it, create a file named Dockerfile with the following content:


FROM nginx:latest
COPY index.html /usr/share/nginx/html/index.html

   2. Create an index.html File

In the same directory, create a file named index.html with the following content:


<html>
<body>
  <h1>Hello, ECS!</h1>
</body>
</html>

3. Build the Docker Image

Run the following command to build the Docker image:


docker build -t my-nginx-app .

4. Push the Docker Image to Amazon ECR

To push an image to ECR, we need to create the repo in the management console and then use the CLI commands to push the image to the registry.

  • Create a Repository: Open the ECR console from the AWS management console and create a new repository named my-nginx-app.
  • Prepare the image and push it using the following 3 steps. Do this from the CLI.
    • Tag the Image:
      • replace <account-id> with your account ID
      • replace <region> with your region

 docker tag my-nginx-app:latest <account-id>.dkr.ecr.<region>.amazonaws.com/my-nginx-app:latest

    • Authenticate Docker to ECR:
      • replace <account-id> with your account ID
      • replace <region> with your region

 aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <account-id>.dkr.ecr.<region>.amazonaws.com

    • Push the Image:
      • replace <account-id> with your account ID
      • replace <region> with your region

docker push <account-id>.dkr.ecr.<region>.amazonaws.com/my-nginx-app:latest

Alternatively, a more straightforward way is to get the commands from the ECR registry.

  1. Click on the image repo that you created earlier
  2. Click View push commands indicated in orange. The screen will look like below. You can just copy and paste the commands without making any changes:

Deploy the Image to ECS Cluster 

  1. Create a Cluster
  • Open the ECS console and click Create Cluster.
  • Give it a name like devcluster
  • Ensure AWS Fargate (serverless) is selected. (This is the default value)
  • Click Create
  • wait for the cluster to be ready

    2. Create a Task Definition

  • On the ECS navigation panel (to the left), click Task Definitions.
  • Click Create new Task Definition.
  • Give it a name like taskdefinition1
  • Ensure AWS Fargate is selected 
  • Specify task details such as task role and task execution role. Leave the rest as default.
  • Under Container-1, specify container definitions (use the image URL from ECR), and resource requirements.

Example container definition:

{ "name": "nginx",

"image": "<account-id>.dkr.ecr.<region>.amazonaws.com/my-nginx-app:latest",
"essential": true,
"memory": 512, "cpu": 256, }

Leave the rest as default and then click Create.

Wait for the service to be ready.

3. Create a Service

Note:

  1. To create a service, we need to create a load balancer.
  2. To create a load balancer, we need to create a target group
  3. Follow the steps below to create a Target Group and then an Application Load Balancer, and then connect the load balancer to the ECS Service.  
 

Create a target group

Complete the following steps:

  1. Open the Amazon Elastic Compute Cloud (Amazon EC2) console
  2. In the navigation pane, choose Target groups.
  3. Choose Create target group.
  4. For Choose a target type, choose Instances or IP addresses.
    Important: If your service's task definition uses the awsvpc network mode for the AWS Fargate launch type, then you must choose IP addresses. Tasks that use awsvpc are associated with an elastic network interface, not an EC2 instance.
  5. For Target group name, enter a name.
  6. For Protocol : Port, choose a protocol and port combination.
  7. For VPC, choose the virtual private cloud (VPC) that you want to attach to your target group.
  8. (Optional) For Health checks, specify a health check protocol and path.
  9. Choose Next.
  10. On the Register targets page, choose Create target group.

Create an Application Load Balancer and associate your target group with it

Complete the following steps:

  1. Open the Amazon EC2 console.
  2. In the navigation pane, choose Load Balancers.
  3. Choose Create Load Balancer.
  4. On the Compare and select load balancer type page, under Application Load Balancer, choose Create.
  5. For Load balancer name, enter a name for your load balancer.
  6. For Network mapping, specify your VPC and Availability Zones or subnets.
    Note: Choose subnets from at least two different Availability Zones.
  7. For Security groups, choose the security group that you want to associate with the load balancer.
  8. For Listeners and routing, choose a protocol and port. For Default action, select your target group.
  9. Choose Create load balancer.

Use the previous Application Load Balancer to create an Amazon ECS service

Complete the following steps:

  1. Open the Amazon Elastic Container Store (Amazon ECS) console.
  2. In the navigation pane, choose Clusters, and then select your cluster.
  3. In the Services section, choose Create.
  4. In the Environment section, choose a LaunchType or CapacityProvider strategy.
  5. In the Deployment configuration section, enter the following information:
    For Application type, choose Service.
    For Task definition, choose a task definition family and revision. If you don't have a task definition, then create one.
    Important: To use a load balancer, you must use containerPort in your task definition.
    For Service name, enter a service name.
    For Desired tasks, enter the number of tasks.
  6. In the Load balancing section, enter the following information:
    For Load balancer type, choose Application Load Balancer.
    For Container, select the container in the task definition that you want to register as Target in the target group.
    For Application Load Balancer, select Use an existing load balancer
    From the Load balancer dropdown list, select the your Application Load Balancer.
    For Listener, select the Use an existing listener, and then choose the listener that you pointed to the target group.
    For Target group, select the Use an existing target group.
    For Target group name, choose your target group.
  7. Choose Create.
  8. Choose View Service, and then choose the Tasks tab.
  9. Choose the refresh icon, and then verify that your tasks are in the RUNNING state.

View your tasks registered in the target group

Complete the following steps:

  1. Open the Amazon EC2 console.
  2. Choose Target Groups, and then choose your target group.
  3. To view the registered targets in the target group, choose the Targets tab.

 

Practical Labs

Lab 1: Launch a Simple Web Application

  1. Follow the steps above to create an ECS Cluster, Task Definition, and Service using the Nginx Docker image.
  2. Verify Deployment: Check the ECS Console to see if your service is running and the desired number of tasks are active.
  3. Access Your Application: If you used an Application Load Balancer (ALB), find the ALB DNS name in the EC2 console and visit it in your browser to see your "Hello, ECS!" page.

Lab 2: Update and Roll Back Your Application

  1. Update the Docker Image
  • Modify index.html to display a new message.
  • Rebuild and push the Docker image to ECR.
  1. Update the Task Definition
  • Create a new revision of your task definition with the updated image.
  • Update the ECS service to use the new task definition revision.
  1. Verify the Update: Check the application in your browser to see the new message.
  2. Rollback (if necessary): If something goes wrong, you can update the ECS service to use a previous revision of the task definition.

Conclusion

AWS ECS simplifies the deployment and management of containerized applications, offering flexibility and scalability. By understanding the core components of ECS and following practical labs, you can start building and managing containerized applications efficiently.

As you become more comfortable with ECS, explore advanced features such as service auto-scaling.

Zero-to-Hero Program: We Train and Mentor you to land your first Tech role