Create Task Definition in ECS and Application Load balancer (ALB) for the Task which is to be run on Fargate Cluster
Tags: fargate, ecr, ecs, loadbalancer
Published: 2023-02-09 23:00:23

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](https://dev.to/aws-builders/how-to-create-a-docker-image-with-nginx-from-an-ec2-instance-and-push-to-ecr-repository-nkb)**

_**Let’s get started!**_


Please visit my **[GitHub Repository for Docker/ECS/ECR articles](https://github.com/awsmine/Docker_projects)** 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](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definitions.html)**

**[Application Load Balancer](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/introduction.html)**

# Steps for implementation to this project:

## 1. Create role

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

- Create role


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nrd1uo6vuvhrh5gsiawf.png)


- **`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"
		}
	]
}
```

- Update policy


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a85di1caxs7xfqkzw6vd.png)


## 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"
}
```

- Task definition - ecs-family


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4h8qan5je7fkiebseoom.png)


- Container name - my-website


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d83mf5i0o86xu4q82usi.png)


## 3. Run Task


- **`Click on task definition - ecs-family, Run task`**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/usrgr1vxbfdjl1cjahq3.png)


- **take these values and run task**

- **`Existing cluster - my-ecs-cluster, click Launch-type`**

- **PHOTO-1**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m142y0q1zgeb6hqj5s4e.png)

- **PHOTO-2**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i67y0c2ksovttk78ub64.png)


- **PHOTO-3**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bwufzyk38gzv4bi6yi9n.png)


- **PHOTO-4**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k49uleukqye4db40vca3.png)


- Create


- Status - Task running


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bi5c7uu1j564y1y8txu6.png)


## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ktbcqikp1522ajncrjsf.png)


**Target Groups**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v23m2x78a6t61eauugsz.png)


# 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.