Skip to content

Commit 96f6044

Browse files
grubernautstack72
authored andcommitted
provider/aws: ECS Placement constraints fix (hashicorp#11475)
* fixing AWS ECS placement constraints * correcting AWS ECS task definition doc * reverting unnecessary change to resource_aws_ecs_task_definition * provider/aws: ECS Placement constraints fix Expands upon hashicorp#11446 from @bgetsug Adds: - Acceptance Test - Improves `nil` check on constraint expression Fixes: hashicorp#10968
1 parent 061c8cc commit 96f6044

3 files changed

Lines changed: 61 additions & 8 deletions

File tree

builtin/providers/aws/resource_aws_ecs_service.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,14 @@ func resourceAwsEcsServiceCreate(d *schema.ResourceData, meta interface{}) error
202202
if err := validateAwsEcsPlacementConstraint(t, e); err != nil {
203203
return err
204204
}
205-
pc = append(pc, &ecs.PlacementConstraint{
206-
Type: aws.String(t),
207-
Expression: aws.String(e),
208-
})
205+
constraint := &ecs.PlacementConstraint{
206+
Type: aws.String(t),
207+
}
208+
if e != "" {
209+
constraint.Expression = aws.String(e)
210+
}
211+
212+
pc = append(pc, constraint)
209213
}
210214
input.PlacementConstraints = pc
211215
}
@@ -336,7 +340,10 @@ func flattenServicePlacementConstraints(pcs []*ecs.PlacementConstraint) []map[st
336340
for _, pc := range pcs {
337341
c := make(map[string]interface{})
338342
c["type"] = *pc.Type
339-
c["expression"] = *pc.Expression
343+
if pc.Expression != nil {
344+
c["expression"] = *pc.Expression
345+
}
346+
340347
results = append(results, c)
341348
}
342349
return results

builtin/providers/aws/resource_aws_ecs_service_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,23 @@ func TestAccAWSEcsServiceWithPlacementConstraints(t *testing.T) {
298298
})
299299
}
300300

301+
func TestAccAWSEcsServiceWithPlacementConstraints_emptyExpression(t *testing.T) {
302+
resource.Test(t, resource.TestCase{
303+
PreCheck: func() { testAccPreCheck(t) },
304+
Providers: testAccProviders,
305+
CheckDestroy: testAccCheckAWSEcsServiceDestroy,
306+
Steps: []resource.TestStep{
307+
{
308+
Config: testAccAWSEcsServiceWithPlacementConstraintEmptyExpression,
309+
Check: resource.ComposeTestCheckFunc(
310+
testAccCheckAWSEcsServiceExists("aws_ecs_service.mongo"),
311+
resource.TestCheckResourceAttr("aws_ecs_service.mongo", "placement_constraints.#", "1"),
312+
),
313+
},
314+
},
315+
})
316+
}
317+
301318
func testAccCheckAWSEcsServiceDestroy(s *terraform.State) error {
302319
conn := testAccProvider.Meta().(*AWSClient).ecsconn
303320

@@ -465,6 +482,35 @@ resource "aws_ecs_service" "mongo" {
465482
}
466483
`
467484

485+
var testAccAWSEcsServiceWithPlacementConstraintEmptyExpression = `
486+
resource "aws_ecs_cluster" "default" {
487+
name = "terraformecstest212"
488+
}
489+
resource "aws_ecs_task_definition" "mongo" {
490+
family = "mongodb"
491+
container_definitions = <<DEFINITION
492+
[
493+
{
494+
"cpu": 128,
495+
"essential": true,
496+
"image": "mongo:latest",
497+
"memory": 128,
498+
"name": "mongodb"
499+
}
500+
]
501+
DEFINITION
502+
}
503+
resource "aws_ecs_service" "mongo" {
504+
name = "mongodb"
505+
cluster = "${aws_ecs_cluster.default.id}"
506+
task_definition = "${aws_ecs_task_definition.mongo.arn}"
507+
desired_count = 1
508+
placement_constraints {
509+
type = "distinctInstance"
510+
}
511+
}
512+
`
513+
468514
var testAccAWSEcsService_withIamRole = `
469515
resource "aws_ecs_cluster" "main" {
470516
name = "terraformecstest11"

website/source/docs/providers/aws/r/ecs_task_definition.html.markdown

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ parameter of container definition in the `mountPoints` section.
9292

9393
`placement_constraints` support the following:
9494

95-
* `type` - (Required) The type of constraint. The only valid values at this time are `memberOf` or `distinctInstance`.
96-
* `expression` - (Optional) Cluster Query Language expression to apply to the constraint. Does not need to be specified
97-
for the `distinctInstance` type.
95+
* `type` - (Required) The type of constraint. Use `memberOf` to restrict selection to a group of valid candidates.
96+
Note that `distinctInstance` is not supported in task definitions.
97+
* `expression` - (Optional) Cluster Query Language expression to apply to the constraint.
9898
For more information, see [Cluster Query Language in the Amazon EC2 Container
9999
Service Developer
100100
Guide](http://docs.aws.amazon.com/AmazonECS/latest/developerguide/cluster-query-language.html).

0 commit comments

Comments
 (0)