Create Task Definition in ECS and Application Load balancer (ALB) for the Task which is to be run on Fargate Cluster
Revathi Joshi
Posted on February 9, 2023
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:
Steps for implementation to this project:
1. Create role
Roles, Create role, Use cases - EC2, Next, Search for AmazonECSTaskExecutionRolePolicy, AmazonECSTaskExecutionRole
Create role
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
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
- Container name - my-website
3. Run Task
Click on task definition - ecs-family, Run task
take these values and run task
Existing cluster - my-ecs-cluster, click Launch-type
PHOTO-1
- PHOTO-2
- PHOTO-3
- PHOTO-4
Create
Status - Task running
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
Target Groups
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.
Posted on February 9, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.