DEV Community

Revathi Joshi for AWS Community Builders

Posted on

Create Task Definition in ECS and Application Load balancer (ALB) for the Task which is to be run on Fargate Cluster

In this 2nd part of 3-part series on ECR Repository, I am going to show you how to create

  • (1) Task Definition in ECS that references the image in the repository - nginx and
  • (2) Application Load balancer (ALB) for the Task which is to be run on Fargate Cluster.

As a reference, please read my 1st article - How to create a Docker Image with Nginx from an EC2 Instance and Push to ECR Repository

Let’s get started!

Please visit my GitHub Repository for Docker/ECS/ECR articles on various topics being updated on constant basis.

Objectives:

1. Create role

2. Create Task definition

3. Run Task

4. Create an Application Load Balancer (ALB) and Target groups

Pre-requisites:

  • AWS user account with admin access, not a root account.
  • AWS CLI.

Resources Used:

Amazon ECS task definitions

Application Load Balancer

Steps for implementation to this project:

1. Create role

  • Roles, Create role, Use cases - EC2, Next, Search for AmazonECSTaskExecutionRolePolicy, AmazonECSTaskExecutionRole

  • Create role

Image description

  • click AmazonECSTaskExecutionRole, Trust relationships, Edit Trust policy, delete the default code and copy and paste this policy


{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "",
            "Effect": "Allow",
            "Principal": {
                "Service": "ecs-tasks.amazonaws.com"
            },
            "Action": "sts:AssumeRole"
        }
    ]
}


Enter fullscreen mode Exit fullscreen mode
  • Update policy

Image description

2. Create Task definition

  • On the ECS Dashboard, Task definitions, Create new task definition with JSON, delete the code, copy and paste this code

  • Create



{
    "executionRoleArn": "arn:aws:iam::YOUR_ACCOUNT_NUMBER:role/AmazonECSTaskExecutionRole",
    "containerDefinitions": [
        {
            "name": "my-website",
            "image": "nginx",
            "essential": true,
            "portMappings": [
                {
                    "hostPort": 80,
                    "protocol": "tcp",
                    "containerPort": 80
                }
            ]
        }
    ],
    "requiresCompatibilities": [
        "FARGATE"
    ],
    "networkMode": "awsvpc",
    "cpu": "256",
    "memory": "512",
    "family": "ecs-family"
}


Enter fullscreen mode Exit fullscreen mode
  • Task definition - ecs-family

Image description

  • Container name - my-website

Image description

3. Run Task

  • Click on task definition - ecs-family, Run task

Image description

  • take these values and run task

  • Existing cluster - my-ecs-cluster, click Launch-type

  • PHOTO-1

Image description

  • PHOTO-2

Image description

  • PHOTO-3

Image description

  • PHOTO-4

Image description

  • Create

  • Status - Task running

Image description

4. Create an Application Load Balancer (ALB) and Target groups

Application Load Balancer

  • On the EC2 Dashboard, Load Balancers, Create load balancer, Application load balancer, Create, my-ecs-alb, Internet-facing, default vpc, Choose 2 public subnets in different AZs - us-east-1a, us-east-1b, security group - Web-access

  • Listener - HTTP:80, Create target group, target type = IP addresses, target-group-1, protocol HTTP 80, take default values, Next, Create target group

  • Back on Load Balancer, refresh to select target-group-1

  • Add listener - HTTP:8080, Create target group, target type = IP addresses, target-group-2, protocol HTTP 8080, take default values, Next, Create target group

  • Back on Load Balancer, refresh to select target-group-2

  • Create load balancer

  • Update security group - Web-access to allow inbound on 80 and 8080

Image description

Target Groups

Image description

What we have done so far

  • We have successfully created
  • (1) Task Definition in ECS that references the image in the repository - nginx and
  • (2) Application Load balancer (ALB) for the Task which is to be run on Fargate Cluster.

Top comments (2)

Collapse
 
alejos4n profile image
Alejandro Sánchez • Edited

I first had to create the cluster, then create and execute the task. Although being me, I think I did something wrong and that's why I reversed the order.

Collapse
 
awsmine profile image
Revathi Joshi

Read all the 3parts