What is ECS?
ECS stands for Elastic Container Service, and it's a service provided by AWS that simplifies the management of containers. Containers are lightweight, portable packages for applications. ECS helps you run and scale these containers easily in the cloud, making it simpler to deploy and manage your applications. It's particularly useful if you want to use container technology, like Docker, to build and run your software applications.
What is ECR?
ECR stands for Elastic Container Registry. It's a service provided by AWS that allows you to store, manage, and deploy container images. Think of it as a secure, cloud-based storage for your container images, making it easy to share them with others and deploy containers in services like AWS Elastic Kubernetes Service (EKS) and Amazon Elastic Container Service (ECS). In simple terms, ECR helps you store and use container images in the AWS cloud.
Before you Start
Learn to create IAM users and roles, and attach policies.
Proficient in Git and Docker.
Project Workflow
We'll set up a cluster on ECS and build an image on an EC2 Instance. This image will be pushed to ECR. Then, we'll create a task definition on ECS, which is like running a container using the image from ECR. Finally, the project will be built and accessible within the ECS cluster.
Creating a Cluster
Open the AWS Management Console and look for Amazon Elastic Container Service.
Access ECS, then click on the 'Create cluster' button.
Suggest a name for the cluster.
After scrolling down, choose 'AWS Fargate (serverless)' from the Infrastructure section.
AWS Fargate is a serverless, pay-as-you-go compute engine that lets you focus on building applications without managing servers.
To finish, simply click on the 'Create' button.
Establishing a Repository on ECR
Go to the ECR service, and in the 'Repositories' section, click the 'Create repository' button.
In the general settings, choose 'Public'*.*
Name your repository.
Scroll down and select the operating systems and system architectures that are compatible with the images in your repository.
Finally, click the 'Create repository' button, and you'll soon see your repository on the dashboard.
Setting Permissions for an IAM User
To ensure a smooth process, permissions must be granted to the IAM user. If you don't have one already, you can create a user and attach the policies demonstrated below.
AmazonEC2ContainerRegistryFullAccess,
AmazonECS_FullAccess
AWSAppRunnerServicePolicyForECRAccess
EC2InstanceProfileForImageBuilderECRContainerBuilds
IAMUserChangePassword
SecretsManagerReadWrite
Access keys are essential for connecting to the ECR. Without them, the connection won't work. If you want to learn how to create an access key for CLI, I've covered it in another blog post. You can access the instructions by Clicking here.
Building the Project
Now, let's clone the project we plan to deploy on the cluster. You can access the project by Clicking here. It's a straightforward Flask application hosted on GitHub, created to help you learn about ECS and ECR.
Clone the repository.
git clone https://github.com/LondheShubham153/flask-app-ecs.git
Navigate to the ECR repository you've set up and click on the 'View push commands' button.
Copy these commands exactly to your EC2 Instance.
aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws/u6o2f0j6 docker build -t flask-app . docker tag flask-app:latest public.ecr.aws/u6o2f0j6/flask-app:latest docker push public.ecr.aws/u6o2f0j6/flask-app:latest
These steps will generate and push the latest image of your Flask application to ECR, which will be essential for creating a task definition on ECS.
Creating Task Definition
Visit the task definition section in the ECS service.
Click 'Create new task definition' and provide a name for the task.
In the Infrastructure Requirements section, choose 'AWS Fargate,' and specify the desired CPU and memory settings.
Name the container, provide the image URL, and define the ports through which your application will be accessible.
Finally, click on the 'Create' button.
Press the 'Deploy' button, and then click 'Run task' to create and start the container.
Congratulations!๐ Your application is now successfully deployed and accessible through the specified ports.