2015-05-04 23:46:27 +02:00
---
layout: "aws"
page_title: "AWS: aws_ecs_service"
sidebar_current: "docs-aws-resource-ecs-service"
description: |-
Provides an ECS service.
---
# aws\_ecs\_service
2016-07-08 18:16:27 +02:00
-> **Note:** To prevent a race condition during service deletion, make sure to set `depends_on` to the related `aws_iam_role_policy` ; otherwise, the policy may be destroyed too soon and the ECS service will then get stuck in the `DRAINING` state.
2015-08-23 18:32:12 +02:00
2016-07-08 18:16:27 +02:00
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).
2015-05-04 23:46:27 +02:00
2016-01-14 21:55:39 +01:00
See [ECS Services section in AWS developer guide ](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs_services.html ).
2015-05-04 23:46:27 +02:00
## Example Usage
```
resource "aws_ecs_service" "mongo" {
2017-02-18 23:48:50 +01:00
name = "mongodb"
cluster = "${aws_ecs_cluster.foo.id}"
2015-05-04 23:46:27 +02:00
task_definition = "${aws_ecs_task_definition.mongo.arn}"
2017-02-18 23:48:50 +01:00
desired_count = 3
iam_role = "${aws_iam_role.foo.arn}"
depends_on = ["aws_iam_role_policy.foo"]
2015-05-04 23:46:27 +02:00
2017-01-17 19:10:45 +01:00
placement_strategy {
2017-02-18 23:48:50 +01:00
type = "binpack"
2017-03-14 13:58:09 +01:00
field = "cpu"
2017-01-17 19:10:45 +01:00
}
2015-05-04 23:46:27 +02:00
load_balancer {
2017-02-18 23:48:50 +01:00
elb_name = "${aws_elb.foo.name}"
2015-05-04 23:46:27 +02:00
container_name = "mongo"
container_port = 8080
}
2017-01-17 19:43:34 +01:00
placement_constraints {
2017-02-18 23:48:50 +01:00
type = "memberOf"
2017-01-17 19:43:34 +01:00
expression = "attribute:ecs.availability-zone in [us-west-2a, us-west-2b]"
}
2015-05-04 23:46:27 +02:00
}
```
## Argument Reference
The following arguments are supported:
* `name` - (Required) The name of the service (up to 255 letters, numbers, hyphens, and underscores)
* `task_definition` - (Required) The family and revision (`family:revision`) or full ARN of the task definition that you want to run in your service.
* `desired_count` - (Required) The number of instances of the task definition to place and keep running
* `cluster` - (Optional) ARN of an ECS cluster
2016-08-03 05:24:54 +02:00
* `iam_role` - (Optional) The ARN of IAM role that allows your Amazon ECS container agent to make calls to your load balancer on your behalf. This parameter is only required if you are using a load balancer with your service.
2016-02-19 21:32:39 +01:00
* `deployment_maximum_percent` - (Optional) The upper limit (as a percentage of the service's desiredCount) of the number of running tasks that can be running in a service during a deployment.
* `deployment_minimum_healthy_percent` - (Optional) The lower limit (as a percentage of the service's desiredCount) of the number of running tasks that must remain running and healthy in a service during a deployment.
2017-01-17 19:10:45 +01:00
* `placement_strategy` - (Optional) Service level strategy rules that are taken
into consideration during task placement. The maximum number of
2017-01-22 19:25:16 +01:00
`placement_strategy` blocks is `5` . Defined below.
2015-05-04 23:46:27 +02:00
* `load_balancer` - (Optional) A load balancer block. Load balancers documented below.
2017-01-17 19:43:34 +01:00
* `placement_constraints` - (Optional) rules that are taken into consideration during task placement. Maximum number of
`placement_constraints` is `10` . Defined below.
2015-05-04 23:46:27 +02:00
2016-07-08 18:16:27 +02:00
-> **Note:** As a result of an AWS limitation, a single `load_balancer` can be attached to the ECS service at most. See [related docs ](http://docs.aws.amazon.com/AmazonECS/latest/developerguide/service-load-balancing.html#load-balancing-concepts ).
2016-03-13 16:21:57 +01:00
2015-05-04 23:46:27 +02:00
Load balancers support the following:
2016-08-23 18:19:43 +02:00
* `elb_name` - (Required for ELB Classic) The name of the ELB (Classic) to associate with the service.
* `target_group_arn` - (Required for ALB) The ARN of the ALB target group to associate with the service.
2015-05-04 23:46:27 +02:00
* `container_name` - (Required) The name of the container to associate with the load balancer (as it appears in a container definition).
* `container_port` - (Required) The port on the container to associate with the load balancer.
2017-01-22 19:25:16 +01:00
## placement_strategy
`placement_strategy` supports the following:
* `type` - (Required) The type of placement strategy. Must be one of: `binpack` , `random` , or `spread`
* `field` - (Optional) For the `spread` placement strategy, valid values are instanceId (or host,
which has the same effect), or any platform or custom attribute that is applied to a container instance.
For the `binpack` type, valid values are `memory` and `cpu` . For the `random` type, this attribute is not
needed. For more information, see [Placement Strategy ](http://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_PlacementStrategy.html ).
2017-01-17 19:43:34 +01:00
## placement_constraints
`placement_constraints` support the following:
2017-01-19 23:54:52 +01:00
* `type` - (Required) The type of constraint. The only valid values at this time are `memberOf` and `distinctInstance` .
* `expression` - (Optional) Cluster Query Language expression to apply to the constraint. Does not need to be specified
for the `distinctInstance` type.
2017-01-17 19:43:34 +01:00
For more information, see [Cluster Query Language in the Amazon EC2 Container
Service Developer
Guide](http://docs.aws.amazon.com/AmazonECS/latest/developerguide/cluster-query-language.html).
2017-01-19 23:54:52 +01:00
2017-01-17 19:43:34 +01:00
2015-05-04 23:46:27 +02:00
## Attributes Reference
The following attributes are exported:
* `id` - The Amazon Resource Name (ARN) that identifies the service
* `name` - The name of the service
* `cluster` - The Amazon Resource Name (ARN) of cluster which the service runs on
* `iam_role` - The ARN of IAM role used for ELB
* `desired_count` - The number of instances of the task definition