I have an AWS cluster which is running multiple tasks, each of these being a revision of the previous one as these revisions are identical except for a path change the docker command passed to them. I wanted to automate this process using Terraform. I want to create a new revision of an existing task, and run a new task in an existing cluster which picks this latest task revision. But right now I am only able to create a new task definition and a new ECS cluster everytime I am running the script. I am not sure what I am missing.
Here are the relevant sections of my script
# main.tf
# Provider configuration
provider "aws" {
region = "ap-northeast-1"
}
# Read input.yaml file
data "local_file" "input" {
filename = "input.yaml"
}
# Parse input.yaml file
locals {
input_data = yamldecode(data.local_file.input.content)
}
# Retrieve existing ECS task definition
data "aws_ecs_task_definition" "existing_task" {
task_definition = "modbus-simulator-fargate-task"
}
# Create new ECS task definition based on existing one with modifications
resource "aws_ecs_task_definition" "modbus_simulator" {
family = data.aws_ecs_task_definition.existing_task.family
task_role_arn = data.aws_ecs_task_definition.existing_task.task_role_arn
execution_role_arn = data.aws_ecs_task_definition.existing_task.execution_role_arn
network_mode = data.aws_ecs_task_definition.existing_task.network_mode
requires_compatibilities = ["FARGATE"] # Add FARGATE here
cpu = 1024 # Specify CPU here
memory = 3072 # Specify memory here
container_definitions = jsonencode([
{
"name": "client-server-simulator",
"image": "337039605624.dkr.ecr.ap-northeast-1.amazonaws.com/client-server-simulator:26-june-2024",
"cpu": 0,
"portMappings": [
{
"name": "client-server-simulator-80-tcp",
"containerPort": 80,
"hostPort": 80,
"protocol": "tcp",
"appProtocol": "http"
}
],
"essential": true,
"command": [
"--s3-path",
local.input_data[0].s3_path,
"--modbus-ip",
local.input_data[0].modbus_ip,
"--modbus-port",
local.input_data[0].modbus_port,
"--interval",
local.input_data[0].interval,
"--edge-id",
local.input_data[0].edge_id,
"--location",
local.input_data[0].location,
"--company",
local.input_data[0].company
],
"environment": [],
"mountPoints": [],
"volumesFrom": [],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/modbus-simulator-fargate-task",
"awslogs-create-group": "true",
"awslogs-region": "ap-northeast-1",
"awslogs-stream-prefix": "ecs"
}
},
"systemControls": []
}
])
}
# Update ECS service to use the new task definition revision
resource "aws_ecs_service" "modbus_simulator_service" {
name = "modbus-simulator-service"
cluster = "modbus-simulator-cluster" # use the existing cluster
task_definition = aws_ecs_task_definition.modbus_simulator.arn
desired_count = 1
# Update only if there's a change in the task definition
lifecycle {
ignore_changes = [task_definition]
}
}
I have tried going through the examples provided in the repository.