Skip to content

Commit

Permalink
resource/aws_alb_target_group: Add support for target_type
Browse files Browse the repository at this point in the history
Fixes: #1588

```
% make testacc TEST=./aws TESTARGS='-run=TestAccAWSALBTargetGroup_'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -run=TestAccAWSALBTargetGroup_ -timeout 120m
=== RUN   TestAccAWSALBTargetGroup_basic
--- PASS: TestAccAWSALBTargetGroup_basic (73.55s)
=== RUN   TestAccAWSALBTargetGroup_namePrefix
--- PASS: TestAccAWSALBTargetGroup_namePrefix (72.26s)
=== RUN   TestAccAWSALBTargetGroup_generatedName
--- PASS: TestAccAWSALBTargetGroup_generatedName (74.94s)
=== RUN   TestAccAWSALBTargetGroup_changeNameForceNew
--- PASS: TestAccAWSALBTargetGroup_changeNameForceNew (129.78s)
=== RUN   TestAccAWSALBTargetGroup_changeProtocolForceNew
--- PASS: TestAccAWSALBTargetGroup_changeProtocolForceNew (151.66s)
=== RUN   TestAccAWSALBTargetGroup_changePortForceNew
--- PASS: TestAccAWSALBTargetGroup_changePortForceNew (129.73s)
=== RUN   TestAccAWSALBTargetGroup_changeVpcForceNew
--- PASS: TestAccAWSALBTargetGroup_changeVpcForceNew (112.48s)
=== RUN   TestAccAWSALBTargetGroup_tags
--- PASS: TestAccAWSALBTargetGroup_tags (124.72s)
=== RUN   TestAccAWSALBTargetGroup_updateHealthCheck
--- PASS: TestAccAWSALBTargetGroup_updateHealthCheck (122.61s)
=== RUN   TestAccAWSALBTargetGroup_updateSticknessEnabled
--- PASS: TestAccAWSALBTargetGroup_updateSticknessEnabled (170.23s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	1161.982s
```
  • Loading branch information
stack72 committed Oct 5, 2017
1 parent ecc0491 commit 7e2a397
Show file tree
Hide file tree
Showing 6 changed files with 893 additions and 29 deletions.
789 changes: 789 additions & 0 deletions aws/resource_aws_alb_target_group_test.go

Large diffs are not rendered by default.

17 changes: 13 additions & 4 deletions aws/resource_aws_lb_target_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ func resourceAwsLbTargetGroup() *schema.Resource {
ValidateFunc: validateAwsLbTargetGroupDeregistrationDelay,
},

"target_type": {
Type: schema.TypeString,
Optional: true,
Default: "instance",
ForceNew: true,
},

"stickiness": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -191,10 +198,11 @@ func resourceAwsLbTargetGroupCreate(d *schema.ResourceData, meta interface{}) er
}

params := &elbv2.CreateTargetGroupInput{
Name: aws.String(groupName),
Port: aws.Int64(int64(d.Get("port").(int))),
Protocol: aws.String(d.Get("protocol").(string)),
VpcId: aws.String(d.Get("vpc_id").(string)),
Name: aws.String(groupName),
Port: aws.Int64(int64(d.Get("port").(int))),
Protocol: aws.String(d.Get("protocol").(string)),
VpcId: aws.String(d.Get("vpc_id").(string)),
TargetType: aws.String(d.Get("target_type").(string)),
}

if healthChecks := d.Get("health_check").([]interface{}); len(healthChecks) == 1 {
Expand Down Expand Up @@ -476,6 +484,7 @@ func flattenAwsLbTargetGroupResource(d *schema.ResourceData, meta interface{}, t
d.Set("port", targetGroup.Port)
d.Set("protocol", targetGroup.Protocol)
d.Set("vpc_id", targetGroup.VpcId)
d.Set("target_type", targetGroup.TargetType)

healthCheck := make(map[string]interface{})
healthCheck["interval"] = *targetGroup.HealthCheckIntervalSeconds
Expand Down
18 changes: 18 additions & 0 deletions aws/resource_aws_lb_target_group_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ func resourceAwsLbTargetGroupAttachment() *schema.Resource {
ForceNew: true,
Optional: true,
},

"availability_zone": {
Type: schema.TypeString,
ForceNew: true,
Optional: true,
},
},
}
}
Expand All @@ -51,6 +57,10 @@ func resourceAwsLbAttachmentCreate(d *schema.ResourceData, meta interface{}) err
target.Port = aws.Int64(int64(v.(int)))
}

if v, ok := d.GetOk("availability_zone"); ok {
target.AvailabilityZone = aws.String(v.(string))
}

params := &elbv2.RegisterTargetsInput{
TargetGroupArn: aws.String(d.Get("target_group_arn").(string)),
Targets: []*elbv2.TargetDescription{target},
Expand Down Expand Up @@ -80,6 +90,10 @@ func resourceAwsLbAttachmentDelete(d *schema.ResourceData, meta interface{}) err
target.Port = aws.Int64(int64(v.(int)))
}

if v, ok := d.GetOk("availability_zone"); ok {
target.AvailabilityZone = aws.String(v.(string))
}

params := &elbv2.DeregisterTargetsInput{
TargetGroupArn: aws.String(d.Get("target_group_arn").(string)),
Targets: []*elbv2.TargetDescription{target},
Expand Down Expand Up @@ -108,6 +122,10 @@ func resourceAwsLbAttachmentRead(d *schema.ResourceData, meta interface{}) error
target.Port = aws.Int64(int64(v.(int)))
}

if v, ok := d.GetOk("availability_zone"); ok {
target.AvailabilityZone = aws.String(v.(string))
}

resp, err := elbconn.DescribeTargetHealth(&elbv2.DescribeTargetHealthInput{
TargetGroupArn: aws.String(d.Get("target_group_arn").(string)),
Targets: []*elbv2.TargetDescription{target},
Expand Down
89 changes: 65 additions & 24 deletions aws/resource_aws_lb_target_group_attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,25 @@ func TestAccAWSLBTargetGroupAttachment_withoutPort(t *testing.T) {
})
}

func TestAccAWSALBTargetGroupAttachment_ipAddress(t *testing.T) {
targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: "aws_lb_target_group.test",
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSLBTargetGroupAttachmentDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLBTargetGroupAttachmentConfigWithIpAddress(targetGroupName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSLBTargetGroupAttachmentExists("aws_lb_target_group_attachment.test"),
),
},
},
})
}

func testAccCheckAWSLBTargetGroupAttachmentExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -158,26 +177,21 @@ resource "aws_lb_target_group_attachment" "test" {
target_group_arn = "${aws_lb_target_group.test.arn}"
target_id = "${aws_instance.test.id}"
}
resource "aws_instance" "test" {
ami = "ami-f701cb97"
instance_type = "t2.micro"
subnet_id = "${aws_subnet.subnet.id}"
}
resource "aws_lb_target_group" "test" {
name = "%s"
port = 443
protocol = "HTTPS"
vpc_id = "${aws_vpc.test.id}"
deregistration_delay = 200
stickiness {
type = "lb_cookie"
cookie_duration = 10000
}
health_check {
path = "/health"
interval = 60
Expand All @@ -189,13 +203,10 @@ resource "aws_lb_target_group" "test" {
matcher = "200-299"
}
}
resource "aws_subnet" "subnet" {
cidr_block = "10.0.1.0/24"
vpc_id = "${aws_vpc.test.id}"
}
resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"
tags {
Expand All @@ -211,26 +222,21 @@ resource "aws_lb_target_group_attachment" "test" {
target_id = "${aws_instance.test.id}"
port = 80
}
resource "aws_instance" "test" {
ami = "ami-f701cb97"
instance_type = "t2.micro"
subnet_id = "${aws_subnet.subnet.id}"
}
resource "aws_lb_target_group" "test" {
name = "%s"
port = 443
protocol = "HTTPS"
vpc_id = "${aws_vpc.test.id}"
deregistration_delay = 200
stickiness {
type = "lb_cookie"
cookie_duration = 10000
}
health_check {
path = "/health"
interval = 60
Expand All @@ -242,13 +248,10 @@ resource "aws_lb_target_group" "test" {
matcher = "200-299"
}
}
resource "aws_subnet" "subnet" {
cidr_block = "10.0.1.0/24"
vpc_id = "${aws_vpc.test.id}"
}
resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"
tags {
Expand All @@ -264,26 +267,21 @@ resource "aws_alb_target_group_attachment" "test" {
target_id = "${aws_instance.test.id}"
port = 80
}
resource "aws_instance" "test" {
ami = "ami-f701cb97"
instance_type = "t2.micro"
subnet_id = "${aws_subnet.subnet.id}"
}
resource "aws_alb_target_group" "test" {
name = "%s"
port = 443
protocol = "HTTPS"
vpc_id = "${aws_vpc.test.id}"
deregistration_delay = 200
stickiness {
type = "lb_cookie"
cookie_duration = 10000
}
health_check {
path = "/health"
interval = 60
Expand All @@ -295,17 +293,60 @@ resource "aws_alb_target_group" "test" {
matcher = "200-299"
}
}
resource "aws_subnet" "subnet" {
cidr_block = "10.0.1.0/24"
vpc_id = "${aws_vpc.test.id}"
}
resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"
tags {
Name = "testAccAWSLBTargetGroupAttachmentConfig_basic"
}
}`, targetGroupName)
}

func testAccAWSLBTargetGroupAttachmentConfigWithIpAddress(targetGroupName string) string {
return fmt.Sprintf(`
resource "aws_lb_target_group_attachment" "test" {
target_group_arn = "${aws_lb_target_group.test.arn}"
target_id = "${aws_instance.test.private_ip}"
availability_zone = "${aws_instance.test.availability_zone}"
}
resource "aws_instance" "test" {
ami = "ami-f701cb97"
instance_type = "t2.micro"
subnet_id = "${aws_subnet.subnet.id}"
}
resource "aws_lb_target_group" "test" {
name = "%s"
port = 443
protocol = "HTTPS"
vpc_id = "${aws_vpc.test.id}"
target_type = "ip"
deregistration_delay = 200
stickiness {
type = "lb_cookie"
cookie_duration = 10000
}
health_check {
path = "/health"
interval = 60
port = 8081
protocol = "HTTP"
timeout = 3
healthy_threshold = 3
unhealthy_threshold = 3
matcher = "200-299"
}
}
resource "aws_subnet" "subnet" {
cidr_block = "10.0.1.0/24"
vpc_id = "${aws_vpc.test.id}"
}
resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"
tags {
Name = "testAccAWSALBTargetGroupAttachmentConfigWithoutPort"
}
}`, targetGroupName)
}
6 changes: 6 additions & 0 deletions website/docs/r/lb_target_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ The following arguments are supported:
* `deregistration_delay` - (Optional) The amount time for Elastic Load Balancing to wait before changing the state of a deregistering target from draining to unused. The range is 0-3600 seconds. The default value is 300 seconds.
* `stickiness` - (Optional) A Stickiness block. Stickiness blocks are documented below.
* `health_check` - (Optional) A Health Check block. Health Check blocks are documented below.
* `target_type` - (Optional) The type of target that you must specify when registering targets with this target group.
The possible values are `instance` (targets are specified by instance ID) or `ip` (targets are specified by IP address).
The default is `instance`. Note that you can't specify targets for a target group using both instance IDs and IP addresses.
If the target type is `ip`, specify IP addresses from the subnets of the virtual private cloud (VPC) for the target group,
the RFC 1918 range (10.0.0.0/8, 172.16.0.0/12, and 192.168.0.0/16), and the RFC 6598 range (100.64.0.0/10).
You can't specify publicly routable IP addresses.
* `tags` - (Optional) A mapping of tags to assign to the resource.

Stickiness Blocks (`stickiness`) support the following:
Expand Down
3 changes: 2 additions & 1 deletion website/docs/r/lb_target_group_attachment.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ resource "aws_instance" "test" {
The following arguments are supported:

* `target_group_arn` - (Required) The ARN of the target group with which to register targets
* `target_id` (Required) The ID of the target. This is the Instance ID for an instance, or the container ID for an ECS container.
* `target_id` (Required) The ID of the target. This is the Instance ID for an instance, or the container ID for an ECS container. If the target type is ip, specify an IP address.
* `port` - (Optional) The port on which targets receive traffic.
* `availability_zone` - (Optional) The Availability Zone where the IP address of the target is to be registered.

## Attributes Reference

Expand Down

0 comments on commit 7e2a397

Please sign in to comment.