Skip to content

Commit

Permalink
r/aws_ecs_service: Add health_check_grace_period_seconds attribute (#…
Browse files Browse the repository at this point in the history
…2788)

* r/aws_ecs_service: Add health_check_grace_period_seconds attribute

* r/aws_ecs_service: Indentation fixes for testAccAWSEcsService_healthCheckGracePeriodSeconds
  • Loading branch information
bflad authored and radeksimko committed Jan 8, 2018
1 parent b221c37 commit 78d0314
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 1 deletion.
24 changes: 23 additions & 1 deletion aws/resource_aws_ecs_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ func resourceAwsEcsService() *schema.Resource {
Optional: true,
},

"health_check_grace_period_seconds": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validateAwsEcsServiceHealthCheckGracePeriodSeconds,
},

"launch_type": {
Type: schema.TypeString,
ForceNew: true,
Expand Down Expand Up @@ -213,6 +219,10 @@ func resourceAwsEcsServiceCreate(d *schema.ResourceData, meta interface{}) error
input.Cluster = aws.String(v.(string))
}

if v, ok := d.GetOk("health_check_grace_period_seconds"); ok {
input.HealthCheckGracePeriodSeconds = aws.Int64(int64(v.(int)))
}

if v, ok := d.GetOk("launch_type"); ok {
input.LaunchType = aws.String(v.(string))
}
Expand Down Expand Up @@ -352,7 +362,7 @@ func resourceAwsEcsServiceRead(d *schema.ResourceData, meta interface{}) error {
}

d.Set("desired_count", service.DesiredCount)

d.Set("health_check_grace_period_seconds", service.HealthCheckGracePeriodSeconds)
d.Set("launch_type", service.LaunchType)

// Save cluster in the same format
Expand Down Expand Up @@ -469,6 +479,10 @@ func resourceAwsEcsServiceUpdate(d *schema.ResourceData, meta interface{}) error
_, n := d.GetChange("desired_count")
input.DesiredCount = aws.Int64(int64(n.(int)))
}
if d.HasChange("health_check_grace_period_seconds") {
_, n := d.GetChange("health_check_grace_period_seconds")
input.HealthCheckGracePeriodSeconds = aws.Int64(int64(n.(int)))
}
if d.HasChange("task_definition") {
_, n := d.GetChange("task_definition")
input.TaskDefinition = aws.String(n.(string))
Expand Down Expand Up @@ -646,3 +660,11 @@ func parseTaskDefinition(taskDefinition string) (string, string, error) {

return matches[0][1], matches[0][2], nil
}

func validateAwsEcsServiceHealthCheckGracePeriodSeconds(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)
if (value < 0) || (value > 1800) {
errors = append(errors, fmt.Errorf("%q must be between 0 and 1800", k))
}
return
}
167 changes: 167 additions & 0 deletions aws/resource_aws_ecs_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,41 @@ func TestAccAWSEcsServiceWithRenamedCluster(t *testing.T) {
})
}

func TestAccAWSEcsService_healthCheckGracePeriodSeconds(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc")
resourceName := "aws_ecs_service.with_alb"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEcsServiceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEcsService_healthCheckGracePeriodSeconds(rName, t.Name(), -1),
ExpectError: regexp.MustCompile(`must be between 0 and 1800`),
},
{
Config: testAccAWSEcsService_healthCheckGracePeriodSeconds(rName, t.Name(), 1801),
ExpectError: regexp.MustCompile(`must be between 0 and 1800`),
},
{
Config: testAccAWSEcsService_healthCheckGracePeriodSeconds(rName, t.Name(), 300),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEcsServiceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "health_check_grace_period_seconds", "300"),
),
},
{
Config: testAccAWSEcsService_healthCheckGracePeriodSeconds(rName, t.Name(), 600),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEcsServiceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "health_check_grace_period_seconds", "600"),
),
},
},
})
}

func TestAccAWSEcsService_withIamRole(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down Expand Up @@ -698,6 +733,138 @@ resource "aws_ecs_service" "main" {
`, rInt, rInt, rInt, rInt)
}

func testAccAWSEcsService_healthCheckGracePeriodSeconds(rName string, testName string, healthCheckGracePeriodSeconds int) string {
return fmt.Sprintf(`
data "aws_availability_zones" "available" {}
resource "aws_vpc" "main" {
cidr_block = "10.10.0.0/16"
tags {
Name = "%[2]s"
}
}
resource "aws_subnet" "main" {
count = 2
cidr_block = "${cidrsubnet(aws_vpc.main.cidr_block, 8, count.index)}"
availability_zone = "${data.aws_availability_zones.available.names[count.index]}"
vpc_id = "${aws_vpc.main.id}"
}
resource "aws_ecs_cluster" "main" {
name = "%[1]s"
}
resource "aws_ecs_task_definition" "with_lb_changes" {
family = "%[1]s"
container_definitions = <<DEFINITION
[
{
"cpu": 256,
"essential": true,
"image": "ghost:latest",
"memory": 512,
"name": "ghost",
"portMappings": [
{
"containerPort": 2368,
"hostPort": 8080
}
]
}
]
DEFINITION
}
resource "aws_iam_role" "ecs_service" {
name = "%[1]s"
assume_role_policy = <<EOF
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "ecs.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}
resource "aws_iam_role_policy" "ecs_service" {
name = "%[1]s"
role = "${aws_iam_role.ecs_service.name}"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ec2:Describe*",
"elasticloadbalancing:DeregisterInstancesFromLoadBalancer",
"elasticloadbalancing:DeregisterTargets",
"elasticloadbalancing:Describe*",
"elasticloadbalancing:RegisterInstancesWithLoadBalancer",
"elasticloadbalancing:RegisterTargets"
],
"Resource": "*"
}
]
}
EOF
}
resource "aws_lb_target_group" "test" {
name = "%[1]s"
port = 80
protocol = "HTTP"
vpc_id = "${aws_vpc.main.id}"
}
resource "aws_lb" "main" {
name = "%[1]s"
internal = true
subnets = ["${aws_subnet.main.*.id}"]
}
resource "aws_lb_listener" "front_end" {
load_balancer_arn = "${aws_lb.main.id}"
port = "80"
protocol = "HTTP"
default_action {
target_group_arn = "${aws_lb_target_group.test.id}"
type = "forward"
}
}
resource "aws_ecs_service" "with_alb" {
name = "%[1]s"
cluster = "${aws_ecs_cluster.main.id}"
task_definition = "${aws_ecs_task_definition.with_lb_changes.arn}"
desired_count = 1
health_check_grace_period_seconds = %[3]d
iam_role = "${aws_iam_role.ecs_service.name}"
load_balancer {
target_group_arn = "${aws_lb_target_group.test.id}"
container_name = "ghost"
container_port = "2368"
}
depends_on = [
"aws_iam_role_policy.ecs_service",
"aws_lb_listener.front_end"
]
}
`, rName, testName, healthCheckGracePeriodSeconds)
}

var testAccAWSEcsService_withIamRole = `
resource "aws_ecs_cluster" "main" {
name = "terraformecstest11"
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 @@ -58,6 +58,7 @@ The following arguments are supported:
* `placement_strategy` - (Optional) Service level strategy rules that are taken
into consideration during task placement. The maximum number of
`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 1800. Only valid for services configured to use load balancers.
* `load_balancer` - (Optional) A load balancer block. Load balancers documented below.
* `placement_constraints` - (Optional) rules that are taken into consideration during task placement. Maximum number of
`placement_constraints` is `10`. Defined below.
Expand Down

0 comments on commit 78d0314

Please sign in to comment.