Skip to content

Commit

Permalink
Merge pull request #6603 from kl4w/ecs-service-propagate-tags
Browse files Browse the repository at this point in the history
r/ecs_service: add propagate_tags attribute
  • Loading branch information
bflad authored Dec 5, 2018
2 parents 80bd076 + 343ddaf commit fb5a1be
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
22 changes: 22 additions & 0 deletions aws/resource_aws_ecs_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,23 @@ func resourceAwsEcsService() *schema.Resource {
},
},

"propagate_tags": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if old == "NONE" && new == "" {
return true
}
return false
},
ValidateFunc: validation.StringInSlice([]string{
ecs.PropagateTagsService,
ecs.PropagateTagsTaskDefinition,
"",
}, false),
},

"service_registries": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -395,6 +412,10 @@ func resourceAwsEcsServiceCreate(d *schema.ResourceData, meta interface{}) error
input.LaunchType = aws.String(v.(string))
}

if v, ok := d.GetOk("propagate_tags"); ok {
input.PropagateTags = aws.String(v.(string))
}

loadBalancers := expandEcsLoadBalancers(d.Get("load_balancer").(*schema.Set).List())
if len(loadBalancers) > 0 {
log.Printf("[DEBUG] Adding ECS load balancers: %s", loadBalancers)
Expand Down Expand Up @@ -574,6 +595,7 @@ func resourceAwsEcsServiceRead(d *schema.ResourceData, meta interface{}) error {
d.Set("health_check_grace_period_seconds", service.HealthCheckGracePeriodSeconds)
d.Set("launch_type", service.LaunchType)
d.Set("enable_ecs_managed_tags", service.EnableECSManagedTags)
d.Set("propagate_tags", service.PropagateTags)

// Save cluster in the same format
if strings.HasPrefix(d.Get("cluster").(string), "arn:"+meta.(*AWSClient).partition+":ecs:") {
Expand Down
77 changes: 77 additions & 0 deletions aws/resource_aws_ecs_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,42 @@ func TestAccAWSEcsService_ManagedTags(t *testing.T) {
})
}

func TestAccAWSEcsService_PropagateTags(t *testing.T) {
var first, second, third ecs.Service
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_ecs_service.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEcsServiceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEcsServiceConfigPropagateTags(rName, "SERVICE"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEcsServiceExists(resourceName, &first),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "propagate_tags", ecs.PropagateTagsService),
),
},
{
Config: testAccAWSEcsServiceConfigPropagateTags(rName, "TASK_DEFINITION"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEcsServiceExists(resourceName, &second),
resource.TestCheckResourceAttr(resourceName, "propagate_tags", ecs.PropagateTagsTaskDefinition),
),
},
{
Config: testAccAWSEcsServiceConfigManagedTags(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEcsServiceExists(resourceName, &third),
resource.TestCheckResourceAttr(resourceName, "propagate_tags", "NONE"),
),
},
},
})
}

func testAccCheckAWSEcsServiceDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ecsconn

Expand Down Expand Up @@ -2519,6 +2555,47 @@ resource "aws_ecs_service" "test" {
`, rName, rName, rName)
}

func testAccAWSEcsServiceConfigPropagateTags(rName, propagate string) string {
return fmt.Sprintf(`
resource "aws_ecs_cluster" "test" {
name = %q
}
resource "aws_ecs_task_definition" "test" {
family = %q
container_definitions = <<DEFINITION
[
{
"cpu": 128,
"essential": true,
"image": "mongo:latest",
"memory": 128,
"name": "mongodb"
}
]
DEFINITION
tags {
tag-key = "task-def"
}
}
resource "aws_ecs_service" "test" {
cluster = "${aws_ecs_cluster.test.id}"
desired_count = 0
name = %q
task_definition = "${aws_ecs_task_definition.test.arn}"
enable_ecs_managed_tags = true
propagate_tags = "%s"
tags {
tag-key = "service"
}
}
`, rName, rName, rName, propagate)
}

func testAccAWSEcsServiceWithReplicaSchedulingStrategy(clusterName, tdName, svcName string) string {
return fmt.Sprintf(`
resource "aws_ecs_cluster" "default" {
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/ecs_service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ The following arguments are supported:
* `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. Not valid when using the `DAEMON` scheduling strategy.
* `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.
* `enable_ecs_managed_tags` - (Optional) Specifies whether to enable Amazon ECS managed tags for the tasks within the service.
* `propagate_tags` - (Optional) Specifies whether to propagate the tags from the task definition or the service to the tasks. The valid values are `SERVICE` and `TASK_DEFINITION`.
* `placement_strategy` - (Optional) **Deprecated**, use `ordered_placement_strategy` instead.
* `ordered_placement_strategy` - (Optional) Service level strategy rules that are taken into consideration during task placement. List from top to bottom in order of precedence. The maximum number of `ordered_placement_strategy` blocks is `5`. Defined below.
* `health_check_grace_period_seconds` - (Optional) Seconds to ignore failing load balancer health checks on newly instantiated tasks to prevent premature shutdown, up to 7200. Only valid for services configured to use load balancers.
Expand Down

0 comments on commit fb5a1be

Please sign in to comment.