DEV Community

Cover image for 🚒 AWS 138: Container Orchestration - Deploying Apps with Amazon ECS Fargate
Hritik Raj
Hritik Raj

Posted on

🚒 AWS 138: Container Orchestration - Deploying Apps with Amazon ECS Fargate

AWS

πŸ—οΈ From Registry to Runtime: Mastering the ECS Fargate Workflow

Hey Cloud Engineers πŸ‘‹

Welcome to Day 38 of the #100DaysOfCloud Challenge!
Today, we are completing the full container lifecycle for the Nautilus project. We aren't just storing an image; we are running it in a serverless, scalable environment using Amazon ECS (Elastic Container Service) with the Fargate launch type. This is the ultimate "NoOps" way to run containers.

This task is part of my hands-on practice on the KodeKloud Engineer platform, which I highly recommend for anyone looking to master real-world DevOps scenarios.


🎯 Objective

  • Create a private Amazon ECR repository named devops-ecr.
  • Build, tag (latest), and push a Python app image from the aws-client host.
  • Provision an ECS Cluster named devops-cluster using Fargate.
  • Register a Task Definition named devops-taskdefinition using the ECR image.
  • Deploy a Service named devops-service to maintain a running task.

πŸ’‘ Why ECS Fargate is the Future

In the old days, you had to manage the EC2 instances that ran your containers. With Fargate, AWS manages the underlying servers for you.

πŸ”Ή Key Concepts

  • Task Definition: The "Blueprint." It defines which image to use, how much CPU/Memory is needed, and which ports to open. [Image of the ECS Task Definition configuration showing CPU, Memory, and Container Image settings]
  • Cluster: The logical grouping. It’s the "home" where your services and tasks live.
  • Service: The "Manager." It ensures that your desired number of tasks (in our case, at least 1) are always running and healthy.

πŸ› οΈ Step-by-Step: The ECS Deployment Workflow


πŸ”Ή Phase A: Prepare the Image (ECR)

  • Create Repo: aws ecr create-repository --repository-name devops-ecr
  • Login & Build: Authenticate Docker and build the image at /root/pyapp.

  • Push: Tag the image with your full ECR URI and push it to the cloud.

πŸ”Ή Phase B: Configure the Cluster & Task

  • Cluster: Create devops-cluster. When choosing the infrastructure, select AWS Fargate (serverless).

  • Task Definition: Create devops-taskdefinition.

    • Infrastructure: Fargate.
    • Task Role/Execution Role: Ensure it has permissions to pull from ECR.
    • Container Image: Point to your ECR Image URI (e.g., <account>.dkr.ecr.<region>.amazonaws.com/devops-ecr:latest).

πŸ”Ή Phase C: Launch the Service

  • Deploy Service: Inside the cluster, create a service named devops-service.
  • Deployment Option: Select your devops-taskdefinition.
  • Desired Tasks: Set this to 1.
  • Networking: Ensure the service is in a subnet with internet access (or a NAT Gateway) so it can pull the image from ECR.

βœ… Verify Success

  • Monitor Deployment: Go to the Services tab in your cluster.
  • Confirm: πŸŽ‰ Once the service status turns to Active and the task status turns to 🟒 RUNNING, your containerized application is live!


πŸ“ Key Takeaways

  • πŸš€ Serverless Containers: No EC2 instances to patch, scale, or manage.
  • πŸ›‘οΈ IAM Roles: The Task Execution Role is critical; without it, ECS cannot "log in" to ECR to pull your image.
  • πŸ•’ Provisioning: Fargate tasks take about 1-2 minutes to pull the image and start the container.

🚫 Common Mistakes

  • Networking: Running a Fargate task in a private subnet without a NAT Gateway or S3/ECR VPC Endpoints. The task will fail to pull the image!
  • Memory/CPU Mismatch: Fargate has specific allowed combinations of CPU and Memory. If you choose an invalid pair, the task won't register.
  • Execution Role: Forgetting to grant the execution role ecr:GetDownloadUrlForLayer permissions.

🌟 Final Thoughts

You’ve just built a modern, cloud-native deployment pipeline! This ECS setup is exactly how thousands of companies run their microservices at scale. By mastering ECR and ECS Fargate, you are now equipped to handle complex, high-traffic container workloads.


🌟 Practice Like a Pro

If you want to try these tasks yourself in a real AWS environment, check out:
πŸ‘‰ KodeKloud Engineer - Practice Labs

It’s where I’ve been sharpening my skills daily!


πŸ”— Let’s Connect

Top comments (0)