Containers on AWS — Amazon Elastic Container Service (ECS) on Amazon EC2
Tags: aws, cloud, ecs, containers
Published: 2023-03-13 06:40:55

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

Hello everyone. When I try **Amazon ECS** on **Amazon EC2**, I get challenged on how to use a load balancer and auto-scaling. Finally, I can deploy a web application to Amazon ECS on Amazon EC2. If you want to read about Amazon ECS on AWS Fargate, [you can see the link](https://dev.to/budionosan/containers-on-aws-amazon-elastic-container-service-ecs-on-aws-fargate-91b).

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 I 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*vDBn5TwL4bQLayYgksTIXQ.jpeg)

Create 2 security groups for VPC — security group for the load balancer and EC2. But the security group for the load balancer and EC2 is different. The security group for EC2 connects to the security group for the load balancer.

![SECURITY GROUP FOR LOAD BALANCER](https://cdn-images-1.medium.com/max/2086/1*rbISH_n7kAlWBMweLKexyA.jpeg)

![SECURITY GROUP FOR LOAD BALANCER](https://cdn-images-1.medium.com/max/2374/1*afl_scewhhruooK6WSfkNw.jpeg)

![SECURITY GROUP FOR EC2](https://cdn-images-1.medium.com/max/2054/1*qvDNtbUxMFWI1Vrv7nnKKA.jpeg)

![SECURITY GROUP FOR EC2](https://cdn-images-1.medium.com/max/2404/1*TZ1sBq-Kl3cZftU4cKn6dA.jpeg)

When security group for EC2, the source associated with security group for load balancer (secg-for-elb with ID sg-…58ec1)

![SECURITY GROUP IN VPC](https://cdn-images-1.medium.com/max/2086/1*I6cz_V6uw2l9oZQpR3p7GA.jpeg)

After creating VPC and security group in VPC, go to Amazon ECS and create an ECS cluster. Choose **EC2 Linux + Networking**.

![](https://cdn-images-1.medium.com/max/2082/1*F9NIXCl-GmgLAZvvtvGrFg.jpeg)

Fill the ECS cluster and choose on-demand instance, choose EC2 instance type or also can fill the EC2 instance type with click **manually enter desired instance type**.

**NOTE:** EC2 instance type changed from c4.large to t2.large.

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

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

For networking, use VPC that already created. Then, choose 2 public subnets for the subnets. Enable auto-assign public IP. For security group, use security group for EC2.

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

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

Create an IAM role for the container instance. (Optional) Enable container insights and see insight results in Amazon CloudWatch. And click Create. Waiting until can click View Cluster. The ECS cluster on EC2 was created with the CloudFormation stack.

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

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

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

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

After creating the ECS cluster, create a task definition for add a container from Amazon ECR private. Choose EC2 for task definition.

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

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

Fill in the task definition name. Create IAM role for ECS task execution.

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

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

Click Add container.

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

Fill in the container name. For image, you can see my article about Amazon ECR with [the link](https://dev.to/budionosan/containers-on-aws-amazon-elastic-container-registry-ecr-and-aws-app-runner-33eo).

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

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

When after add container, can see the container name **webapp** is added. And click **Create task**.

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

After creating the ECS task definition, create an ECS service on EC2. Choose task definition and version (revision) then choose cluster.

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

Fill in the service name. For service type, can read different between replica and daemon with [the link](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs_services.html).

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

For load balancer, choose Application Load Balancer. If you don’t have ALB, you must create ALB in EC2. Click the **EC2 console** and automatically open to load balancer page.

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

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

[**Load balancer**](https://aws.amazon.com/elasticloadbalancing/) in EC2 has 3 types:

 1. **Application Load Balancer** — for applications with HTTP and HTTPS and target to the application like EC2 instances, IP address, Lambda function, and Network Load Balancer (NLB). ALB works in AWS Region, local zone, and AWS Outposts.

 2. **Network Load Balancer** — same as ALB but needs [AWS PrivateLink](https://aws.amazon.com/privatelink/). NLB works in AWS Region only.

 3. **Gateway Load Balancer**.

Choose Application Load Balancer to create a load balancer.

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

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

Because use an Application Load Balancer, I screenshot how ALB work for the application.

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

Fill in the load balancer name, choose internet-facing for target group.

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

Choose VPC that has already been created. Mappings can use a public subnet. Mappings must require two AZ and one subnet per AZ.

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

For the security group on ALB, can choose a security group for ALB that has already been created.

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

For listeners, fill 8080 for the port. Click create target group. The target group for connecting to EC2 instances. If the target group is already created, click refresh and show the target group.

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

Choose instances because targeted to EC2 instances that have already been created. Fill in the target group name, choose VPC and click Next.

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

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

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

When instances are available, checklist all EC2 instances. Fill 8080 in the port and click include as pending below.

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

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

You can see review targets before click create target group. The EC2 instances has health status is pending. Click create target group.

![](https://cdn-images-1.medium.com/max/2474/1*3M9sKJG6p--K-p7I7OcxBw.jpeg)

After creating a target group, click the target group name and can see if the healthy status is unused.

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

![](https://cdn-images-1.medium.com/max/2078/1*9eSxSLcB0th8zUfwHEJdpw.jpeg)

Back to Listeners. Click refresh again and choose the target group that has already been created.

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

You can see the summary and click create the load balancer. The application load balancer is successfully created. View load balancer. Waiting for application load balancer until status is active.

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

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

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

When the application load balancer is active, check again the target group already healthy or unhealthy. The results are healthy.

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

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

![BACK TO THIS](https://cdn-images-1.medium.com/max/2000/1*UD7aHcHORCFLTv2gHvqgYQ.jpeg)

After creating a load balancer, click refresh again and choose the load balancer that has already been created. Also select the target group.

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

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

Create [auto scaling](https://aws.amazon.com/ec2/autoscaling/). Auto scaling work for add or remove EC2 instances that match needs.

![](https://cdn-images-1.medium.com/max/2282/1*qL2TR_-dXHP4TJw5qaY84w.jpeg)

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

Create an auto scaling policy. Choose scaling policy type with target tracking. The ECS service metric is based on average CPU utilization. If all settings are ready, can click **Create service**. After services have already been created, click **View service**.

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

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

The services already active and running.

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

Click details. Details are explaining the services have load balancer and also target group with container name and port. Click also events. Events are explaining during services are process creating.

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

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

Check tasks are running or not.

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

Then go to the load balancer in EC2, click DNS name, copy and paste to a new tab.

![LOAD BALANCER IN EC2](https://cdn-images-1.medium.com/max/2066/1*iSKnKZLERTM_2rmNUKfX0g.jpeg)

Aaaaaaa, load balancer is successful and can running.

![](https://cdn-images-1.medium.com/max/2698/1*329q4Uq6VUO0cmI0ACafFw.jpeg)

When creating an ECS cluster, I want 2 EC2 instances. Go to EC2. I want to test auto scaling is running or not. When I try to terminate one EC2 instance, the ECS cluster now has one EC2 instance only. Waiting a few minutes and new EC2 instances are created.

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

Thank you very much that already read this tutorial! :)