Day-48: Exploring AWS ECS and Setting up Nginx

Dhruv MoradiyaDhruv Moradiya
3 min read

Today’s journey dives into AWS Elastic Container Service (ECS), a powerful tool for managing and running containerized applications. Let’s break down ECS, understand its comparison with EKS, and finally implement Nginx on ECS.


What is ECS?

ECS (Elastic Container Service) is a fully managed container orchestration service from AWS. It simplifies running and managing Docker containers without worrying about the underlying infrastructure.

Key Features of ECS:

  • Launch Types:

    • Fargate: AWS manages the infrastructure.

    • EC2: You manage the underlying EC2 instances.

  • Integration: Seamlessly integrates with AWS services like ELB, Auto Scaling, and Amazon VPC.

  • Scalability: Automatically scales workloads with ease.

  • Flexibility: Supports Docker Compose and Kubernetes workflows.


ECS vs. EKS

FeatureECSEKS
ArchitectureCentralized control planeDistributed Kubernetes control plane
Kubernetes SupportNot natively supportedFully managed Kubernetes service
ScalingManual scaling policiesAuto-scaling Kubernetes clusters
FlexibilitySimpler, AWS-driven orchestrationHighly customizable Kubernetes setup
CommunityAWS-centric, smaller communityLarge open-source Kubernetes community

Task: Setting up Nginx on ECS

Requirements:

  1. AWS Account.

  2. AWS CLI configured.

  3. Basic knowledge of Docker and containerization.


Step 1: Create a Docker Image for Nginx

  • Write a Dockerfile:
FROM nginx:latest  
COPY index.html /usr/share/nginx/html
  • Create a simple index.html file:
<!DOCTYPE html>  
<html>  
<head>  
    <title>Welcome to ECS</title>  
</head>  
<body>  
    <h1>Hello from Nginx on ECS!</h1>  
</body>  
</html>
  • Build and push the Docker image to Amazon ECR (Elastic Container Registry):
# Authenticate Docker to ECR  
aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <account-id>.dkr.ecr.<region>.amazonaws.com  

# Build and tag the image  
docker build -t nginx-ecs .  

# Push the image  
docker tag nginx-ecs:latest <account-id>.dkr.ecr.<region>.amazonaws.com/nginx-ecs:latest  
docker push <account-id>.dkr.ecr.<region>.amazonaws.com/nginx-ecs:latest

Step 2: Set Up an ECS Cluster

  1. Create an ECS Cluster:

    • Go to ECS Console → Clusters → Create Cluster.

    • Choose Networking only (for Fargate).

  2. Configure the Cluster:

    • Add a name for the cluster.

    • Configure networking (choose an existing VPC and Subnets).


Step 3: Define a Task Definition

  1. Create a Task Definition:

    • Navigate to ECS → Task Definitions → Create.

    • Select Fargate as the launch type.

  2. Add Container Details:


Step 4: Deploy the Service

  1. Create a Service:

    • Go to your ECS Cluster → Services → Create Service.

    • Choose Fargate and your task definition.

  2. Service Details:

    • Number of tasks: 1.

    • VPC and Subnets: Select those used earlier.

    • Configure security groups to allow HTTP traffic on port 80.


Step 5: Test Your Deployment

  • Retrieve the Public IP or Load Balancer URL from the service.

  • Open the URL in a browser to see the Nginx welcome page:

    Hello from Nginx on ECS!


Key Takeaways:

  • ECS simplifies containerized application management with minimal infrastructure overhead.

  • Choosing between ECS and EKS depends on your workload and familiarity with Kubernetes.

  • Deploying Nginx on ECS showcases how easily applications can be containerized and scaled using AWS tools.

Happy Learning! 🚀

0
Subscribe to my newsletter

Read articles from Dhruv Moradiya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Dhruv Moradiya
Dhruv Moradiya