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

aws_ecs_service support deployment_minimum_healthy_percent for DAEMON strategy #6150

Merged
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
34 changes: 25 additions & 9 deletions aws/resource_aws_ecs_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,13 @@ func resourceAwsEcsService() *schema.Resource {
"deployment_maximum_percent": {
Type: schema.TypeInt,
Optional: true,
Default: 200,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if d.Get("scheduling_strategy").(string) == ecs.SchedulingStrategyDaemon {
return true
} else { // must be SchedulingStrategyReplica
if !d.IsNewResource() && new == "0" {
return true
}
}
return false
},
Expand All @@ -105,9 +108,8 @@ func resourceAwsEcsService() *schema.Resource {
"deployment_minimum_healthy_percent": {
Type: schema.TypeInt,
Optional: true,
Default: 100,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if d.Get("scheduling_strategy").(string) == ecs.SchedulingStrategyDaemon {
if !d.IsNewResource() && new == "0" {
return true
}
return false
Expand Down Expand Up @@ -332,12 +334,20 @@ func resourceAwsEcsServiceCreate(d *schema.ResourceData, meta interface{}) error
TaskDefinition: aws.String(d.Get("task_definition").(string)),
DesiredCount: aws.Int64(int64(d.Get("desired_count").(int))),
ClientToken: aws.String(resource.UniqueId()),
DeploymentConfiguration: &ecs.DeploymentConfiguration{
MaximumPercent: aws.Int64(int64(d.Get("deployment_maximum_percent").(int))),
MinimumHealthyPercent: aws.Int64(int64(d.Get("deployment_minimum_healthy_percent").(int))),
},
}

deploymentConfiguration := &ecs.DeploymentConfiguration{}

if v, ok := d.GetOk("deployment_maximum_percent"); ok {
deploymentConfiguration.MaximumPercent = aws.Int64(int64(v.(int)))
}

if v, ok := d.GetOk("deployment_minimum_healthy_percent"); ok {
deploymentConfiguration.MinimumHealthyPercent = aws.Int64(int64(v.(int)))
}

input.DeploymentConfiguration = deploymentConfiguration

if v, ok := d.GetOk("cluster"); ok {
input.Cluster = aws.String(v.(string))
}
Expand All @@ -354,7 +364,7 @@ func resourceAwsEcsServiceCreate(d *schema.ResourceData, meta interface{}) error
input.SchedulingStrategy = aws.String(schedulingStrategy)
if schedulingStrategy == ecs.SchedulingStrategyDaemon {
// unset these if DAEMON
input.DeploymentConfiguration = nil
input.DeploymentConfiguration.MaximumPercent = nil
input.DesiredCount = nil
}

Expand Down Expand Up @@ -767,13 +777,19 @@ func resourceAwsEcsServiceUpdate(d *schema.ResourceData, meta interface{}) error
input.TaskDefinition = aws.String(n.(string))
}

if schedulingStrategy != ecs.SchedulingStrategyDaemon && (d.HasChange("deployment_maximum_percent") || d.HasChange("deployment_minimum_healthy_percent")) {
if schedulingStrategy == ecs.SchedulingStrategyReplica && (d.HasChange("deployment_maximum_percent") || d.HasChange("deployment_minimum_healthy_percent")) {
input.DeploymentConfiguration = &ecs.DeploymentConfiguration{
MaximumPercent: aws.Int64(int64(d.Get("deployment_maximum_percent").(int))),
MinimumHealthyPercent: aws.Int64(int64(d.Get("deployment_minimum_healthy_percent").(int))),
}
}

if schedulingStrategy == ecs.SchedulingStrategyDaemon && d.HasChange("deployment_minimum_healthy_percent") {
input.DeploymentConfiguration = &ecs.DeploymentConfiguration{
MinimumHealthyPercent: aws.Int64(int64(d.Get("deployment_minimum_healthy_percent").(int))),
}
}

if d.HasChange("network_configuration") {
input.NetworkConfiguration = expandEcsNetworkConfiguration(d.Get("network_configuration").([]interface{}))
}
Expand Down
53 changes: 53 additions & 0 deletions aws/resource_aws_ecs_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,30 @@ func TestAccAWSEcsService_withDaemonSchedulingStrategy(t *testing.T) {
})
}

func TestAccAWSEcsService_withDaemonSchedulingStrategySetDeploymentMinimum(t *testing.T) {
var service ecs.Service
rString := acctest.RandString(8)

clusterName := fmt.Sprintf("tf-acc-cluster-svc-w-ss-daemon-%s", rString)
tdName := fmt.Sprintf("tf-acc-td-svc-w-ss-daemon-%s", rString)
svcName := fmt.Sprintf("tf-acc-svc-w-ss-daemon-%s", rString)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEcsServiceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEcsServiceWithDaemonSchedulingStrategySetDeploymentMinimum(clusterName, tdName, svcName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEcsServiceExists("aws_ecs_service.ghost", &service),
resource.TestCheckResourceAttr("aws_ecs_service.ghost", "scheduling_strategy", "DAEMON"),
),
},
},
})
}

func TestAccAWSEcsService_withReplicaSchedulingStrategy(t *testing.T) {
var service ecs.Service
rString := acctest.RandString(8)
Expand Down Expand Up @@ -2096,6 +2120,35 @@ resource "aws_ecs_service" "ghost" {
`, clusterName, tdName, svcName)
}

func testAccAWSEcsServiceWithDaemonSchedulingStrategySetDeploymentMinimum(clusterName, tdName, svcName string) string {
return fmt.Sprintf(`
resource "aws_ecs_cluster" "default" {
name = "%s"
}
resource "aws_ecs_task_definition" "ghost" {
family = "%s"
container_definitions = <<DEFINITION
[
{
"cpu": 128,
"essential": true,
"image": "ghost:latest",
"memory": 128,
"name": "ghost"
}
]
DEFINITION
}
resource "aws_ecs_service" "ghost" {
name = "%s"
cluster = "${aws_ecs_cluster.default.id}"
task_definition = "${aws_ecs_task_definition.ghost.family}:${aws_ecs_task_definition.ghost.revision}"
scheduling_strategy = "DAEMON"
deployment_minimum_healthy_percent = "50"
bflad marked this conversation as resolved.
Show resolved Hide resolved
}
`, clusterName, tdName, svcName)
}

func testAccAWSEcsServiceWithReplicaSchedulingStrategy(clusterName, tdName, svcName string) string {
return fmt.Sprintf(`
resource "aws_ecs_cluster" "default" {
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/ecs_service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ The following arguments are supported:
* `cluster` - (Optional) ARN of an ECS cluster
* `iam_role` - (Optional) ARN of the IAM role that allows Amazon ECS to make calls to your load balancer on your behalf. This parameter is required if you are using a load balancer with your service, but only if your task definition does not use the `awsvpc` network mode. If using `awsvpc` network mode, do not specify this role. If your account has already created the Amazon ECS service-linked role, that role is used by default for your service unless you specify a role here.
* `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. 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.
* `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