Resource: aws_ecs_service

Provides an ECS service - effectively a task that is expected to run until an error occurs or a user terminates it (typically a webserver or a database).

See ECS Services section in AWS developer guide.

Example Usage

resource "aws_ecs_service" "mongo" {
  name            = "mongodb"
  cluster         = aws_ecs_cluster.foo.id
  task_definition = aws_ecs_task_definition.mongo.arn
  desired_count   = 3
  iam_role        = aws_iam_role.foo.arn
  depends_on      = [aws_iam_role_policy.foo]

  ordered_placement_strategy {
    type  = "binpack"
    field = "cpu"
  }

  load_balancer {
    target_group_arn = aws_lb_target_group.foo.arn
    container_name   = "mongo"
    container_port   = 8080
  }

  placement_constraints {
    type       = "memberOf"
    expression = "attribute:ecs.availability-zone in [us-west-2a, us-west-2b]"
  }
}

Ignoring Changes to Desired Count

You can utilize the generic Terraform resource lifecycle configuration block with ignore_changes to create an ECS service with an initial count of running instances, then ignore any changes to that count caused externally (e.g., Application Autoscaling).

resource "aws_ecs_service" "example" {
  # ... other configurations ...

  # Example: Create service with 2 instances to start
  desired_count = 2

  # Optional: Allow external changes without Terraform plan difference
  lifecycle {
    ignore_changes = [desired_count]
  }
}

Daemon Scheduling Strategy

resource "aws_ecs_service" "bar" {
  name                = "bar"
  cluster             = aws_ecs_cluster.foo.id
  task_definition     = aws_ecs_task_definition.bar.arn
  scheduling_strategy = "DAEMON"
}

CloudWatch Deployment Alarms

resource "aws_ecs_service" "example" {
  name    = "example"
  cluster = aws_ecs_cluster.example.id

  alarms {
    enable   = true
    rollback = true
    alarm_names = [
      aws_cloudwatch_metric_alarm.example.alarm_name
    ]
  }
}

External Deployment Controller

resource "aws_ecs_service" "example" {
  name    = "example"
  cluster = aws_ecs_cluster.example.id

  deployment_controller {
    type = "EXTERNAL"
  }
}

Redeploy Service On Every Apply

The key used with triggers is arbitrary.

resource "aws_ecs_service" "example" {
  # ... other configurations ...

  force_new_deployment = true

  triggers = {
    redeployment = plantimestamp()
  }
}

Argument Reference

The following arguments are required:

The following arguments are optional:

alarms

The alarms configuration block supports the following:

capacity_provider_strategy

The capacity_provider_strategy configuration block supports the following:

deployment_circuit_breaker

The deployment_circuit_breaker configuration block supports the following:

deployment_controller

The deployment_controller configuration block supports the following:

load_balancer

load_balancer supports the following:

network_configuration

network_configuration support the following:

For more information, see Task Networking

ordered_placement_strategy

ordered_placement_strategy supports the following:

placement_constraints

placement_constraints support the following:

service_registries

service_registries support the following:

service_connect_configuration

service_connect_configuration supports the following:

log_configuration

log_configuration supports the following:

secret_option

secret_option supports the following:

service

service supports the following:

timeout

timeout supports the following:

tls

tls supports the following:

issuer_cert_authority

issuer_cert_authority supports the following:

client_alias

client_alias supports the following:

Attribute Reference

This resource exports the following attributes in addition to the arguments above:

Timeouts

Configuration options:

Import

In Terraform v1.5.0 and later, use an import block to import ECS services using the name together with ecs cluster name. For example:

import {
  to = aws_ecs_service.imported
  id = "cluster-name/service-name"
}

Using terraform import, import ECS services using the name together with ecs cluster name. For example:

% terraform import aws_ecs_service.imported cluster-name/service-name