Containers on AWS — Amazon Elastic Container Service (ECS) on AWS Fargate
Tags: aws, cloud, containers, fargate
Published: 2023-03-13 06:23:32

![Architecture](https://cdn-images-1.medium.com/max/2000/1*t5C_XDfg9Ymys9Jea7FDxw.png)

Hello everyone, I want to write an Amazon ECS tutorial. [Amazon ECS](https://aws.amazon.com/ecs) is an AWS service that can run highly secure, reliable, and scalable containers. Amazon ECS runs on Amazon EC2 and also can be serverless using [AWS Fargate](https://aws.amazon.com/fargate). In this tutorial, I try Amazon ECS using AWS Fargate and Amazon ECR. Let’s go!

Before creating the ECS cluster, create VPC for the ECS cluster. You can see [the link](https://dev.to/budionosan/networking-on-aws-amazon-virtual-private-cloud-vpc-4phd) to read how to create VPC. But change the VPC configuration — the number of Availability Zone (AZ) is 2, public subnets are 2 and private subnets also 2.

![Subnets in VPC](https://cdn-images-1.medium.com/max/2000/1*lnwQQ8QvDySsaDlF6QG4DQ.jpeg)

Create a security group in VPC — security group for the load balancer and Fargate. But the security group for the load balancer and Fargate is different. Security group for Fargate associate to security group for the load balancer and filled with security group for load balancer — **secg-for-elb**.

![Security Group for Fargate](https://cdn-images-1.medium.com/max/2014/1*F-okK2fcDs9JWqikXCpoPg.jpeg)

![Security Group for Load Balancer](https://cdn-images-1.medium.com/max/2004/1*DLkw19jvgp7VslYaOWl55g.jpeg)

Go to Amazon ECS and click create cluster.

![](https://cdn-images-1.medium.com/max/2638/1*vWhmsf1UTMoM_MZUCPLE5A.jpeg)

You can choose networking only on **AWS Fargate** or use Amazon EC2 Linux for cluster template. Choose Fargate and next step.

![](https://cdn-images-1.medium.com/max/2078/1*8v-811naTerPWPNyYBemxw.jpeg)

Fill in the cluster name, for VPC is not check-listed and click **Create**.

![](https://cdn-images-1.medium.com/max/2000/1*sDEtIrt4ENJQ3erg8P9uDA.jpeg)

Wait until ECS cluster successfully created and can click **View cluster**.

![](https://cdn-images-1.medium.com/max/2000/1*wj04vzbF2UHdSc1cM6GhBw.jpeg)

![ECS cluster](https://cdn-images-1.medium.com/max/2178/1*mF5u3GgeHK7Z5dkaRUvyTA.jpeg)

After creating the ECS cluster, click task definitions and click **Create new Task Definition**. Task definition can have many versions. Choose Fargate because when creating an ECS cluster, choose Fargate.

![](https://cdn-images-1.medium.com/max/2162/1*2f4oRVr00oSFy3zNXmaa5Q.jpeg)

![](https://cdn-images-1.medium.com/max/2446/1*5_9cMRuwCv1VbAQ-5Hc9sg.jpeg)

Fill task definition name, task role, network mode, OS and task execution role. Choose task memory GB and task CPU.

![](https://cdn-images-1.medium.com/max/2458/1*x2QjtID-sjf4TH_itIG1bA.jpeg)

![Task definition settings.](https://cdn-images-1.medium.com/max/2000/1*_l9mED4oJvT_LXV-fE5WBg.jpeg)

Use Amazon ECR for add container. Click **add container**. Fill container name, image from Amazon ECR and port mappings. Click **add**.

![](https://cdn-images-1.medium.com/max/2000/1*SunmD1sO4IcWtqKJzKdA8w.jpeg)

![](https://cdn-images-1.medium.com/max/2000/1*3w3jQmP6XuStJTlmHmsJ9w.jpeg)

![](https://cdn-images-1.medium.com/max/2000/1*KeAvkkLiOioChNjhWoy7TA.jpeg)

![After add container (use Amazon ECR)](https://cdn-images-1.medium.com/max/2000/1*Ne3J5RLZJP9LdQvpxZo4bA.jpeg)

Click **create** for create task definition. When task definition are successfully created, click **view task definition**.

![](https://cdn-images-1.medium.com/max/2000/1*VFm4SPJNHlV6nCHSGi547A.jpeg)

![](https://cdn-images-1.medium.com/max/2644/1*1EavJU3LydA8cyaWwU5ITw.jpeg)

When task definition is ready, can click **Services** and click **Create**.

![](https://cdn-images-1.medium.com/max/2314/1*6yyhtyGOt8Giw3XLqOkYag.jpeg)

Click FARGATE for launch type, OS surely Linux. You can see the task definition. The task definition has revision (version). Fill service name. Choose rolling update for the deployment type. Deployment type also can use AWS CodeDeploy service and click **next step** for Step 2.

![](https://cdn-images-1.medium.com/max/2300/1*o7LeDs4fvFLESnBGFk3IIA.jpeg)

![](https://cdn-images-1.medium.com/max/2000/1*v16OTWXpzCpg1HoPJPxp6g.jpeg)

![](https://cdn-images-1.medium.com/max/2000/1*8vqCI5PXKKQfeP-jQ7xjhw.jpeg)

![](https://cdn-images-1.medium.com/max/2000/1*X2rNE6EovZASUwpz7bJBLg.jpeg)

Choose VPC and subnet that have already been created. Click **edit** on Security groups. When configuring security groups, select existing security groups for the networking. Choose the **security group for Fargate** and click **Save**.

![](https://cdn-images-1.medium.com/max/2000/1*rKMC4lvlwKg52q7bPAQNVw.jpeg)

![](https://cdn-images-1.medium.com/max/2000/1*HCpl4G9hxXenoqT4vH0PJQ.jpeg)

![Security group for Fargate.](https://cdn-images-1.medium.com/max/2000/1*RtId4Qc9s7TYeHJQGkU-eg.jpeg)

Enable IP public. If want [enable](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/load-balancer-types.html) [load balancer](https://aws.amazon.com/elasticloadbalancing), choose **ALB (application load balancer)** or NLB (network load balancer).

![](https://cdn-images-1.medium.com/max/2000/1*NN_3WBxyAlY-hEbYn_8_ew.jpeg)

If load balancer is not available, create load balancer with click **EC2 Console** and redirect to new tab. Click **create load balancers** and then choose **Application Load Balancers**.

![](https://cdn-images-1.medium.com/max/2000/1*YaaWtT7plE1R8ymvufQD-g.jpeg)

Fill in the load balancer name. Choose internet-facing scheme and IPv4 address type.

![](https://cdn-images-1.medium.com/max/2000/1*gE35dT7n5ec6Ip8T4xARpA.jpeg)

Choose VPC that has already been created before. Choose and checklist 2 public subnets for mappings.

![](https://cdn-images-1.medium.com/max/2128/1*dO2fPtmUxd2UvyHpaFvTKQ.jpeg)

For security groups, choose security group for load balancer because inbound rules are open to anyone.

![](https://cdn-images-1.medium.com/max/2000/1*kDDMnkXNrcxkuTR-dSP9PQ.jpeg)

The target group is not available then click **create target group**.

![](https://cdn-images-1.medium.com/max/2000/1*0OTgCpUlkoxSwP-RabHDcg.jpeg)

Target group in Fargate instead choose IP address because don’t have instances.

![](https://cdn-images-1.medium.com/max/2000/1*kBRfe-Vd5Iz0oezrJ8JvIw.jpeg)

Fill in the target group name and choose VPC that has already been created.

![](https://cdn-images-1.medium.com/max/2000/1*LCDTxh3-E9hep9nCIwh_yg.jpeg)

Choose again VPC, fill port 8080 and click **Create target group**.

![](https://cdn-images-1.medium.com/max/2000/1*rnV4GLNp9gVnB52tbvKi9w.jpeg)

After creating the target group, click the refresh icon to the target group is now available. Click **create load balancer**.

![](https://cdn-images-1.medium.com/max/2120/1*SXOu4jhioXtAe046UE4-Zg.jpeg)

![](https://cdn-images-1.medium.com/max/2150/1*-5w1KeR3qov38SMczuXoBQ.jpeg)

After the load balancer is created, click the refresh icon for the load balancer is now available. Click **Add to load balancer**.

![](https://cdn-images-1.medium.com/max/2000/1*rTylgj6wa2SPEEVwtDq2fg.jpeg)

For the target group name, choose the target group that has already been created.

![](https://cdn-images-1.medium.com/max/2000/1*44zPhXMDr8F0d89bI8YwhA.jpeg)

Configure auto scaling. The minimum number of tasks and desired number of tasks — 1 and maximum — 2. Create a policy for auto scaling based on average CPU utilization with a value of 80.

![](https://cdn-images-1.medium.com/max/2000/1*ABZhE7GbPZM_OHUHosMC4g.jpeg)

![](https://cdn-images-1.medium.com/max/2000/1*mBP24asVp8DRUT2cyK7png.jpeg)

After review the configure service, click **create service**.

![](https://cdn-images-1.medium.com/max/2000/1*o2RNAndR9bn0kbjAQTVkpQ.jpeg)

Then go to the ECS cluster that has one service — **deploywebapp**. Click the service name and click Tasks to see running or not. Wait until the status is running. The ECS tasks are running using Fargate. When ECS tasks are now running, means the load balancer also now running.

![THE ACTIVE SERVICE](https://cdn-images-1.medium.com/max/2152/1*6py-IoLY5u2Sk-ukyBo0nQ.jpeg)

![Tasks](https://cdn-images-1.medium.com/max/2000/1*kQE1TjjgiINbI1mgcNk-gQ.jpeg)

Go to EC2 then to Load Balancers. Copy DNS name to new tab.

![Load Balancers](https://cdn-images-1.medium.com/max/2322/1*h0H83BHBKDuKG5AlUEvh6A.jpeg)

The website is now running and successful.

![](https://cdn-images-1.medium.com/max/2710/1*TNF31j1hLUW63MXC5kxdXQ.jpeg)

Thank you very much ;)