Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/ecs_service: add propagate_tags attribute #6603

Merged
merged 2 commits into from
Dec 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": {
kl4w marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Optional: true,
ForceNew: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if old == "NONE" && new == "" {
bflad marked this conversation as resolved.
Show resolved Hide resolved
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