Author Image Chinthaka Dinadasa

How to Create Task Definitions For ECS Deployment

An ECS task definition is a blueprint or configuration file that describes how to run a containerized application within Amazon Elastic Container Service (ECS).

It defines various parameters such as the Docker image to use, the resources allocated to the task, networking settings, environment variables, and other container-specific configurations.

Task definitions play a crucial role in ECS deployments as they provide the instructions for launching and managing containers.

They define the components of a task, which can include one or more containers that work together to form a functional application.

Configuring Task Definition

First, navigate to task definition under the ECS dashboard, then give a unique name for the definition family name.

After that create containers that we need with this definition,

Here we only need a single container that runs with the docker image we created on private ECR repository.

Copy the image URI from the ECR dashboard for the correct repository and let’s create the definition as shown below,

Here we have to define the port mappings for the container, Since we have a spring boot application that runs on a default 8080 port, we have to open it with the port mappings section,

This port mapping will be used by external parties (Eg:- Load balancer) to access the fargate instances running on a private subnet.

Container image details under task definition

Container image details under task definition

Private Registry access

Here we are using a private ECR registry to store the necessary docker images, But we can keep the private registry tick de-active, and ensure the ECR access through the task execution role that we are setting in the next phase in this task definition.

Defining Environmental Variables

The spring boot application that we are going to deploy with this infrastructure has environment variables that we have defined under application.properties for the DEV profile.

spring.datasource.url=jdbc:mysql://${AWS_RDS_DATABASE_HOST}:3306/${AWS_RDS_DATABASE}?useSSL=false&allowPublicKeyRetrieval=true&autoReconnect=true
spring.datasource.username=${AWS_RDS_DATABASE_USERNAME}
spring.datasource.password=${AWS_RDS_DATABASE_PASSWORD}
spring.jpa.hibernate.ddl-auto=update

Then define the necessary environment variables with the database configurations from the RDS database we created earlier.

Environment variables under ECS Task definition

Environment variables under ECS Task definition

Keep other optional parameters as default values and proceed with the next phase.

Environment Configurations

Here we can choose the task size including memory and CPU allocation, with task roles that will be used by container setup.

ECS task roles allow the containers in your task to assume an IAM role to call AWS APIs without having to use AWS Credentials inside the containers. That means the application inside the container can access other AWS services like sending a notification to Amazon SNS or accessing an S3 bucket.

ECS task execution roles grant the ECS agents permission to make AWS API calls who are responsible for managing the tasks in the cluster. That means the task is able to send container logs to CloudWatch or pull a container image from Amazon ECR.

Here we only have a requirement to pull a container image from Amazon ECR, hence setting the ECS task execution role will be enough.

Attaching Storage With ECS - Optional

By default, every ECS task contains 20GB of space, But we can extend it up to 200 GB. In addition to that we have the freedom to attach an EFS volume as well.

Memory and CPU allocation with Task roles configurations for ECS task definition

Memory and CPU allocation with Task roles configurations for ECS task definition

All done now we can proceed with creating the task definition since we have configured all the necessary minimal configurations.

Task definition creation successfully completed

Task definition creation successfully completed

Conclusion

We are done with setting up the task definitions for our ECS fargate application architecture in this article series.

Now we can focus on the next step which focuses on deploying the task as a service under the cluster we created on Amazon ECS in our next article.