Provider AWS: Add Placement Strategy to aws_ecs_service resource (#11201)
* Add aws_ecs_service placement_strategy param * Update docs
This commit is contained in:
parent
ebfaf42cb0
commit
e06f32ffe9
|
@ -101,6 +101,27 @@ func resourceAwsEcsService() *schema.Resource {
|
|||
},
|
||||
Set: resourceAwsEcsLoadBalancerHash,
|
||||
},
|
||||
|
||||
"placement_strategy": &schema.Schema{
|
||||
Type: schema.TypeSet,
|
||||
Optional: true,
|
||||
ForceNew: true,
|
||||
MaxItems: 5,
|
||||
Elem: &schema.Resource{
|
||||
Schema: map[string]*schema.Schema{
|
||||
"type": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
ForceNew: true,
|
||||
Required: true,
|
||||
},
|
||||
"field": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
ForceNew: true,
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -132,6 +153,19 @@ func resourceAwsEcsServiceCreate(d *schema.ResourceData, meta interface{}) error
|
|||
input.Role = aws.String(v.(string))
|
||||
}
|
||||
|
||||
strategies := d.Get("placement_strategy").(*schema.Set).List()
|
||||
if len(strategies) > 0 {
|
||||
var ps []*ecs.PlacementStrategy
|
||||
for _, raw := range strategies {
|
||||
p := raw.(map[string]interface{})
|
||||
ps = append(ps, &ecs.PlacementStrategy{
|
||||
Type: aws.String(p["type"].(string)),
|
||||
Field: aws.String(p["field"].(string)),
|
||||
})
|
||||
}
|
||||
input.PlacementStrategy = ps
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG] Creating ECS service: %s", input)
|
||||
|
||||
// Retry due to AWS IAM policy eventual consistency
|
||||
|
@ -240,9 +274,27 @@ func resourceAwsEcsServiceRead(d *schema.ResourceData, meta interface{}) error {
|
|||
d.Set("load_balancers", flattenEcsLoadBalancers(service.LoadBalancers))
|
||||
}
|
||||
|
||||
if err := d.Set("placement_strategy", flattenPlacementStrategy(service.PlacementStrategy)); err != nil {
|
||||
log.Printf("[ERR] Error setting placement_strategy for (%s): %s", d.Id(), err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func flattenPlacementStrategy(pss []*ecs.PlacementStrategy) []map[string]interface{} {
|
||||
if len(pss) == 0 {
|
||||
return nil
|
||||
}
|
||||
results := make([]map[string]interface{}, 0)
|
||||
for _, ps := range pss {
|
||||
c := make(map[string]interface{})
|
||||
c["type"] = *ps.Type
|
||||
c["field"] = *ps.Field
|
||||
results = append(results, c)
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
func resourceAwsEcsServiceUpdate(d *schema.ResourceData, meta interface{}) error {
|
||||
conn := meta.(*AWSClient).ecsconn
|
||||
|
||||
|
|
|
@ -257,6 +257,30 @@ func TestAccAWSEcsService_withAlb(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func TestAccAWSEcsServiceWithPlacementStrategy(t *testing.T) {
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckAWSEcsServiceDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: testAccAWSEcsService,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckAWSEcsServiceExists("aws_ecs_service.mongo"),
|
||||
resource.TestCheckResourceAttr("aws_ecs_service.mongo", "placement_strategy.#", "0"),
|
||||
),
|
||||
},
|
||||
resource.TestStep{
|
||||
Config: testAccAWSEcsServiceWithPlacementStrategy,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckAWSEcsServiceExists("aws_ecs_service.mongo"),
|
||||
resource.TestCheckResourceAttr("aws_ecs_service.mongo", "placement_strategy.#", "1"),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func testAccCheckAWSEcsServiceDestroy(s *terraform.State) error {
|
||||
conn := testAccProvider.Meta().(*AWSClient).ecsconn
|
||||
|
||||
|
@ -360,6 +384,38 @@ resource "aws_ecs_service" "mongo" {
|
|||
}
|
||||
`
|
||||
|
||||
var testAccAWSEcsServiceWithPlacementStrategy = `
|
||||
resource "aws_ecs_cluster" "default" {
|
||||
name = "terraformecstest1"
|
||||
}
|
||||
|
||||
resource "aws_ecs_task_definition" "mongo" {
|
||||
family = "mongodb"
|
||||
container_definitions = <<DEFINITION
|
||||
[
|
||||
{
|
||||
"cpu": 128,
|
||||
"essential": true,
|
||||
"image": "mongo:latest",
|
||||
"memory": 128,
|
||||
"name": "mongodb"
|
||||
}
|
||||
]
|
||||
DEFINITION
|
||||
}
|
||||
|
||||
resource "aws_ecs_service" "mongo" {
|
||||
name = "mongodb"
|
||||
cluster = "${aws_ecs_cluster.default.id}"
|
||||
task_definition = "${aws_ecs_task_definition.mongo.arn}"
|
||||
desired_count = 1
|
||||
placement_strategy {
|
||||
type = "binpack"
|
||||
field = "MEMORY"
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
var testAccAWSEcsService_withIamRole = `
|
||||
resource "aws_ecs_cluster" "main" {
|
||||
name = "terraformecstest11"
|
||||
|
|
|
@ -25,6 +25,11 @@ resource "aws_ecs_service" "mongo" {
|
|||
iam_role = "${aws_iam_role.foo.arn}"
|
||||
depends_on = ["aws_iam_role_policy.foo"]
|
||||
|
||||
placement_strategy {
|
||||
type = "binpack"
|
||||
field = "CPU"
|
||||
}
|
||||
|
||||
load_balancer {
|
||||
elb_name = "${aws_elb.foo.name}"
|
||||
container_name = "mongo"
|
||||
|
@ -44,6 +49,10 @@ The following arguments are supported:
|
|||
* `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.
|
||||
* `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.
|
||||
* `placement_strategy` - (Optional) Service level strategy rules that are taken
|
||||
into consideration during task placement. The maximum number of
|
||||
`placement_strategy` blocks is `5`. See [the related docs](http://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-placement-strategies.html) for
|
||||
details on attributes.
|
||||
* `load_balancer` - (Optional) A load balancer block. Load balancers documented below.
|
||||
|
||||
-> **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).
|
||||
|
|
Loading…
Reference in New Issue