Running a new task using Terraform without a service in AWS ECS
In Amazon Elastic Container Service (ECS), tasks are the fundamental unit of work that can be executed on a cluster. While services in ECS are used for long-running tasks, there are scenarios where you might need to run a task without associating it with a service. In this blog post, we’ll explore how to use Terraform to define and run a standalone ECS task without a service.
First thing first
Before proceeding, ensure you have the following:
- An AWS account with permissions to create ECS resources.
- Terraform installed on your local machine.
- Basic knowledge of ECS task definitions and Terraform.
Defining the ECS Task
To define an ECS task without a service, we’ll first create a task definition. This task definition will specify the container image, resource requirements, and any other configuration needed for the task.
resource "aws_ecs_task_definition" "task_definition" {
family = "my-task"
network_mode = "awsvpc"
cpu = 256
memory = 512
requires_compatibilities = ["FARGATE"]
container_definitions = jsonencode([
{
name = "my-container"
image = "nginx:latest"
cpu = 256
memory = 512
essential = true
}
])
}
In this example, we define a task named “my-task” using the nginx container image running on AWS Fargate.
Running the ECS Task
To run the ECS task, we’ll use a null resource with a local exec provisioner in Terraform. This provisioner will execute the aws ecs run-task
command to start the task.
resource "null_resource" "migration_task_run" {
depends_on = [
aws_ecs_task_definition.task_definition // to make sure that you run task only after creating the task definition
]
provisioner "local-exec" {
command = <<EOF
aws ecs run-task \
--cluster <<cluster_name>> \
--task-definition <<task_definition_name>> \
--count 1 --launch-type FARGATE \
--network-configuration '{ // This is required if you have chosen awsvpc in network config for your task definition. Else, this can be ignored
"awsvpcConfiguration": {
"assignPublicIp":"DISABLED",
"securityGroups": ["<<security_group>>"],
"subnets": ["<<your subnets>>"]
}
}'
EOF
}
}
Use cases:
Running a standalone ECS task without a service using Terraform provides a flexible way to execute short-lived tasks, such as batch processing or one-time operations. By defining the task with Terraform and using the aws ecs run-task
command, you can easily manage and execute ECS tasks as part of your infrastructure automation workflows.
- Automated Deployment Pipeline: Incorporate the execution of ECS tasks into your CI/CD pipeline. For example, after building and pushing a Docker image, use Terraform to update the ECS task definition and then automatically run the updated task.
- Scheduled Tasks: Schedule tasks to run at specific times or intervals using a cron job or another scheduling mechanism. Terraform can be used to define and manage these scheduled tasks, ensuring they are executed as intended.
- One-off Tasks: Run ad-hoc tasks that are not part of your regular ECS services. These tasks might be used for maintenance, data processing, or other temporary operations.
- Data base migration: To run DB migration jobs as part of your CI/CD pipeline.
- Environment Initialization: Use Terraform to initialize your ECS environment by running tasks that perform setup operations, such as database migrations, configuration loading, or resource provisioning.