6-part series - (2) Create Task Definition in ECS and Application Load balancer for the Task to be run on Fargate Cluster
Tags: docker, ecr, ecs, ec2
Published: 2023-02-14 00:46:29

In this **6-part series on configuring a CI/CD pipeline using Customized Docker image on an Apache Web Server, Application Load Balancer, ECS, ECR, CodeCommit, CodeBuild, CodeDeply services** -

In the **2nd article**, We will create Task Definition in ECS, that references the customized image **`my_apache_image`** in the repository - and Application Load balancer (ALB) for the Task which is to be run on Fargate Cluster.

**1st article**
- As a reference, please read my **[1st article - 6-part series - (1) Deploy a Custom Docker Image with Centos on an Apache web server and Save it to AWS ECR Repository](https://dev.to/aws-builders/6-part-series-1-deploy-a-custom-docker-image-with-centos-on-an-apache-web-server-and-save-it-to-aws-ecr-repository-186)**

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

Please visit my **[GitHub Repository for CI-CD/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.** 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

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

- Create role


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ns55693lqb5jtrjbjwqw.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/x5f220wvisrs2fuptdol.png)


## 2. Create Task definition

- **`on Elastic Container Service in the Containers section, Task definitions, Create new Task Definition, my_task, Under Container details, my_container`** 

**From my 1st article, Step-7, Copy image URI -** 
**`<YOUR ACCOUNT NUMBER>.dkr.ecr.us-east-1.amazonaws.com/my-ecr:latest`**


- **`Next, Fargate, Under Task size, choose Task memory as 0.5 GB and Task CPU as 0.25 vCPU, Task execution role - ecsTaskExecutionRole is selected automatically, Next`**


- Create


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


- Task definition - my_task


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


- Container name - my_container


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



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

- **`On EC2 Dashboard, Load Balancers, Create load balancer
Application load Balancer, Create, my-alb, internet-facing, default vpc, 2 public subnets - us-east-1a and us-east-1b, 
my_sg (Security group of EC2 Instance)`**


- **`create target group, instances, my-tg, Next, Available instances, DO NOT SELECT EC2 INSTANCE, Create target group`**

- **`On Load balancer page, refresh, select my-tg`**


- Create Load Balancer


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


**Target Groups**


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


# What we have done so far

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