DEV Community

Budiono Santoso for AWS Community Builders

Posted on • Edited on

Containers on AWS — Amazon Elastic Container Service (ECS) on AWS Fargate

Architecture

Hello everyone, I want to write an Amazon ECS tutorial. Amazon 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. 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 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

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

Security Group for Load Balancer

Go to Amazon ECS and click create cluster.

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

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

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

ECS cluster

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.

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

Task definition settings.

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

After add container (use Amazon ECR)

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

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

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.

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.

Security group for Fargate.

Enable IP public. If want enable load balancer, choose ALB (application load balancer) or NLB (network load balancer).

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.

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

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

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

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

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

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

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

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

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

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

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.

After review the configure service, click create service.

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

Tasks

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

Load Balancers

The website is now running and successful.

Thank you very much ;)

Top comments (0)