GitHub - https://github.com/SUBHAM-NANDI/Project11-Demo-AWS-ECS
What is Amazon ECS?
Amazon Elastic Container Service (ECS) is a fully managed container orchestration service offered by AWS. It enables you to easily run, stop, and manage containers on a cluster of EC2 instances or using AWS Fargate, a serverless compute engine for containers.
How ECS Works
ECS manages containerized applications by organizing them into clusters. These clusters can run on either EC2 instances or Fargate, depending on your requirements:
Task Definitions: These are blueprints that define how the containers should run, including details such as CPU/memory allocation, Docker image, and networking.
Tasks and Services: A task is a single running instance of a container, while a service ensures that a specified number of tasks are running at all times. Services can also integrate with load balancers to distribute traffic.
Clusters: A cluster is a logical grouping of tasks or services. ECS manages scaling, deployment, and networking for containers within a cluster.
Advantages of ECS
Managed Service: ECS is fully managed by AWS, meaning you donβt have to worry about infrastructure, patching, or updates.
Integration with AWS Services: ECS integrates seamlessly with other AWS services like CloudWatch for monitoring, IAM for role-based access, and ECR for storing container images.
Cost-Effective: Using ECS, particularly with Fargate, helps you avoid over-provisioning of resources, which reduces costs as you only pay for the exact compute capacity you use.
Scalability: ECS can scale your applications dynamically based on traffic or load without manual intervention.
Disadvantages of ECS
AWS Lock-In: Since ECS is a proprietary AWS service, migrating your workloads to a non-AWS environment may be challenging. Kubernetes, on the other hand, is cloud-agnostic and can run across multiple cloud platforms.
Limited Customization with Fargate: While Fargate eliminates the need to manage EC2 instances, it also limits control over underlying resources. Some organizations might prefer the fine-tuned customization that Kubernetes provides.
Complexity for Larger Workloads: For more complex applications or multi-cloud strategies, Kubernetes offers more flexibility and is better suited for handling large-scale deployments with intricate configurations.
Deploying a Python Flask Application on AWS ECS with Fargate
In this tutorial, we'll walk through the steps to deploy a Python Flask application on AWS Elastic Container Service (ECS) using Fargate. ECS is a scalable and secure way to run containers, and Fargate makes it easier by allowing you to run containers without needing to manage the underlying infrastructure.
Prerequisites
Before you begin, you will need:
- An AWS account
- Basic knowledge of Docker and containerized applications
- AWS CLI and Docker installed on your local machine
Step 1: Prepare the Application and Docker Image
We will deploy a simple Python Flask application. The source code consists of a Dockerfile
and a Python file app.py
.
-
app.py
(Python Flask Application):
from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return "Hello, World!" @app.route('/greet') def greet(): return "Greetings from Flask!" if __name__ == "__main__": app.run(host='0.0.0.0', port=3000)
Dockerfile
:
This Dockerfile creates a container image for our Flask application.
```Dockerfile
FROM python:3.8-slim
WORKDIR /app
COPY app.py /app
RUN pip install flask
EXPOSE 3000
CMD ["python", "app.py"]
```
Step 2: Build and Push Docker Image to Amazon ECR
1. Create an ECR Repository
- Open the AWS Management Console, search for ECR (Elastic Container Registry), and create a private repository for your Docker image.
2. Build and Push the Docker Image
First, log in to your AWS ECR by running the following command:
aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <aws_account_id>.dkr.ecr.<region>.amazonaws.com
Next, tag your Docker image with the ECR repository URI:
docker build -t flask-app .
docker tag flask-app:latest <aws_account_id>.dkr.ecr.<region>.amazonaws.com/flask-app:latest
Finally, push the image to your ECR repository:
docker push <aws_account_id>.dkr.ecr.<region>.amazonaws.com/flask-app:latest
Step 3: Create an ECS Cluster
- In the AWS Management Console, search for ECS and select Elastic Container Service.
- Click on Create Cluster.
- Choose Fargate as the launch type and proceed with the default settings. Name the cluster as
demo-ecs-cluster
. - The cluster will be created in a few moments.
Step 4: Define Task Definition
A task definition tells ECS how to run Docker containers.
- In the ECS console, navigate to Task Definitions and click Create New Task Definition.
- Choose Fargate as the launch type.
-
Set up the task with the following configurations:
-
Task Name:
demo-ecs-task
- Operating System Family: Linux
- Task Execution Role: Select or create a role that allows access to ECR and CloudWatch.
- Task Size: Use the smallest available options (0.5GB memory, 0.25 vCPU).
-
Task Name:
-
Add the container to the task definition:
-
Container Name:
flask-container
-
Image: The URI of the Docker image from ECR (e.g.,
<aws_account_id>.dkr.ecr.<region>.amazonaws.com/flask-app:latest
). - Port Mappings: Container port 3000 (the port on which the Flask app runs).
-
Container Name:
Enable logging by adding CloudWatch Logs in the Log Configuration section.
Click Create to finish setting up the task definition.
Step 5: Run the Task on ECS
- In the ECS console, navigate to the Clusters section and select your
demo-ecs-cluster
. - Click Run Task, and select the task definition you created (
demo-ecs-task
). - Choose Fargate as the launch type and click Run.
The task will start, and ECS will pull the Docker image from ECR and run it on the cluster.
Step 6: Access the Application
Once the task is running, you can access the Flask application by navigating to the Tasks section in your ECS cluster. Locate the public IP address of the running task, and open the following URLs in your browser:
-
Home:
http://<public_ip>:3000/
-
Greet:
http://<public_ip>:3000/greet
You should see the messages "Hello, World!" and "Greetings from Flask!" respectively.
Step 7: Clean Up
To avoid unnecessary charges, delete the ECS cluster and stop the running task after testing the application. You can also delete the ECR repository if no longer needed.
Top comments (0)