How to create Task Definitions and run it as a service in ECS

Learn&Grow
4 min readOct 3, 2023

--

In this page, we would focus on the creation of ECS Task Definition and Services. If you have not created the ECS cluster, first create it with the help of this page.

Once you have ECS cluster created, follow the below steps.

Create ECS Task Definitions and Service

  • A task definition is a blueprint for your application. It is a text file in JSON format that describes the parameters and one or more containers that form your application.
  • If you want to know more about task definition and its parameters, please visit this official document.

Infrastructure Requirements:

  • Choose the Instance Type, OS & Arch that you chose while creating ECS.
  • Network mode is chosen based on your requirement. It is crucial to understand the network mode of ECS to ensure the deployment of the containers. Please refer this documentation.
  • I chose default, as I don’t have any specific requirements like nginx proxy or containers communicating with each other in a private network, etc.

Based on your requirement, CPU and memory must be decided.

Task Execution Role that I have given is the basic ecsTaskExecutionRole. But if you think the container makes API requests/if it is required , then create a new role based on the required privileges. Please note, do not create administrative privileged roles to just deploy an application, though it may take some time for you to conclude the required privileges. Ensure least privilege is practiced.

Container Details:

Image URI : Copy the ECR URI

HostPort: Mention the port in which you need to access the web application.

Container Port: The port which the application is listening inside the container.

After you create a task definition, you can run the task definition as a task or a service.

Note: You can add health check, logging, Resource Limits, Timeouts, etc are all additional settings that you can add to the Task Definition creation.

If required, to add a separate data storage for containers in this task, add a storage volume in the storage section.

Successfully created a Task definition, it must have an active status.

Create service

The service scheduler ensures that the scheduling strategy that you specify is followed and reschedules tasks when a task fails.

You may choose based on your requirement, I have chosen the Launch Type as EC2 which would look for the existing EC2 instance to which the task definition would be applied

Choose the task definition and revision based on the task definition creation.

  • Replica — The scheduler places and maintains the number of tasks that you specify across your cluster. By default the scheduler spreads tasks across Availability Zones.
  • Daemon — The daemon scheduling strategy deploys exactly one task on each active container instance that meets all of the task placement constraints that you specify in your cluster.

Finally, you have created a task definition which will fetch the image from ECR , deploy it as a container in the EC2 machine spawn up in the ECS cluster and managed by the ECS.

--

--

Learn&Grow

Technology Architect with profound understanding on the Multi-Cloud Platform engineering with a strong DevOps Solutioning Experience.