diff --git a/aws/data_source_aws_alb_target_group_test.go b/aws/data_source_aws_alb_target_group_test.go deleted file mode 100644 index 173fed88f16..00000000000 --- a/aws/data_source_aws_alb_target_group_test.go +++ /dev/null @@ -1,172 +0,0 @@ -package aws - -import ( - "fmt" - "testing" - - "github.com/hashicorp/terraform/helper/acctest" - "github.com/hashicorp/terraform/helper/resource" -) - -func TestAccDataSourceAWSALBTargetGroup_basic(t *testing.T) { - albName := fmt.Sprintf("testalb-%s", acctest.RandStringFromCharSet(13, acctest.CharSetAlphaNum)) - targetGroupName := fmt.Sprintf("testtargetgroup-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: testAccDataSourceAWSALBTargetGroupConfigBasic(albName, targetGroupName), - Check: resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "name", targetGroupName), - resource.TestCheckResourceAttrSet("data.aws_alb_target_group.alb_tg_test_with_arn", "arn"), - resource.TestCheckResourceAttrSet("data.aws_alb_target_group.alb_tg_test_with_arn", "arn_suffix"), - resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "port", "8080"), - resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "protocol", "HTTP"), - resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "protocol", "HTTP"), - resource.TestCheckResourceAttrSet("data.aws_alb_target_group.alb_tg_test_with_arn", "vpc_id"), - resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "deregistration_delay", "300"), - resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "tags.%", "1"), - resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "tags.TestName", "TestAccDataSourceAWSALBTargetGroup_basic"), - resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "stickiness.#", "1"), - resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "health_check.#", "1"), - resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "health_check.0.path", "/health"), - resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "health_check.0.port", "8081"), - resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "health_check.0.protocol", "HTTP"), - resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "health_check.0.timeout", "3"), - resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "health_check.0.healthy_threshold", "3"), - resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "health_check.0.unhealthy_threshold", "3"), - resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "health_check.0.matcher", "200-299"), - - resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "name", targetGroupName), - resource.TestCheckResourceAttrSet("data.aws_alb_target_group.alb_tg_test_with_name", "arn"), - resource.TestCheckResourceAttrSet("data.aws_alb_target_group.alb_tg_test_with_name", "arn_suffix"), - resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "port", "8080"), - resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "protocol", "HTTP"), - resource.TestCheckResourceAttrSet("data.aws_alb_target_group.alb_tg_test_with_name", "vpc_id"), - resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "deregistration_delay", "300"), - resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "tags.%", "1"), - resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "tags.TestName", "TestAccDataSourceAWSALBTargetGroup_basic"), - resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "stickiness.#", "1"), - resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "health_check.#", "1"), - resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "health_check.0.path", "/health"), - resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "health_check.0.port", "8081"), - resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "health_check.0.protocol", "HTTP"), - resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "health_check.0.timeout", "3"), - resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "health_check.0.healthy_threshold", "3"), - resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "health_check.0.unhealthy_threshold", "3"), - resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "health_check.0.matcher", "200-299"), - ), - }, - }, - }) -} - -func testAccDataSourceAWSALBTargetGroupConfigBasic(albName string, targetGroupName string) string { - return fmt.Sprintf(`resource "aws_alb_listener" "front_end" { - load_balancer_arn = "${aws_alb.alb_test.id}" - protocol = "HTTP" - port = "80" - - default_action { - target_group_arn = "${aws_alb_target_group.test.id}" - type = "forward" - } -} - -resource "aws_alb" "alb_test" { - name = "%s" - internal = true - security_groups = ["${aws_security_group.alb_test.id}"] - subnets = ["${aws_subnet.alb_test.*.id}"] - - idle_timeout = 30 - enable_deletion_protection = false - - tags { - TestName = "TestAccDataSourceAWSALBTargetGroup_basic" - } -} - -resource "aws_alb_target_group" "test" { - name = "%s" - port = 8080 - protocol = "HTTP" - vpc_id = "${aws_vpc.alb_test.id}" - - health_check { - path = "/health" - interval = 60 - port = 8081 - protocol = "HTTP" - timeout = 3 - healthy_threshold = 3 - unhealthy_threshold = 3 - matcher = "200-299" - } - - tags { - TestName = "TestAccDataSourceAWSALBTargetGroup_basic" - } -} - -variable "subnets" { - default = ["10.0.1.0/24", "10.0.2.0/24"] - type = "list" -} - -data "aws_availability_zones" "available" {} - -resource "aws_vpc" "alb_test" { - cidr_block = "10.0.0.0/16" - - tags { - TestName = "TestAccDataSourceAWSALBTargetGroup_basic" - } -} - -resource "aws_subnet" "alb_test" { - count = 2 - vpc_id = "${aws_vpc.alb_test.id}" - cidr_block = "${element(var.subnets, count.index)}" - map_public_ip_on_launch = true - availability_zone = "${element(data.aws_availability_zones.available.names, count.index)}" - - tags { - TestName = "TestAccDataSourceAWSALBTargetGroup_basic" - } -} - -resource "aws_security_group" "alb_test" { - name = "allow_all_alb_test" - description = "Used for ALB Testing" - vpc_id = "${aws_vpc.alb_test.id}" - - ingress { - from_port = 0 - to_port = 0 - protocol = "-1" - cidr_blocks = ["0.0.0.0/0"] - } - - egress { - from_port = 0 - to_port = 0 - protocol = "-1" - cidr_blocks = ["0.0.0.0/0"] - } - - tags { - TestName = "TestAccDataSourceAWSALBTargetGroup_basic" - } -} - -data "aws_alb_target_group" "alb_tg_test_with_arn" { - arn = "${aws_alb_target_group.test.arn}" -} - -data "aws_alb_target_group" "alb_tg_test_with_name" { - name = "${aws_alb_target_group.test.name}" -}`, albName, targetGroupName) -} diff --git a/aws/data_source_aws_alb_test.go b/aws/data_source_aws_alb_test.go deleted file mode 100644 index 2626fe74b75..00000000000 --- a/aws/data_source_aws_alb_test.go +++ /dev/null @@ -1,124 +0,0 @@ -package aws - -import ( - "fmt" - "testing" - - "github.com/hashicorp/terraform/helper/acctest" - "github.com/hashicorp/terraform/helper/resource" -) - -func TestAccDataSourceAWSALB_basic(t *testing.T) { - albName := fmt.Sprintf("testaccawsalb-basic-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: testAccDataSourceAWSALBConfigBasic(albName), - Check: resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_arn", "name", albName), - resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_arn", "internal", "true"), - resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_arn", "subnets.#", "2"), - resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_arn", "security_groups.#", "1"), - resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_arn", "tags.%", "1"), - resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_arn", "tags.TestName", "TestAccAWSALB_basic"), - resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_arn", "enable_deletion_protection", "false"), - resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_arn", "idle_timeout", "30"), - resource.TestCheckResourceAttrSet("data.aws_alb.alb_test_with_arn", "vpc_id"), - resource.TestCheckResourceAttrSet("data.aws_alb.alb_test_with_arn", "zone_id"), - resource.TestCheckResourceAttrSet("data.aws_alb.alb_test_with_arn", "dns_name"), - resource.TestCheckResourceAttrSet("data.aws_alb.alb_test_with_arn", "arn"), - resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_name", "name", albName), - resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_name", "internal", "true"), - resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_name", "subnets.#", "2"), - resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_name", "security_groups.#", "1"), - resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_name", "tags.%", "1"), - resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_name", "tags.TestName", "TestAccAWSALB_basic"), - resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_name", "enable_deletion_protection", "false"), - resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_name", "idle_timeout", "30"), - resource.TestCheckResourceAttrSet("data.aws_alb.alb_test_with_name", "vpc_id"), - resource.TestCheckResourceAttrSet("data.aws_alb.alb_test_with_name", "zone_id"), - resource.TestCheckResourceAttrSet("data.aws_alb.alb_test_with_name", "dns_name"), - resource.TestCheckResourceAttrSet("data.aws_alb.alb_test_with_name", "arn"), - ), - }, - }, - }) -} - -func testAccDataSourceAWSALBConfigBasic(albName string) string { - return fmt.Sprintf(`resource "aws_alb" "alb_test" { - name = "%s" - internal = true - security_groups = ["${aws_security_group.alb_test.id}"] - subnets = ["${aws_subnet.alb_test.*.id}"] - - idle_timeout = 30 - enable_deletion_protection = false - - tags { - TestName = "TestAccAWSALB_basic" - } -} - -variable "subnets" { - default = ["10.0.1.0/24", "10.0.2.0/24"] - type = "list" -} - -data "aws_availability_zones" "available" {} - -resource "aws_vpc" "alb_test" { - cidr_block = "10.0.0.0/16" - - tags { - TestName = "TestAccAWSALB_basic" - } -} - -resource "aws_subnet" "alb_test" { - count = 2 - vpc_id = "${aws_vpc.alb_test.id}" - cidr_block = "${element(var.subnets, count.index)}" - map_public_ip_on_launch = true - availability_zone = "${element(data.aws_availability_zones.available.names, count.index)}" - - tags { - TestName = "TestAccAWSALB_basic" - } -} - -resource "aws_security_group" "alb_test" { - name = "allow_all_alb_test" - description = "Used for ALB Testing" - vpc_id = "${aws_vpc.alb_test.id}" - - ingress { - from_port = 0 - to_port = 0 - protocol = "-1" - cidr_blocks = ["0.0.0.0/0"] - } - - egress { - from_port = 0 - to_port = 0 - protocol = "-1" - cidr_blocks = ["0.0.0.0/0"] - } - - tags { - TestName = "TestAccAWSALB_basic" - } -} - -data "aws_alb" "alb_test_with_arn" { - arn = "${aws_alb.alb_test.arn}" -} - -data "aws_alb" "alb_test_with_name" { - name = "${aws_alb.alb_test.name}" -}`, albName) -} diff --git a/aws/data_source_aws_alb.go b/aws/data_source_aws_lb.go similarity index 66% rename from aws/data_source_aws_alb.go rename to aws/data_source_aws_lb.go index d314e0ed7a6..2b3864112a7 100644 --- a/aws/data_source_aws_alb.go +++ b/aws/data_source_aws_lb.go @@ -9,9 +9,9 @@ import ( "github.com/hashicorp/terraform/helper/schema" ) -func dataSourceAwsAlb() *schema.Resource { +func dataSourceAwsLb() *schema.Resource { return &schema.Resource{ - Read: dataSourceAwsAlbRead, + Read: dataSourceAwsLbRead, Schema: map[string]*schema.Schema{ "arn": { Type: schema.TypeString, @@ -35,6 +35,11 @@ func dataSourceAwsAlb() *schema.Resource { Computed: true, }, + "load_balancer_type": { + Type: schema.TypeString, + Computed: true, + }, + "security_groups": { Type: schema.TypeSet, Elem: &schema.Schema{Type: schema.TypeString}, @@ -49,6 +54,23 @@ func dataSourceAwsAlb() *schema.Resource { Set: schema.HashString, }, + "subnet_mapping": { + Type: schema.TypeSet, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "subnet_id": { + Type: schema.TypeString, + Required: true, + }, + "allocation_id": { + Type: schema.TypeString, + Optional: true, + }, + }, + }, + }, + "access_logs": { Type: schema.TypeList, Computed: true, @@ -101,27 +123,27 @@ func dataSourceAwsAlb() *schema.Resource { } } -func dataSourceAwsAlbRead(d *schema.ResourceData, meta interface{}) error { +func dataSourceAwsLbRead(d *schema.ResourceData, meta interface{}) error { elbconn := meta.(*AWSClient).elbv2conn - albArn := d.Get("arn").(string) - albName := d.Get("name").(string) + lbArn := d.Get("arn").(string) + lbName := d.Get("name").(string) - describeAlbOpts := &elbv2.DescribeLoadBalancersInput{} + describeLbOpts := &elbv2.DescribeLoadBalancersInput{} switch { - case albArn != "": - describeAlbOpts.LoadBalancerArns = []*string{aws.String(albArn)} - case albName != "": - describeAlbOpts.Names = []*string{aws.String(albName)} + case lbArn != "": + describeLbOpts.LoadBalancerArns = []*string{aws.String(lbArn)} + case lbName != "": + describeLbOpts.Names = []*string{aws.String(lbName)} } - describeResp, err := elbconn.DescribeLoadBalancers(describeAlbOpts) + describeResp, err := elbconn.DescribeLoadBalancers(describeLbOpts) if err != nil { - return errwrap.Wrapf("Error retrieving ALB: {{err}}", err) + return errwrap.Wrapf("Error retrieving LB: {{err}}", err) } if len(describeResp.LoadBalancers) != 1 { return fmt.Errorf("Search returned %d results, please revise so only one is returned", len(describeResp.LoadBalancers)) } d.SetId(*describeResp.LoadBalancers[0].LoadBalancerArn) - return flattenAwsAlbResource(d, meta, describeResp.LoadBalancers[0]) + return flattenAwsLbResource(d, meta, describeResp.LoadBalancers[0]) } diff --git a/aws/data_source_aws_alb_listener.go b/aws/data_source_aws_lb_listener.go similarity index 81% rename from aws/data_source_aws_alb_listener.go rename to aws/data_source_aws_lb_listener.go index 63ec4ed1a1f..dd7570a9f2d 100644 --- a/aws/data_source_aws_alb_listener.go +++ b/aws/data_source_aws_lb_listener.go @@ -2,9 +2,9 @@ package aws import "github.com/hashicorp/terraform/helper/schema" -func dataSourceAwsAlbListener() *schema.Resource { +func dataSourceAwsLbListener() *schema.Resource { return &schema.Resource{ - Read: dataSourceAwsAlbListenerRead, + Read: dataSourceAwsLbListenerRead, Schema: map[string]*schema.Schema{ "arn": { @@ -56,7 +56,7 @@ func dataSourceAwsAlbListener() *schema.Resource { } } -func dataSourceAwsAlbListenerRead(d *schema.ResourceData, meta interface{}) error { +func dataSourceAwsLbListenerRead(d *schema.ResourceData, meta interface{}) error { d.SetId(d.Get("arn").(string)) - return resourceAwsAlbListenerRead(d, meta) + return resourceAwsLbListenerRead(d, meta) } diff --git a/aws/data_source_aws_alb_listener_test.go b/aws/data_source_aws_lb_listener_test.go similarity index 60% rename from aws/data_source_aws_alb_listener_test.go rename to aws/data_source_aws_lb_listener_test.go index 5eea80e24cb..dc668322933 100644 --- a/aws/data_source_aws_alb_listener_test.go +++ b/aws/data_source_aws_lb_listener_test.go @@ -10,8 +10,8 @@ import ( "github.com/hashicorp/terraform/helper/resource" ) -func TestAccDataSourceAWSALBListener_basic(t *testing.T) { - albName := fmt.Sprintf("testlistener-basic-%s", acctest.RandStringFromCharSet(13, acctest.CharSetAlphaNum)) +func TestAccDataSourceAWSLBListener_basic(t *testing.T) { + lbName := fmt.Sprintf("testlistener-basic-%s", acctest.RandStringFromCharSet(13, acctest.CharSetAlphaNum)) targetGroupName := fmt.Sprintf("testtargetgroup-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) resource.Test(t, resource.TestCase{ @@ -19,7 +19,31 @@ func TestAccDataSourceAWSALBListener_basic(t *testing.T) { Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccDataSourceAWSALBListenerConfigBasic(albName, targetGroupName), + Config: testAccDataSourceAWSLBListenerConfigBasic(lbName, targetGroupName), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttrSet("data.aws_lb_listener.front_end", "load_balancer_arn"), + resource.TestCheckResourceAttrSet("data.aws_lb_listener.front_end", "arn"), + resource.TestCheckResourceAttr("data.aws_lb_listener.front_end", "protocol", "HTTP"), + resource.TestCheckResourceAttr("data.aws_lb_listener.front_end", "port", "80"), + resource.TestCheckResourceAttr("data.aws_lb_listener.front_end", "default_action.#", "1"), + resource.TestCheckResourceAttr("data.aws_lb_listener.front_end", "default_action.0.type", "forward"), + resource.TestCheckResourceAttrSet("data.aws_lb_listener.front_end", "default_action.0.target_group_arn"), + ), + }, + }, + }) +} + +func TestAccDataSourceAWSLBListenerBackwardsCompatibility(t *testing.T) { + lbName := fmt.Sprintf("testlistener-basic-%s", acctest.RandStringFromCharSet(13, acctest.CharSetAlphaNum)) + targetGroupName := fmt.Sprintf("testtargetgroup-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAWSLBListenerConfigBackwardsCompatibility(lbName, targetGroupName), Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttrSet("data.aws_alb_listener.front_end", "load_balancer_arn"), resource.TestCheckResourceAttrSet("data.aws_alb_listener.front_end", "arn"), @@ -34,8 +58,8 @@ func TestAccDataSourceAWSALBListener_basic(t *testing.T) { }) } -func TestAccDataSourceAWSALBListener_https(t *testing.T) { - albName := fmt.Sprintf("testlistener-https-%s", acctest.RandStringFromCharSet(13, acctest.CharSetAlphaNum)) +func TestAccDataSourceAWSLBListener_https(t *testing.T) { + lbName := fmt.Sprintf("testlistener-https-%s", acctest.RandStringFromCharSet(13, acctest.CharSetAlphaNum)) targetGroupName := fmt.Sprintf("testtargetgroup-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) resource.Test(t, resource.TestCase{ @@ -43,24 +67,124 @@ func TestAccDataSourceAWSALBListener_https(t *testing.T) { Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccDataSourceAWSALBListenerConfigHTTPS(albName, targetGroupName), + Config: testAccDataSourceAWSLBListenerConfigHTTPS(lbName, targetGroupName), Check: resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttrSet("data.aws_alb_listener.front_end", "load_balancer_arn"), - resource.TestCheckResourceAttrSet("data.aws_alb_listener.front_end", "arn"), - resource.TestCheckResourceAttr("data.aws_alb_listener.front_end", "protocol", "HTTPS"), - resource.TestCheckResourceAttr("data.aws_alb_listener.front_end", "port", "443"), - resource.TestCheckResourceAttr("data.aws_alb_listener.front_end", "default_action.#", "1"), - resource.TestCheckResourceAttr("data.aws_alb_listener.front_end", "default_action.0.type", "forward"), - resource.TestCheckResourceAttrSet("data.aws_alb_listener.front_end", "default_action.0.target_group_arn"), - resource.TestCheckResourceAttrSet("data.aws_alb_listener.front_end", "certificate_arn"), - resource.TestCheckResourceAttr("data.aws_alb_listener.front_end", "ssl_policy", "ELBSecurityPolicy-2015-05"), + resource.TestCheckResourceAttrSet("data.aws_lb_listener.front_end", "load_balancer_arn"), + resource.TestCheckResourceAttrSet("data.aws_lb_listener.front_end", "arn"), + resource.TestCheckResourceAttr("data.aws_lb_listener.front_end", "protocol", "HTTPS"), + resource.TestCheckResourceAttr("data.aws_lb_listener.front_end", "port", "443"), + resource.TestCheckResourceAttr("data.aws_lb_listener.front_end", "default_action.#", "1"), + resource.TestCheckResourceAttr("data.aws_lb_listener.front_end", "default_action.0.type", "forward"), + resource.TestCheckResourceAttrSet("data.aws_lb_listener.front_end", "default_action.0.target_group_arn"), + resource.TestCheckResourceAttrSet("data.aws_lb_listener.front_end", "certificate_arn"), + resource.TestCheckResourceAttr("data.aws_lb_listener.front_end", "ssl_policy", "ELBSecurityPolicy-2015-05"), ), }, }, }) } -func testAccDataSourceAWSALBListenerConfigBasic(albName, targetGroupName string) string { +func testAccDataSourceAWSLBListenerConfigBasic(lbName, targetGroupName string) string { + return fmt.Sprintf(`resource "aws_lb_listener" "front_end" { + load_balancer_arn = "${aws_lb.alb_test.id}" + protocol = "HTTP" + port = "80" + + default_action { + target_group_arn = "${aws_lb_target_group.test.id}" + type = "forward" + } +} + +resource "aws_lb" "alb_test" { + name = "%s" + internal = true + security_groups = ["${aws_security_group.alb_test.id}"] + subnets = ["${aws_subnet.alb_test.*.id}"] + + idle_timeout = 30 + enable_deletion_protection = false + + tags { + TestName = "TestAccAWSALB_basic" + } +} + +resource "aws_lb_target_group" "test" { + name = "%s" + port = 8080 + protocol = "HTTP" + vpc_id = "${aws_vpc.alb_test.id}" + + health_check { + path = "/health" + interval = 60 + port = 8081 + protocol = "HTTP" + timeout = 3 + healthy_threshold = 3 + unhealthy_threshold = 3 + matcher = "200-299" + } +} + +variable "subnets" { + default = ["10.0.1.0/24", "10.0.2.0/24"] + type = "list" +} + +data "aws_availability_zones" "available" {} + +resource "aws_vpc" "alb_test" { + cidr_block = "10.0.0.0/16" + + tags { + TestName = "TestAccAWSALB_basic" + } +} + +resource "aws_subnet" "alb_test" { + count = 2 + vpc_id = "${aws_vpc.alb_test.id}" + cidr_block = "${element(var.subnets, count.index)}" + map_public_ip_on_launch = true + availability_zone = "${element(data.aws_availability_zones.available.names, count.index)}" + + tags { + TestName = "TestAccAWSALB_basic" + } +} + +resource "aws_security_group" "alb_test" { + name = "allow_all_alb_test" + description = "Used for ALB Testing" + vpc_id = "${aws_vpc.alb_test.id}" + + ingress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } + + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } + + tags { + TestName = "TestAccAWSALB_basic" + } +} + +data "aws_lb_listener" "front_end" { + arn = "${aws_lb_listener.front_end.arn}" +}`, lbName, targetGroupName) +} + +func testAccDataSourceAWSLBListenerConfigBackwardsCompatibility(lbName, targetGroupName string) string { return fmt.Sprintf(`resource "aws_alb_listener" "front_end" { load_balancer_arn = "${aws_alb.alb_test.id}" protocol = "HTTP" @@ -157,24 +281,24 @@ resource "aws_security_group" "alb_test" { data "aws_alb_listener" "front_end" { arn = "${aws_alb_listener.front_end.arn}" -}`, albName, targetGroupName) +}`, lbName, targetGroupName) } -func testAccDataSourceAWSALBListenerConfigHTTPS(albName, targetGroupName string) string { - return fmt.Sprintf(`resource "aws_alb_listener" "front_end" { - load_balancer_arn = "${aws_alb.alb_test.id}" +func testAccDataSourceAWSLBListenerConfigHTTPS(lbName, targetGroupName string) string { + return fmt.Sprintf(`resource "aws_lb_listener" "front_end" { + load_balancer_arn = "${aws_lb.alb_test.id}" protocol = "HTTPS" port = "443" ssl_policy = "ELBSecurityPolicy-2015-05" certificate_arn = "${aws_iam_server_certificate.test_cert.arn}" default_action { - target_group_arn = "${aws_alb_target_group.test.id}" + target_group_arn = "${aws_lb_target_group.test.id}" type = "forward" } } -resource "aws_alb" "alb_test" { +resource "aws_lb" "alb_test" { name = "%s" internal = false security_groups = ["${aws_security_group.alb_test.id}"] @@ -188,7 +312,7 @@ resource "aws_alb" "alb_test" { } } -resource "aws_alb_target_group" "test" { +resource "aws_lb_target_group" "test" { name = "%s" port = 8080 protocol = "HTTP" @@ -320,7 +444,7 @@ dg+Sd4Wjm89UQoUUoiIcstY7FPbqfBtYKfh4RYHAHV2BwDFqzZCM EOF } -data "aws_alb_listener" "front_end" { - arn = "${aws_alb_listener.front_end.arn}" -}`, albName, targetGroupName, rand.New(rand.NewSource(time.Now().UnixNano())).Int()) +data "aws_lb_listener" "front_end" { + arn = "${aws_lb_listener.front_end.arn}" +}`, lbName, targetGroupName, rand.New(rand.NewSource(time.Now().UnixNano())).Int()) } diff --git a/aws/data_source_aws_alb_target_group.go b/aws/data_source_aws_lb_target_group.go similarity index 89% rename from aws/data_source_aws_alb_target_group.go rename to aws/data_source_aws_lb_target_group.go index 82dd8e3040c..741772ce2ce 100644 --- a/aws/data_source_aws_alb_target_group.go +++ b/aws/data_source_aws_lb_target_group.go @@ -9,9 +9,9 @@ import ( "github.com/hashicorp/terraform/helper/schema" ) -func dataSourceAwsAlbTargetGroup() *schema.Resource { +func dataSourceAwsLbTargetGroup() *schema.Resource { return &schema.Resource{ - Read: dataSourceAwsAlbTargetGroupRead, + Read: dataSourceAwsLbTargetGroupRead, Schema: map[string]*schema.Schema{ "arn": { Type: schema.TypeString, @@ -125,7 +125,7 @@ func dataSourceAwsAlbTargetGroup() *schema.Resource { } } -func dataSourceAwsAlbTargetGroupRead(d *schema.ResourceData, meta interface{}) error { +func dataSourceAwsLbTargetGroupRead(d *schema.ResourceData, meta interface{}) error { elbconn := meta.(*AWSClient).elbv2conn tgArn := d.Get("arn").(string) tgName := d.Get("name").(string) @@ -140,7 +140,7 @@ func dataSourceAwsAlbTargetGroupRead(d *schema.ResourceData, meta interface{}) e describeResp, err := elbconn.DescribeTargetGroups(describeTgOpts) if err != nil { - return errwrap.Wrapf("Error retrieving ALB Target Group: {{err}}", err) + return errwrap.Wrapf("Error retrieving LB Target Group: {{err}}", err) } if len(describeResp.TargetGroups) != 1 { return fmt.Errorf("Search returned %d results, please revise so only one is returned", len(describeResp.TargetGroups)) @@ -149,5 +149,5 @@ func dataSourceAwsAlbTargetGroupRead(d *schema.ResourceData, meta interface{}) e targetGroup := describeResp.TargetGroups[0] d.SetId(*targetGroup.TargetGroupArn) - return flattenAwsAlbTargetGroupResource(d, meta, targetGroup) + return flattenAwsLbTargetGroupResource(d, meta, targetGroup) } diff --git a/aws/data_source_aws_lb_target_group_test.go b/aws/data_source_aws_lb_target_group_test.go new file mode 100644 index 00000000000..8db27e31f5c --- /dev/null +++ b/aws/data_source_aws_lb_target_group_test.go @@ -0,0 +1,335 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccDataSourceAWSALBTargetGroup_basic(t *testing.T) { + lbName := fmt.Sprintf("testlb-%s", acctest.RandStringFromCharSet(13, acctest.CharSetAlphaNum)) + targetGroupName := fmt.Sprintf("testtargetgroup-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAWSLBTargetGroupConfigBasic(lbName, targetGroupName), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_arn", "name", targetGroupName), + resource.TestCheckResourceAttrSet("data.aws_lb_target_group.alb_tg_test_with_arn", "arn"), + resource.TestCheckResourceAttrSet("data.aws_lb_target_group.alb_tg_test_with_arn", "arn_suffix"), + resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_arn", "port", "8080"), + resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_arn", "protocol", "HTTP"), + resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_arn", "protocol", "HTTP"), + resource.TestCheckResourceAttrSet("data.aws_lb_target_group.alb_tg_test_with_arn", "vpc_id"), + resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_arn", "deregistration_delay", "300"), + resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_arn", "tags.%", "1"), + resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_arn", "tags.TestName", "TestAccDataSourceAWSALBTargetGroup_basic"), + resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_arn", "stickiness.#", "1"), + resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_arn", "health_check.#", "1"), + resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_arn", "health_check.0.path", "/health"), + resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_arn", "health_check.0.port", "8081"), + resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_arn", "health_check.0.protocol", "HTTP"), + resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_arn", "health_check.0.timeout", "3"), + resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_arn", "health_check.0.healthy_threshold", "3"), + resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_arn", "health_check.0.unhealthy_threshold", "3"), + resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_arn", "health_check.0.matcher", "200-299"), + + resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_name", "name", targetGroupName), + resource.TestCheckResourceAttrSet("data.aws_lb_target_group.alb_tg_test_with_name", "arn"), + resource.TestCheckResourceAttrSet("data.aws_lb_target_group.alb_tg_test_with_name", "arn_suffix"), + resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_name", "port", "8080"), + resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_name", "protocol", "HTTP"), + resource.TestCheckResourceAttrSet("data.aws_lb_target_group.alb_tg_test_with_name", "vpc_id"), + resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_name", "deregistration_delay", "300"), + resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_name", "tags.%", "1"), + resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_name", "tags.TestName", "TestAccDataSourceAWSALBTargetGroup_basic"), + resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_name", "stickiness.#", "1"), + resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_name", "health_check.#", "1"), + resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_name", "health_check.0.path", "/health"), + resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_name", "health_check.0.port", "8081"), + resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_name", "health_check.0.protocol", "HTTP"), + resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_name", "health_check.0.timeout", "3"), + resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_name", "health_check.0.healthy_threshold", "3"), + resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_name", "health_check.0.unhealthy_threshold", "3"), + resource.TestCheckResourceAttr("data.aws_lb_target_group.alb_tg_test_with_name", "health_check.0.matcher", "200-299"), + ), + }, + }, + }) +} + +func TestAccDataSourceAWSLBTargetGroupBackwardsCompatibility(t *testing.T) { + lbName := fmt.Sprintf("testlb-%s", acctest.RandStringFromCharSet(13, acctest.CharSetAlphaNum)) + targetGroupName := fmt.Sprintf("testtargetgroup-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAWSLBTargetGroupConfigBackwardsCompatibility(lbName, targetGroupName), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "name", targetGroupName), + resource.TestCheckResourceAttrSet("data.aws_alb_target_group.alb_tg_test_with_arn", "arn"), + resource.TestCheckResourceAttrSet("data.aws_alb_target_group.alb_tg_test_with_arn", "arn_suffix"), + resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "port", "8080"), + resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "protocol", "HTTP"), + resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "protocol", "HTTP"), + resource.TestCheckResourceAttrSet("data.aws_alb_target_group.alb_tg_test_with_arn", "vpc_id"), + resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "deregistration_delay", "300"), + resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "tags.%", "1"), + resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "tags.TestName", "TestAccDataSourceAWSALBTargetGroup_basic"), + resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "stickiness.#", "1"), + resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "health_check.#", "1"), + resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "health_check.0.path", "/health"), + resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "health_check.0.port", "8081"), + resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "health_check.0.protocol", "HTTP"), + resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "health_check.0.timeout", "3"), + resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "health_check.0.healthy_threshold", "3"), + resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "health_check.0.unhealthy_threshold", "3"), + resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_arn", "health_check.0.matcher", "200-299"), + + resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "name", targetGroupName), + resource.TestCheckResourceAttrSet("data.aws_alb_target_group.alb_tg_test_with_name", "arn"), + resource.TestCheckResourceAttrSet("data.aws_alb_target_group.alb_tg_test_with_name", "arn_suffix"), + resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "port", "8080"), + resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "protocol", "HTTP"), + resource.TestCheckResourceAttrSet("data.aws_alb_target_group.alb_tg_test_with_name", "vpc_id"), + resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "deregistration_delay", "300"), + resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "tags.%", "1"), + resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "tags.TestName", "TestAccDataSourceAWSALBTargetGroup_basic"), + resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "stickiness.#", "1"), + resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "health_check.#", "1"), + resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "health_check.0.path", "/health"), + resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "health_check.0.port", "8081"), + resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "health_check.0.protocol", "HTTP"), + resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "health_check.0.timeout", "3"), + resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "health_check.0.healthy_threshold", "3"), + resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "health_check.0.unhealthy_threshold", "3"), + resource.TestCheckResourceAttr("data.aws_alb_target_group.alb_tg_test_with_name", "health_check.0.matcher", "200-299"), + ), + }, + }, + }) +} + +func testAccDataSourceAWSLBTargetGroupConfigBasic(lbName string, targetGroupName string) string { + return fmt.Sprintf(`resource "aws_lb_listener" "front_end" { + load_balancer_arn = "${aws_lb.alb_test.id}" + protocol = "HTTP" + port = "80" + + default_action { + target_group_arn = "${aws_lb_target_group.test.id}" + type = "forward" + } +} + +resource "aws_lb" "alb_test" { + name = "%s" + internal = true + security_groups = ["${aws_security_group.alb_test.id}"] + subnets = ["${aws_subnet.alb_test.*.id}"] + + idle_timeout = 30 + enable_deletion_protection = false + + tags { + TestName = "TestAccDataSourceAWSALBTargetGroup_basic" + } +} + +resource "aws_lb_target_group" "test" { + name = "%s" + port = 8080 + protocol = "HTTP" + vpc_id = "${aws_vpc.alb_test.id}" + + health_check { + path = "/health" + interval = 60 + port = 8081 + protocol = "HTTP" + timeout = 3 + healthy_threshold = 3 + unhealthy_threshold = 3 + matcher = "200-299" + } + + tags { + TestName = "TestAccDataSourceAWSALBTargetGroup_basic" + } +} + +variable "subnets" { + default = ["10.0.1.0/24", "10.0.2.0/24"] + type = "list" +} + +data "aws_availability_zones" "available" {} + +resource "aws_vpc" "alb_test" { + cidr_block = "10.0.0.0/16" + + tags { + TestName = "TestAccDataSourceAWSALBTargetGroup_basic" + } +} + +resource "aws_subnet" "alb_test" { + count = 2 + vpc_id = "${aws_vpc.alb_test.id}" + cidr_block = "${element(var.subnets, count.index)}" + map_public_ip_on_launch = true + availability_zone = "${element(data.aws_availability_zones.available.names, count.index)}" + + tags { + TestName = "TestAccDataSourceAWSALBTargetGroup_basic" + } +} + +resource "aws_security_group" "alb_test" { + name = "allow_all_alb_test" + description = "Used for ALB Testing" + vpc_id = "${aws_vpc.alb_test.id}" + + ingress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } + + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } + + tags { + TestName = "TestAccDataSourceAWSALBTargetGroup_basic" + } +} + +data "aws_lb_target_group" "alb_tg_test_with_arn" { + arn = "${aws_lb_target_group.test.arn}" +} + +data "aws_lb_target_group" "alb_tg_test_with_name" { + name = "${aws_lb_target_group.test.name}" +}`, lbName, targetGroupName) +} + +func testAccDataSourceAWSLBTargetGroupConfigBackwardsCompatibility(lbName string, targetGroupName string) string { + return fmt.Sprintf(`resource "aws_alb_listener" "front_end" { + load_balancer_arn = "${aws_alb.alb_test.id}" + protocol = "HTTP" + port = "80" + + default_action { + target_group_arn = "${aws_alb_target_group.test.id}" + type = "forward" + } +} + +resource "aws_alb" "alb_test" { + name = "%s" + internal = true + security_groups = ["${aws_security_group.alb_test.id}"] + subnets = ["${aws_subnet.alb_test.*.id}"] + + idle_timeout = 30 + enable_deletion_protection = false + + tags { + TestName = "TestAccDataSourceAWSALBTargetGroup_basic" + } +} + +resource "aws_alb_target_group" "test" { + name = "%s" + port = 8080 + protocol = "HTTP" + vpc_id = "${aws_vpc.alb_test.id}" + + health_check { + path = "/health" + interval = 60 + port = 8081 + protocol = "HTTP" + timeout = 3 + healthy_threshold = 3 + unhealthy_threshold = 3 + matcher = "200-299" + } + + tags { + TestName = "TestAccDataSourceAWSALBTargetGroup_basic" + } +} + +variable "subnets" { + default = ["10.0.1.0/24", "10.0.2.0/24"] + type = "list" +} + +data "aws_availability_zones" "available" {} + +resource "aws_vpc" "alb_test" { + cidr_block = "10.0.0.0/16" + + tags { + TestName = "TestAccDataSourceAWSALBTargetGroup_basic" + } +} + +resource "aws_subnet" "alb_test" { + count = 2 + vpc_id = "${aws_vpc.alb_test.id}" + cidr_block = "${element(var.subnets, count.index)}" + map_public_ip_on_launch = true + availability_zone = "${element(data.aws_availability_zones.available.names, count.index)}" + + tags { + TestName = "TestAccDataSourceAWSALBTargetGroup_basic" + } +} + +resource "aws_security_group" "alb_test" { + name = "allow_all_alb_test" + description = "Used for ALB Testing" + vpc_id = "${aws_vpc.alb_test.id}" + + ingress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } + + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } + + tags { + TestName = "TestAccDataSourceAWSALBTargetGroup_basic" + } +} + +data "aws_alb_target_group" "alb_tg_test_with_arn" { + arn = "${aws_alb_target_group.test.arn}" +} + +data "aws_alb_target_group" "alb_tg_test_with_name" { + name = "${aws_alb_target_group.test.name}" +}`, lbName, targetGroupName) +} diff --git a/aws/data_source_aws_lb_test.go b/aws/data_source_aws_lb_test.go new file mode 100644 index 00000000000..b5412793f5d --- /dev/null +++ b/aws/data_source_aws_lb_test.go @@ -0,0 +1,239 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccDataSourceAWSLB_basic(t *testing.T) { + lbName := fmt.Sprintf("testaccawslb-basic-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAWSLBConfigBasic(lbName), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("data.aws_lb.alb_test_with_arn", "name", lbName), + resource.TestCheckResourceAttr("data.aws_lb.alb_test_with_arn", "internal", "true"), + resource.TestCheckResourceAttr("data.aws_lb.alb_test_with_arn", "subnets.#", "2"), + resource.TestCheckResourceAttr("data.aws_lb.alb_test_with_arn", "security_groups.#", "1"), + resource.TestCheckResourceAttr("data.aws_lb.alb_test_with_arn", "tags.%", "1"), + resource.TestCheckResourceAttr("data.aws_lb.alb_test_with_arn", "tags.TestName", "TestAccAWSALB_basic"), + resource.TestCheckResourceAttr("data.aws_lb.alb_test_with_arn", "enable_deletion_protection", "false"), + resource.TestCheckResourceAttr("data.aws_lb.alb_test_with_arn", "idle_timeout", "30"), + resource.TestCheckResourceAttrSet("data.aws_lb.alb_test_with_arn", "vpc_id"), + resource.TestCheckResourceAttrSet("data.aws_lb.alb_test_with_arn", "zone_id"), + resource.TestCheckResourceAttrSet("data.aws_lb.alb_test_with_arn", "dns_name"), + resource.TestCheckResourceAttrSet("data.aws_lb.alb_test_with_arn", "arn"), + resource.TestCheckResourceAttr("data.aws_lb.alb_test_with_name", "name", lbName), + resource.TestCheckResourceAttr("data.aws_lb.alb_test_with_name", "internal", "true"), + resource.TestCheckResourceAttr("data.aws_lb.alb_test_with_name", "subnets.#", "2"), + resource.TestCheckResourceAttr("data.aws_lb.alb_test_with_name", "security_groups.#", "1"), + resource.TestCheckResourceAttr("data.aws_lb.alb_test_with_name", "tags.%", "1"), + resource.TestCheckResourceAttr("data.aws_lb.alb_test_with_name", "tags.TestName", "TestAccAWSALB_basic"), + resource.TestCheckResourceAttr("data.aws_lb.alb_test_with_name", "enable_deletion_protection", "false"), + resource.TestCheckResourceAttr("data.aws_lb.alb_test_with_name", "idle_timeout", "30"), + resource.TestCheckResourceAttrSet("data.aws_lb.alb_test_with_name", "vpc_id"), + resource.TestCheckResourceAttrSet("data.aws_lb.alb_test_with_name", "zone_id"), + resource.TestCheckResourceAttrSet("data.aws_lb.alb_test_with_name", "dns_name"), + resource.TestCheckResourceAttrSet("data.aws_lb.alb_test_with_name", "arn"), + ), + }, + }, + }) +} + +func TestAccDataSourceAWSLBBackwardsCompatibility(t *testing.T) { + lbName := fmt.Sprintf("testaccawsalb-basic-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAWSLBConfigBackardsCompatibility(lbName), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_arn", "name", lbName), + resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_arn", "internal", "true"), + resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_arn", "subnets.#", "2"), + resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_arn", "security_groups.#", "1"), + resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_arn", "tags.%", "1"), + resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_arn", "tags.TestName", "TestAccAWSALB_basic"), + resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_arn", "enable_deletion_protection", "false"), + resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_arn", "idle_timeout", "30"), + resource.TestCheckResourceAttrSet("data.aws_alb.alb_test_with_arn", "vpc_id"), + resource.TestCheckResourceAttrSet("data.aws_alb.alb_test_with_arn", "zone_id"), + resource.TestCheckResourceAttrSet("data.aws_alb.alb_test_with_arn", "dns_name"), + resource.TestCheckResourceAttrSet("data.aws_alb.alb_test_with_arn", "arn"), + resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_name", "name", lbName), + resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_name", "internal", "true"), + resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_name", "subnets.#", "2"), + resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_name", "security_groups.#", "1"), + resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_name", "tags.%", "1"), + resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_name", "tags.TestName", "TestAccAWSALB_basic"), + resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_name", "enable_deletion_protection", "false"), + resource.TestCheckResourceAttr("data.aws_alb.alb_test_with_name", "idle_timeout", "30"), + resource.TestCheckResourceAttrSet("data.aws_alb.alb_test_with_name", "vpc_id"), + resource.TestCheckResourceAttrSet("data.aws_alb.alb_test_with_name", "zone_id"), + resource.TestCheckResourceAttrSet("data.aws_alb.alb_test_with_name", "dns_name"), + resource.TestCheckResourceAttrSet("data.aws_alb.alb_test_with_name", "arn"), + ), + }, + }, + }) +} + +func testAccDataSourceAWSLBConfigBasic(lbName string) string { + return fmt.Sprintf(`resource "aws_lb" "alb_test" { + name = "%s" + internal = true + security_groups = ["${aws_security_group.alb_test.id}"] + subnets = ["${aws_subnet.alb_test.*.id}"] + + idle_timeout = 30 + enable_deletion_protection = false + + tags { + TestName = "TestAccAWSALB_basic" + } +} + +variable "subnets" { + default = ["10.0.1.0/24", "10.0.2.0/24"] + type = "list" +} + +data "aws_availability_zones" "available" {} + +resource "aws_vpc" "alb_test" { + cidr_block = "10.0.0.0/16" + + tags { + TestName = "TestAccAWSALB_basic" + } +} + +resource "aws_subnet" "alb_test" { + count = 2 + vpc_id = "${aws_vpc.alb_test.id}" + cidr_block = "${element(var.subnets, count.index)}" + map_public_ip_on_launch = true + availability_zone = "${element(data.aws_availability_zones.available.names, count.index)}" + + tags { + TestName = "TestAccAWSALB_basic" + } +} + +resource "aws_security_group" "alb_test" { + name = "allow_all_alb_test" + description = "Used for ALB Testing" + vpc_id = "${aws_vpc.alb_test.id}" + + ingress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } + + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } + + tags { + TestName = "TestAccAWSALB_basic" + } +} + +data "aws_lb" "alb_test_with_arn" { + arn = "${aws_lb.alb_test.arn}" +} + +data "aws_lb" "alb_test_with_name" { + name = "${aws_lb.alb_test.name}" +}`, lbName) +} + +func testAccDataSourceAWSLBConfigBackardsCompatibility(albName string) string { + return fmt.Sprintf(`resource "aws_alb" "alb_test" { + name = "%s" + internal = true + security_groups = ["${aws_security_group.alb_test.id}"] + subnets = ["${aws_subnet.alb_test.*.id}"] + + idle_timeout = 30 + enable_deletion_protection = false + + tags { + TestName = "TestAccAWSALB_basic" + } +} + +variable "subnets" { + default = ["10.0.1.0/24", "10.0.2.0/24"] + type = "list" +} + +data "aws_availability_zones" "available" {} + +resource "aws_vpc" "alb_test" { + cidr_block = "10.0.0.0/16" + + tags { + TestName = "TestAccAWSALB_basic" + } +} + +resource "aws_subnet" "alb_test" { + count = 2 + vpc_id = "${aws_vpc.alb_test.id}" + cidr_block = "${element(var.subnets, count.index)}" + map_public_ip_on_launch = true + availability_zone = "${element(data.aws_availability_zones.available.names, count.index)}" + + tags { + TestName = "TestAccAWSALB_basic" + } +} + +resource "aws_security_group" "alb_test" { + name = "allow_all_alb_test" + description = "Used for ALB Testing" + vpc_id = "${aws_vpc.alb_test.id}" + + ingress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } + + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } + + tags { + TestName = "TestAccAWSALB_basic" + } +} + +data "aws_alb" "alb_test_with_arn" { + arn = "${aws_alb.alb_test.arn}" +} + +data "aws_alb" "alb_test_with_name" { + name = "${aws_alb.alb_test.name}" +}`, albName) +} diff --git a/aws/provider.go b/aws/provider.go index a2cc49544d3..795893d3332 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -161,9 +161,6 @@ func Provider() terraform.ResourceProvider { DataSourcesMap: map[string]*schema.Resource{ "aws_acm_certificate": dataSourceAwsAcmCertificate(), - "aws_alb": dataSourceAwsAlb(), - "aws_alb_listener": dataSourceAwsAlbListener(), - "aws_alb_target_group": dataSourceAwsAlbTargetGroup(), "aws_ami": dataSourceAwsAmi(), "aws_ami_ids": dataSourceAwsAmiIds(), "aws_autoscaling_groups": dataSourceAwsAutoscalingGroups(), @@ -219,14 +216,17 @@ func Provider() terraform.ResourceProvider { "aws_vpc_endpoint_service": dataSourceAwsVpcEndpointService(), "aws_vpc_peering_connection": dataSourceAwsVpcPeeringConnection(), "aws_vpn_gateway": dataSourceAwsVpnGateway(), + + // Adding the Aliases for the ALB -> LB Rename + "aws_lb": dataSourceAwsLb(), + "aws_alb": dataSourceAwsLb(), + "aws_lb_listener": dataSourceAwsLbListener(), + "aws_alb_listener": dataSourceAwsLbListener(), + "aws_lb_target_group": dataSourceAwsLbTargetGroup(), + "aws_alb_target_group": dataSourceAwsLbTargetGroup(), }, ResourcesMap: map[string]*schema.Resource{ - "aws_alb": resourceAwsAlb(), - "aws_alb_listener": resourceAwsAlbListener(), - "aws_alb_listener_rule": resourceAwsAlbListenerRule(), - "aws_alb_target_group": resourceAwsAlbTargetGroup(), - "aws_alb_target_group_attachment": resourceAwsAlbTargetGroupAttachment(), "aws_ami": resourceAwsAmi(), "aws_ami_copy": resourceAwsAmiCopy(), "aws_ami_from_instance": resourceAwsAmiFromInstance(), @@ -489,6 +489,21 @@ func Provider() terraform.ResourceProvider { "aws_batch_compute_environment": resourceAwsBatchComputeEnvironment(), "aws_batch_job_definition": resourceAwsBatchJobDefinition(), "aws_batch_job_queue": resourceAwsBatchJobQueue(), + + // ALBs are actually LBs because they can be type `network` or `application` + // To avoid regressions, we will add a new resource for each and they both point + // back to the old ALB version. IF the Terraform supported aliases for resources + // this would be a whole lot simplier + "aws_alb": resourceAwsLb(), + "aws_lb": resourceAwsLb(), + "aws_alb_listener": resourceAwsLbListener(), + "aws_lb_listener": resourceAwsLbListener(), + "aws_alb_listener_rule": resourceAwsLbbListenerRule(), + "aws_lb_listener_rule": resourceAwsLbbListenerRule(), + "aws_alb_target_group": resourceAwsLbTargetGroup(), + "aws_lb_target_group": resourceAwsLbTargetGroup(), + "aws_alb_target_group_attachment": resourceAwsLbTargetGroupAttachment(), + "aws_lb_target_group_attachment": resourceAwsLbTargetGroupAttachment(), }, ConfigureFunc: providerConfigure, } diff --git a/aws/resource_aws_alb_target_group_test.go b/aws/resource_aws_alb_target_group_test.go deleted file mode 100644 index dc800cfd8b2..00000000000 --- a/aws/resource_aws_alb_target_group_test.go +++ /dev/null @@ -1,788 +0,0 @@ -package aws - -import ( - "errors" - "fmt" - "regexp" - "testing" - - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/elbv2" - "github.com/hashicorp/errwrap" - "github.com/hashicorp/terraform/helper/acctest" - "github.com/hashicorp/terraform/helper/resource" - "github.com/hashicorp/terraform/terraform" -) - -func TestALBTargetGroupCloudwatchSuffixFromARN(t *testing.T) { - cases := []struct { - name string - arn *string - suffix string - }{ - { - name: "valid suffix", - arn: aws.String(`arn:aws:elasticloadbalancing:us-east-1:123456:targetgroup/my-targets/73e2d6bc24d8a067`), - suffix: `targetgroup/my-targets/73e2d6bc24d8a067`, - }, - { - name: "no suffix", - arn: aws.String(`arn:aws:elasticloadbalancing:us-east-1:123456:targetgroup`), - suffix: ``, - }, - { - name: "nil ARN", - arn: nil, - suffix: ``, - }, - } - - for _, tc := range cases { - actual := albTargetGroupSuffixFromARN(tc.arn) - if actual != tc.suffix { - t.Fatalf("bad suffix: %q\nExpected: %s\n Got: %s", tc.name, tc.suffix, actual) - } - } -} - -func TestAccAWSALBTargetGroup_basic(t *testing.T) { - var conf elbv2.TargetGroup - targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - IDRefreshName: "aws_alb_target_group.test", - Providers: testAccProviders, - CheckDestroy: testAccCheckAWSALBTargetGroupDestroy, - Steps: []resource.TestStep{ - { - Config: testAccAWSALBTargetGroupConfig_basic(targetGroupName), - Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf), - resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "arn"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupName), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "443"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTPS"), - resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "vpc_id"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "deregistration_delay", "200"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.#", "1"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.enabled", "true"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.type", "lb_cookie"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.cookie_duration", "10000"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.#", "1"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.path", "/health"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.interval", "60"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.port", "8081"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.protocol", "HTTP"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.timeout", "3"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.healthy_threshold", "3"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.unhealthy_threshold", "3"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.matcher", "200-299"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "tags.%", "1"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "tags.TestName", "TestAccAWSALBTargetGroup_basic"), - ), - }, - }, - }) -} - -func TestAccAWSALBTargetGroup_namePrefix(t *testing.T) { - var conf elbv2.TargetGroup - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - IDRefreshName: "aws_alb_target_group.test", - Providers: testAccProviders, - CheckDestroy: testAccCheckAWSALBTargetGroupDestroy, - Steps: []resource.TestStep{ - { - Config: testAccAWSALBTargetGroupConfig_namePrefix, - Check: resource.ComposeTestCheckFunc( - testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf), - resource.TestMatchResourceAttr("aws_alb_target_group.test", "name", regexp.MustCompile("^tf-")), - ), - }, - }, - }) -} - -func TestAccAWSALBTargetGroup_generatedName(t *testing.T) { - var conf elbv2.TargetGroup - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - IDRefreshName: "aws_alb_target_group.test", - Providers: testAccProviders, - CheckDestroy: testAccCheckAWSALBTargetGroupDestroy, - Steps: []resource.TestStep{ - { - Config: testAccAWSALBTargetGroupConfig_generatedName, - Check: resource.ComposeTestCheckFunc( - testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf), - ), - }, - }, - }) -} - -func TestAccAWSALBTargetGroup_changeNameForceNew(t *testing.T) { - var before, after elbv2.TargetGroup - targetGroupNameBefore := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) - targetGroupNameAfter := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(4, acctest.CharSetAlphaNum)) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - IDRefreshName: "aws_alb_target_group.test", - Providers: testAccProviders, - CheckDestroy: testAccCheckAWSALBTargetGroupDestroy, - Steps: []resource.TestStep{ - { - Config: testAccAWSALBTargetGroupConfig_basic(targetGroupNameBefore), - Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &before), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupNameBefore), - ), - }, - { - Config: testAccAWSALBTargetGroupConfig_basic(targetGroupNameAfter), - Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &after), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupNameAfter), - ), - }, - }, - }) -} - -func TestAccAWSALBTargetGroup_changeProtocolForceNew(t *testing.T) { - var before, after elbv2.TargetGroup - targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - IDRefreshName: "aws_alb_target_group.test", - Providers: testAccProviders, - CheckDestroy: testAccCheckAWSALBTargetGroupDestroy, - Steps: []resource.TestStep{ - { - Config: testAccAWSALBTargetGroupConfig_basic(targetGroupName), - Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &before), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTPS"), - ), - }, - { - Config: testAccAWSALBTargetGroupConfig_updatedProtocol(targetGroupName), - Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &after), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTP"), - ), - }, - }, - }) -} - -func TestAccAWSALBTargetGroup_changePortForceNew(t *testing.T) { - var before, after elbv2.TargetGroup - targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - IDRefreshName: "aws_alb_target_group.test", - Providers: testAccProviders, - CheckDestroy: testAccCheckAWSALBTargetGroupDestroy, - Steps: []resource.TestStep{ - { - Config: testAccAWSALBTargetGroupConfig_basic(targetGroupName), - Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &before), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "443"), - ), - }, - { - Config: testAccAWSALBTargetGroupConfig_updatedPort(targetGroupName), - Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &after), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "442"), - ), - }, - }, - }) -} - -func TestAccAWSALBTargetGroup_changeVpcForceNew(t *testing.T) { - var before, after elbv2.TargetGroup - targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - IDRefreshName: "aws_alb_target_group.test", - Providers: testAccProviders, - CheckDestroy: testAccCheckAWSALBTargetGroupDestroy, - Steps: []resource.TestStep{ - { - Config: testAccAWSALBTargetGroupConfig_basic(targetGroupName), - Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &before), - ), - }, - { - Config: testAccAWSALBTargetGroupConfig_updatedVpc(targetGroupName), - Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &after), - ), - }, - }, - }) -} - -func TestAccAWSALBTargetGroup_tags(t *testing.T) { - var conf elbv2.TargetGroup - targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - IDRefreshName: "aws_alb_target_group.test", - Providers: testAccProviders, - CheckDestroy: testAccCheckAWSALBTargetGroupDestroy, - Steps: []resource.TestStep{ - { - Config: testAccAWSALBTargetGroupConfig_basic(targetGroupName), - Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "tags.%", "1"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "tags.TestName", "TestAccAWSALBTargetGroup_basic"), - ), - }, - { - Config: testAccAWSALBTargetGroupConfig_updateTags(targetGroupName), - Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "tags.%", "2"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "tags.Environment", "Production"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "tags.Type", "ALB Target Group"), - ), - }, - }, - }) -} - -func TestAccAWSALBTargetGroup_updateHealthCheck(t *testing.T) { - var conf elbv2.TargetGroup - targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - IDRefreshName: "aws_alb_target_group.test", - Providers: testAccProviders, - CheckDestroy: testAccCheckAWSALBTargetGroupDestroy, - Steps: []resource.TestStep{ - { - Config: testAccAWSALBTargetGroupConfig_basic(targetGroupName), - Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf), - resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "arn"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupName), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "443"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTPS"), - resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "vpc_id"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "deregistration_delay", "200"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.#", "1"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.type", "lb_cookie"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.cookie_duration", "10000"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.#", "1"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.path", "/health"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.interval", "60"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.port", "8081"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.protocol", "HTTP"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.timeout", "3"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.healthy_threshold", "3"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.unhealthy_threshold", "3"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.matcher", "200-299"), - ), - }, - { - Config: testAccAWSALBTargetGroupConfig_updateHealthCheck(targetGroupName), - Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf), - resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "arn"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupName), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "443"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTPS"), - resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "vpc_id"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "deregistration_delay", "200"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.#", "1"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.type", "lb_cookie"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.cookie_duration", "10000"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.#", "1"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.path", "/health2"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.interval", "30"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.port", "8082"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.protocol", "HTTPS"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.timeout", "4"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.healthy_threshold", "4"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.unhealthy_threshold", "4"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.matcher", "200"), - ), - }, - }, - }) -} - -func TestAccAWSALBTargetGroup_updateSticknessEnabled(t *testing.T) { - var conf elbv2.TargetGroup - targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - IDRefreshName: "aws_alb_target_group.test", - Providers: testAccProviders, - CheckDestroy: testAccCheckAWSALBTargetGroupDestroy, - Steps: []resource.TestStep{ - { - Config: testAccAWSALBTargetGroupConfig_stickiness(targetGroupName, false, false), - Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf), - resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "arn"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupName), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "443"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTPS"), - resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "vpc_id"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "deregistration_delay", "200"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.#", "1"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.path", "/health2"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.interval", "30"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.port", "8082"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.protocol", "HTTPS"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.timeout", "4"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.healthy_threshold", "4"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.unhealthy_threshold", "4"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.matcher", "200"), - ), - }, - { - Config: testAccAWSALBTargetGroupConfig_stickiness(targetGroupName, true, true), - Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf), - resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "arn"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupName), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "443"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTPS"), - resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "vpc_id"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "deregistration_delay", "200"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.#", "1"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.enabled", "true"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.type", "lb_cookie"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.cookie_duration", "10000"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.#", "1"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.path", "/health2"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.interval", "30"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.port", "8082"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.protocol", "HTTPS"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.timeout", "4"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.healthy_threshold", "4"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.unhealthy_threshold", "4"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.matcher", "200"), - ), - }, - { - Config: testAccAWSALBTargetGroupConfig_stickiness(targetGroupName, true, false), - Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf), - resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "arn"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupName), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "443"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTPS"), - resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "vpc_id"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "deregistration_delay", "200"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.#", "1"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.enabled", "false"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.type", "lb_cookie"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.cookie_duration", "10000"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.#", "1"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.path", "/health2"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.interval", "30"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.port", "8082"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.protocol", "HTTPS"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.timeout", "4"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.healthy_threshold", "4"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.unhealthy_threshold", "4"), - resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.matcher", "200"), - ), - }, - }, - }) -} - -func testAccCheckAWSALBTargetGroupExists(n string, res *elbv2.TargetGroup) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[n] - if !ok { - return fmt.Errorf("Not found: %s", n) - } - - if rs.Primary.ID == "" { - return errors.New("No Target Group ID is set") - } - - conn := testAccProvider.Meta().(*AWSClient).elbv2conn - - describe, err := conn.DescribeTargetGroups(&elbv2.DescribeTargetGroupsInput{ - TargetGroupArns: []*string{aws.String(rs.Primary.ID)}, - }) - - if err != nil { - return err - } - - if len(describe.TargetGroups) != 1 || - *describe.TargetGroups[0].TargetGroupArn != rs.Primary.ID { - return errors.New("Target Group not found") - } - - *res = *describe.TargetGroups[0] - return nil - } -} - -func testAccCheckAWSALBTargetGroupDestroy(s *terraform.State) error { - conn := testAccProvider.Meta().(*AWSClient).elbv2conn - - for _, rs := range s.RootModule().Resources { - if rs.Type != "aws_alb_target_group" { - continue - } - - describe, err := conn.DescribeTargetGroups(&elbv2.DescribeTargetGroupsInput{ - TargetGroupArns: []*string{aws.String(rs.Primary.ID)}, - }) - - if err == nil { - if len(describe.TargetGroups) != 0 && - *describe.TargetGroups[0].TargetGroupArn == rs.Primary.ID { - return fmt.Errorf("Target Group %q still exists", rs.Primary.ID) - } - } - - // Verify the error - if isTargetGroupNotFound(err) { - return nil - } else { - return errwrap.Wrapf("Unexpected error checking ALB destroyed: {{err}}", err) - } - } - - return nil -} - -func testAccAWSALBTargetGroupConfig_basic(targetGroupName string) string { - return fmt.Sprintf(`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 - port = 8081 - protocol = "HTTP" - timeout = 3 - healthy_threshold = 3 - unhealthy_threshold = 3 - matcher = "200-299" - } - - tags { - TestName = "TestAccAWSALBTargetGroup_basic" - } -} - -resource "aws_vpc" "test" { - cidr_block = "10.0.0.0/16" - - tags { - TestName = "TestAccAWSALBTargetGroup_basic" - } -}`, targetGroupName) -} - -func testAccAWSALBTargetGroupConfig_updatedPort(targetGroupName string) string { - return fmt.Sprintf(`resource "aws_alb_target_group" "test" { - name = "%s" - port = 442 - protocol = "HTTPS" - vpc_id = "${aws_vpc.test.id}" - - 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" - } - - tags { - TestName = "TestAccAWSALBTargetGroup_basic" - } -} - -resource "aws_vpc" "test" { - cidr_block = "10.0.0.0/16" - - tags { - TestName = "TestAccAWSALBTargetGroup_basic" - } -}`, targetGroupName) -} - -func testAccAWSALBTargetGroupConfig_updatedProtocol(targetGroupName string) string { - return fmt.Sprintf(`resource "aws_alb_target_group" "test" { - name = "%s" - port = 443 - protocol = "HTTP" - vpc_id = "${aws_vpc.test2.id}" - - 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" - } - - tags { - TestName = "TestAccAWSALBTargetGroup_basic" - } -} - -resource "aws_vpc" "test2" { - cidr_block = "10.10.0.0/16" - - tags { - TestName = "TestAccAWSALBTargetGroup_basic" - } -} - -resource "aws_vpc" "test" { - cidr_block = "10.0.0.0/16" - - tags { - TestName = "TestAccAWSALBTargetGroup_basic" - } -}`, targetGroupName) -} - -func testAccAWSALBTargetGroupConfig_updatedVpc(targetGroupName string) string { - return fmt.Sprintf(`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 - port = 8081 - protocol = "HTTP" - timeout = 3 - healthy_threshold = 3 - unhealthy_threshold = 3 - matcher = "200-299" - } - - tags { - TestName = "TestAccAWSALBTargetGroup_basic" - } -} - -resource "aws_vpc" "test" { - cidr_block = "10.0.0.0/16" - - tags { - TestName = "TestAccAWSALBTargetGroup_basic" - } -}`, targetGroupName) -} - -func testAccAWSALBTargetGroupConfig_updateTags(targetGroupName string) string { - return fmt.Sprintf(`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 - port = 8081 - protocol = "HTTP" - timeout = 3 - healthy_threshold = 3 - unhealthy_threshold = 3 - matcher = "200-299" - } - - tags { - Environment = "Production" - Type = "ALB Target Group" - } -} - -resource "aws_vpc" "test" { - cidr_block = "10.0.0.0/16" - - tags { - TestName = "TestAccAWSALBTargetGroup_basic" - } -}`, targetGroupName) -} - -func testAccAWSALBTargetGroupConfig_updateHealthCheck(targetGroupName string) string { - return fmt.Sprintf(`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 = "/health2" - interval = 30 - port = 8082 - protocol = "HTTPS" - timeout = 4 - healthy_threshold = 4 - unhealthy_threshold = 4 - matcher = "200" - } -} - -resource "aws_vpc" "test" { - cidr_block = "10.0.0.0/16" - - tags { - TestName = "TestAccAWSALBTargetGroup_basic" - } -}`, targetGroupName) -} - -func testAccAWSALBTargetGroupConfig_stickiness(targetGroupName string, addStickinessBlock bool, enabled bool) string { - var stickinessBlock string - - if addStickinessBlock { - stickinessBlock = fmt.Sprintf(`stickiness { - enabled = "%t" - type = "lb_cookie" - cookie_duration = 10000 - }`, enabled) - } - - return fmt.Sprintf(`resource "aws_alb_target_group" "test" { - name = "%s" - port = 443 - protocol = "HTTPS" - vpc_id = "${aws_vpc.test.id}" - - deregistration_delay = 200 - - %s - - health_check { - path = "/health2" - interval = 30 - port = 8082 - protocol = "HTTPS" - timeout = 4 - healthy_threshold = 4 - unhealthy_threshold = 4 - matcher = "200" - } -} - -resource "aws_vpc" "test" { - cidr_block = "10.0.0.0/16" - - tags { - TestName = "TestAccAWSALBTargetGroup_stickiness" - } -}`, targetGroupName, stickinessBlock) -} - -const testAccAWSALBTargetGroupConfig_namePrefix = ` -resource "aws_alb_target_group" "test" { - name_prefix = "tf-" - port = 80 - protocol = "HTTP" - vpc_id = "${aws_vpc.test.id}" -} - -resource "aws_vpc" "test" { - cidr_block = "10.0.0.0/16" - tags { - Name = "testAccAWSALBTargetGroupConfig_namePrefix" - } -} -` - -const testAccAWSALBTargetGroupConfig_generatedName = ` -resource "aws_alb_target_group" "test" { - port = 80 - protocol = "HTTP" - vpc_id = "${aws_vpc.test.id}" -} - -resource "aws_vpc" "test" { - cidr_block = "10.0.0.0/16" - tags { - Name = "testAccAWSALBTargetGroupConfig_generatedName" - } -} -` diff --git a/aws/resource_aws_autoscaling_attachment_test.go b/aws/resource_aws_autoscaling_attachment_test.go index 229cd41674e..d7ddec24cef 100644 --- a/aws/resource_aws_autoscaling_attachment_test.go +++ b/aws/resource_aws_autoscaling_attachment_test.go @@ -149,7 +149,7 @@ func testAccCheckAWSAutocalingAlbAttachmentExists(asgname string, targetGroupCou func testAccAWSAutoscalingAttachment_alb(rInt int) string { return fmt.Sprintf(` -resource "aws_alb_target_group" "test" { +resource "aws_lb_target_group" "test" { name = "test-alb-%d" port = 443 protocol = "HTTPS" @@ -174,11 +174,11 @@ resource "aws_alb_target_group" "test" { } tags { - TestName = "TestAccAWSALBTargetGroup_basic" + TestName = "TestAccAWSLBTargetGroup_basic" } } -resource "aws_alb_target_group" "another_test" { +resource "aws_lb_target_group" "another_test" { name = "atest-alb-%d" port = 443 protocol = "HTTPS" @@ -203,7 +203,7 @@ resource "aws_alb_target_group" "another_test" { } tags { - TestName = "TestAccAWSALBTargetGroup_basic" + TestName = "TestAccAWSLBTargetGroup_basic" } } @@ -234,7 +234,7 @@ resource "aws_vpc" "test" { cidr_block = "10.0.0.0/16" tags { - TestName = "TestAccAWSALBTargetGroup_basic" + TestName = "TestAccAWSLBTargetGroup_basic" } } `, rInt, rInt, rInt, rInt) @@ -300,7 +300,7 @@ func testAccAWSAutoscalingAttachment_alb_associated(rInt int) string { return testAccAWSAutoscalingAttachment_alb(rInt) + ` resource "aws_autoscaling_attachment" "asg_attachment_foo" { autoscaling_group_name = "${aws_autoscaling_group.asg.id}" - alb_target_group_arn = "${aws_alb_target_group.test.arn}" + alb_target_group_arn = "${aws_lb_target_group.test.arn}" }` } @@ -316,6 +316,6 @@ func testAccAWSAutoscalingAttachment_alb_double_associated(rInt int) string { return testAccAWSAutoscalingAttachment_alb_associated(rInt) + ` resource "aws_autoscaling_attachment" "asg_attachment_bar" { autoscaling_group_name = "${aws_autoscaling_group.asg.id}" - alb_target_group_arn = "${aws_alb_target_group.another_test.arn}" + alb_target_group_arn = "${aws_lb_target_group.another_test.arn}" }` } diff --git a/aws/resource_aws_autoscaling_group_test.go b/aws/resource_aws_autoscaling_group_test.go index d5b438b4077..e66993cf4aa 100644 --- a/aws/resource_aws_autoscaling_group_test.go +++ b/aws/resource_aws_autoscaling_group_test.go @@ -510,7 +510,7 @@ func TestAccAWSAutoScalingGroup_ALB_TargetGroups(t *testing.T) { Config: testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_pre, Check: resource.ComposeAggregateTestCheckFunc( testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), - testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &tg), + testAccCheckAWSLBTargetGroupExists("aws_lb_target_group.test", &tg), resource.TestCheckResourceAttr( "aws_autoscaling_group.bar", "target_group_arns.#", "0"), ), @@ -520,8 +520,8 @@ func TestAccAWSAutoScalingGroup_ALB_TargetGroups(t *testing.T) { Config: testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_post_duo, Check: resource.ComposeAggregateTestCheckFunc( testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), - testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &tg), - testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test_more", &tg2), + testAccCheckAWSLBTargetGroupExists("aws_lb_target_group.test", &tg), + testAccCheckAWSLBTargetGroupExists("aws_lb_target_group.test_more", &tg2), testCheck([]*elbv2.TargetGroup{&tg, &tg2}), resource.TestCheckResourceAttr( "aws_autoscaling_group.bar", "target_group_arns.#", "2"), @@ -532,7 +532,7 @@ func TestAccAWSAutoScalingGroup_ALB_TargetGroups(t *testing.T) { Config: testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_post, Check: resource.ComposeAggregateTestCheckFunc( testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), - testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &tg), + testAccCheckAWSLBTargetGroupExists("aws_lb_target_group.test", &tg), testCheck([]*elbv2.TargetGroup{&tg}), resource.TestCheckResourceAttr( "aws_autoscaling_group.bar", "target_group_arns.#", "1"), @@ -588,7 +588,7 @@ func TestAccAWSAutoScalingGroup_ALB_TargetGroups_ELBCapacity(t *testing.T) { Config: testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_ELBCapacity(rInt), Check: resource.ComposeAggregateTestCheckFunc( testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), - testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &tg), + testAccCheckAWSLBTargetGroupExists("aws_lb_target_group.test", &tg), testAccCheckAWSALBTargetGroupHealthy(&tg), ), }, @@ -1342,7 +1342,7 @@ resource "aws_vpc" "default" { } } -resource "aws_alb_target_group" "test" { +resource "aws_lb_target_group" "test" { name = "tf-example-alb-tg" port = 80 protocol = "HTTP" @@ -1424,7 +1424,7 @@ resource "aws_vpc" "default" { } } -resource "aws_alb_target_group" "test" { +resource "aws_lb_target_group" "test" { name = "tf-example-alb-tg" port = 80 protocol = "HTTP" @@ -1464,7 +1464,7 @@ resource "aws_autoscaling_group" "bar" { "${aws_subnet.alt.id}", ] - target_group_arns = ["${aws_alb_target_group.test.arn}"] + target_group_arns = ["${aws_lb_target_group.test.arn}"] max_size = 2 min_size = 0 @@ -1508,14 +1508,14 @@ resource "aws_vpc" "default" { } } -resource "aws_alb_target_group" "test" { +resource "aws_lb_target_group" "test" { name = "tf-example-alb-tg" port = 80 protocol = "HTTP" vpc_id = "${aws_vpc.default.id}" } -resource "aws_alb_target_group" "test_more" { +resource "aws_lb_target_group" "test_more" { name = "tf-example-alb-tg-more" port = 80 protocol = "HTTP" @@ -1556,8 +1556,8 @@ resource "aws_autoscaling_group" "bar" { ] target_group_arns = [ - "${aws_alb_target_group.test.arn}", - "${aws_alb_target_group.test_more.arn}", + "${aws_lb_target_group.test.arn}", + "${aws_lb_target_group.test_more.arn}", ] max_size = 2 @@ -1634,7 +1634,7 @@ resource "aws_vpc" "default" { } } -resource "aws_alb" "test_lb" { +resource "aws_lb" "test_lb" { subnets = ["${aws_subnet.main.id}", "${aws_subnet.alt.id}"] tags { @@ -1642,17 +1642,17 @@ resource "aws_alb" "test_lb" { } } -resource "aws_alb_listener" "test_listener" { - load_balancer_arn = "${aws_alb.test_lb.arn}" +resource "aws_lb_listener" "test_listener" { + load_balancer_arn = "${aws_lb.test_lb.arn}" port = "80" default_action { - target_group_arn = "${aws_alb_target_group.test.arn}" + target_group_arn = "${aws_lb_target_group.test.arn}" type = "forward" } } -resource "aws_alb_target_group" "test" { +resource "aws_lb_target_group" "test" { name = "tf-alb-test-%d" port = 80 protocol = "HTTP" @@ -1748,7 +1748,7 @@ resource "aws_autoscaling_group" "bar" { "${aws_subnet.alt.id}", ] - target_group_arns = ["${aws_alb_target_group.test.arn}"] + target_group_arns = ["${aws_lb_target_group.test.arn}"] max_size = 2 min_size = 2 diff --git a/aws/resource_aws_ecs_service_test.go b/aws/resource_aws_ecs_service_test.go index bc79d813ad1..2fb37f64955 100644 --- a/aws/resource_aws_ecs_service_test.go +++ b/aws/resource_aws_ecs_service_test.go @@ -1030,26 +1030,26 @@ resource "aws_iam_role_policy" "ecs_service" { EOF } -resource "aws_alb_target_group" "test" { +resource "aws_lb_target_group" "test" { name = "%s" port = 80 protocol = "HTTP" vpc_id = "${aws_vpc.main.id}" } -resource "aws_alb" "main" { +resource "aws_lb" "main" { name = "%s" internal = true subnets = ["${aws_subnet.main.*.id}"] } -resource "aws_alb_listener" "front_end" { - load_balancer_arn = "${aws_alb.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_alb_target_group.test.id}" + target_group_arn = "${aws_lb_target_group.test.id}" type = "forward" } } @@ -1062,14 +1062,14 @@ resource "aws_ecs_service" "with_alb" { iam_role = "${aws_iam_role.ecs_service.name}" load_balancer { - target_group_arn = "${aws_alb_target_group.test.id}" + target_group_arn = "${aws_lb_target_group.test.id}" container_name = "ghost" container_port = "2368" } depends_on = [ "aws_iam_role_policy.ecs_service", - "aws_alb_listener.front_end" + "aws_lb_listener.front_end" ] } `, rName, rName, rName, rName, rName, rName, rName) diff --git a/aws/resource_aws_alb.go b/aws/resource_aws_lb.go similarity index 74% rename from aws/resource_aws_alb.go rename to aws/resource_aws_lb.go index 6d074015cb4..8442a748aab 100644 --- a/aws/resource_aws_alb.go +++ b/aws/resource_aws_lb.go @@ -7,20 +7,23 @@ import ( "strconv" "time" + "bytes" + "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/elbv2" "github.com/hashicorp/errwrap" + "github.com/hashicorp/terraform/helper/hashcode" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" ) -func resourceAwsAlb() *schema.Resource { +func resourceAwsLb() *schema.Resource { return &schema.Resource{ - Create: resourceAwsAlbCreate, - Read: resourceAwsAlbRead, - Update: resourceAwsAlbUpdate, - Delete: resourceAwsAlbDelete, + Create: resourceAwsLbCreate, + Read: resoureAwsLbRead, + Update: resourceAwsLbUpdate, + Delete: resourceAwsLbDelete, Importer: &schema.ResourceImporter{ State: schema.ImportStatePassthrough, }, @@ -65,6 +68,13 @@ func resourceAwsAlb() *schema.Resource { Computed: true, }, + "load_balancer_type": { + Type: schema.TypeString, + ForceNew: true, + Optional: true, + Default: "application", + }, + "security_groups": { Type: schema.TypeSet, Elem: &schema.Schema{Type: schema.TypeString}, @@ -76,13 +86,42 @@ func resourceAwsAlb() *schema.Resource { "subnets": { Type: schema.TypeSet, Elem: &schema.Schema{Type: schema.TypeString}, - Required: true, + Optional: true, + Computed: true, Set: schema.HashString, }, + "subnet_mapping": { + Type: schema.TypeSet, + Optional: true, + ForceNew: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "subnet_id": { + Type: schema.TypeString, + Required: true, + }, + "allocation_id": { + Type: schema.TypeString, + Optional: true, + }, + }, + }, + Set: func(v interface{}) int { + var buf bytes.Buffer + m := v.(map[string]interface{}) + buf.WriteString(fmt.Sprintf("%s-", m["subnet_id"].(string))) + if m["allocation_id"] != "" { + buf.WriteString(fmt.Sprintf("%s-", m["allocation_id"].(string))) + } + return hashcode.String(buf.String()) + }, + }, + "access_logs": { Type: schema.TypeList, Optional: true, + Computed: true, MaxItems: 1, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ @@ -93,11 +132,12 @@ func resourceAwsAlb() *schema.Resource { "prefix": { Type: schema.TypeString, Optional: true, + Computed: true, }, "enabled": { Type: schema.TypeBool, Optional: true, - Default: true, + Computed: true, }, }, }, @@ -141,7 +181,7 @@ func resourceAwsAlb() *schema.Resource { } } -func resourceAwsAlbCreate(d *schema.ResourceData, meta interface{}) error { +func resourceAwsLbCreate(d *schema.ResourceData, meta interface{}) error { elbconn := meta.(*AWSClient).elbv2conn var name string @@ -156,6 +196,7 @@ func resourceAwsAlbCreate(d *schema.ResourceData, meta interface{}) error { elbOpts := &elbv2.CreateLoadBalancerInput{ Name: aws.String(name), + Type: aws.String(d.Get("load_balancer_type").(string)), Tags: tagsFromMapELBv2(d.Get("tags").(map[string]interface{})), } @@ -171,6 +212,24 @@ func resourceAwsAlbCreate(d *schema.ResourceData, meta interface{}) error { elbOpts.Subnets = expandStringList(v.(*schema.Set).List()) } + if v, ok := d.GetOk("subnet_mapping"); ok { + rawMappings := v.(*schema.Set).List() + elbOpts.SubnetMappings = make([]*elbv2.SubnetMapping, len(rawMappings)) + for i, mapping := range rawMappings { + subnetMap := mapping.(map[string]interface{}) + + elbOpts.SubnetMappings[i] = &elbv2.SubnetMapping{ + SubnetId: aws.String(subnetMap["subnet_id"].(string)), + } + + if subnetMap["allocation_id"].(string) != "" { + elbOpts.SubnetMappings[i] = &elbv2.SubnetMapping{ + AllocationId: aws.String(subnetMap["allocation_id"].(string)), + } + } + } + } + if v, ok := d.GetOk("ip_address_type"); ok { elbOpts.IpAddressType = aws.String(v.(string)) } @@ -219,18 +278,18 @@ func resourceAwsAlbCreate(d *schema.ResourceData, meta interface{}) error { return err } - return resourceAwsAlbUpdate(d, meta) + return resourceAwsLbUpdate(d, meta) } -func resourceAwsAlbRead(d *schema.ResourceData, meta interface{}) error { +func resoureAwsLbRead(d *schema.ResourceData, meta interface{}) error { elbconn := meta.(*AWSClient).elbv2conn - albArn := d.Id() + lbArn := d.Id() - describeAlbOpts := &elbv2.DescribeLoadBalancersInput{ - LoadBalancerArns: []*string{aws.String(albArn)}, + describeLbOpts := &elbv2.DescribeLoadBalancersInput{ + LoadBalancerArns: []*string{aws.String(lbArn)}, } - describeResp, err := elbconn.DescribeLoadBalancers(describeAlbOpts) + describeResp, err := elbconn.DescribeLoadBalancers(describeLbOpts) if err != nil { if isLoadBalancerNotFound(err) { // The ALB is gone now, so just remove it from the state @@ -245,10 +304,10 @@ func resourceAwsAlbRead(d *schema.ResourceData, meta interface{}) error { return fmt.Errorf("Unable to find ALB: %#v", describeResp.LoadBalancers) } - return flattenAwsAlbResource(d, meta, describeResp.LoadBalancers[0]) + return flattenAwsLbResource(d, meta, describeResp.LoadBalancers[0]) } -func resourceAwsAlbUpdate(d *schema.ResourceData, meta interface{}) error { +func resourceAwsLbUpdate(d *schema.ResourceData, meta interface{}) error { elbconn := meta.(*AWSClient).elbv2conn if !d.IsNewResource() { @@ -295,7 +354,8 @@ func resourceAwsAlbUpdate(d *schema.ResourceData, meta interface{}) error { }) } - if d.HasChange("idle_timeout") { + // It's important to know that Idle timeout is not supported for Network Loadbalancers + if d.Get("load_balancer_type").(string) != "network" && d.HasChange("idle_timeout") { attributes = append(attributes, &elbv2.LoadBalancerAttribute{ Key: aws.String("idle_timeout.timeout_seconds"), Value: aws.String(fmt.Sprintf("%d", d.Get("idle_timeout").(int))), @@ -386,11 +446,11 @@ func resourceAwsAlbUpdate(d *schema.ResourceData, meta interface{}) error { return err } - return resourceAwsAlbRead(d, meta) + return resoureAwsLbRead(d, meta) } -func resourceAwsAlbDelete(d *schema.ResourceData, meta interface{}) error { - albconn := meta.(*AWSClient).elbv2conn +func resourceAwsLbDelete(d *schema.ResourceData, meta interface{}) error { + lbconn := meta.(*AWSClient).elbv2conn log.Printf("[INFO] Deleting ALB: %s", d.Id()) @@ -398,11 +458,11 @@ func resourceAwsAlbDelete(d *schema.ResourceData, meta interface{}) error { deleteElbOpts := elbv2.DeleteLoadBalancerInput{ LoadBalancerArn: aws.String(d.Id()), } - if _, err := albconn.DeleteLoadBalancer(&deleteElbOpts); err != nil { + if _, err := lbconn.DeleteLoadBalancer(&deleteElbOpts); err != nil { return fmt.Errorf("Error deleting ALB: %s", err) } - err := cleanupALBNetworkInterfaces(meta.(*AWSClient).ec2conn, d.Id()) + err := cleanupLBNetworkInterfaces(meta.(*AWSClient).ec2conn, d.Id()) if err != nil { log.Printf("[WARN] Failed to cleanup ENIs for ALB %q: %#v", d.Id(), err) } @@ -414,11 +474,11 @@ func resourceAwsAlbDelete(d *schema.ResourceData, meta interface{}) error { // but the cleanup is asynchronous and may take time // which then blocks IGW, SG or VPC on deletion // So we make the cleanup "synchronous" here -func cleanupALBNetworkInterfaces(conn *ec2.EC2, albArn string) error { +func cleanupLBNetworkInterfaces(conn *ec2.EC2, lbArn string) error { re := regexp.MustCompile("([^/]+/[^/]+/[^/]+)$") - matches := re.FindStringSubmatch(albArn) + matches := re.FindStringSubmatch(lbArn) if len(matches) != 2 { - return fmt.Errorf("Unexpected ARN format: %q", albArn) + return fmt.Errorf("Unexpected ARN format: %q", lbArn) } // e.g. app/example-alb/b26e625cdde161e6 @@ -471,7 +531,7 @@ func flattenSubnetsFromAvailabilityZones(availabilityZones []*elbv2.Availability return result } -func albSuffixFromARN(arn *string) string { +func lbSuffixFromARN(arn *string) string { if arn == nil { return "" } @@ -485,23 +545,41 @@ func albSuffixFromARN(arn *string) string { return "" } -// flattenAwsAlbResource takes a *elbv2.LoadBalancer and populates all respective resource fields. -func flattenAwsAlbResource(d *schema.ResourceData, meta interface{}, alb *elbv2.LoadBalancer) error { +// flattenAwsLbResource takes a *elbv2.LoadBalancer and populates all respective resource fields. +func flattenAwsLbResource(d *schema.ResourceData, meta interface{}, lb *elbv2.LoadBalancer) error { elbconn := meta.(*AWSClient).elbv2conn - d.Set("arn", alb.LoadBalancerArn) - d.Set("arn_suffix", albSuffixFromARN(alb.LoadBalancerArn)) - d.Set("name", alb.LoadBalancerName) - d.Set("internal", (alb.Scheme != nil && *alb.Scheme == "internal")) - d.Set("security_groups", flattenStringList(alb.SecurityGroups)) - d.Set("subnets", flattenSubnetsFromAvailabilityZones(alb.AvailabilityZones)) - d.Set("vpc_id", alb.VpcId) - d.Set("zone_id", alb.CanonicalHostedZoneId) - d.Set("dns_name", alb.DNSName) - d.Set("ip_address_type", alb.IpAddressType) + d.Set("arn", lb.LoadBalancerArn) + d.Set("arn_suffix", lbSuffixFromARN(lb.LoadBalancerArn)) + d.Set("name", lb.LoadBalancerName) + d.Set("internal", (lb.Scheme != nil && *lb.Scheme == "internal")) + d.Set("security_groups", flattenStringList(lb.SecurityGroups)) + d.Set("subnets", flattenSubnetsFromAvailabilityZones(lb.AvailabilityZones)) + d.Set("vpc_id", lb.VpcId) + d.Set("zone_id", lb.CanonicalHostedZoneId) + d.Set("dns_name", lb.DNSName) + d.Set("ip_address_type", lb.IpAddressType) + d.Set("load_balancer_type", lb.Type) + + subnetMappings := make([]interface{}, 0) + for _, az := range lb.AvailabilityZones { + subnetMappingRaw := make([]map[string]interface{}, len(az.LoadBalancerAddresses)) + for _, subnet := range az.LoadBalancerAddresses { + subnetMap := make(map[string]interface{}, 0) + subnetMap["subnet_id"] = *az.SubnetId + + if subnet.AllocationId != nil { + subnetMap["allocation_id"] = *subnet.AllocationId + } + + subnetMappingRaw = append(subnetMappingRaw, subnetMap) + } + subnetMappings = append(subnetMappings, subnetMappingRaw) + } + d.Set("subnet_mapping", subnetMappings) respTags, err := elbconn.DescribeTags(&elbv2.DescribeTagsInput{ - ResourceArns: []*string{alb.LoadBalancerArn}, + ResourceArns: []*string{lb.LoadBalancerArn}, }) if err != nil { return errwrap.Wrapf("Error retrieving ALB Tags: {{err}}", err) diff --git a/aws/resource_aws_alb_listener.go b/aws/resource_aws_lb_listener.go similarity index 78% rename from aws/resource_aws_alb_listener.go rename to aws/resource_aws_lb_listener.go index f94e3b1a16b..ec99fa366f4 100644 --- a/aws/resource_aws_alb_listener.go +++ b/aws/resource_aws_lb_listener.go @@ -15,12 +15,12 @@ import ( "github.com/hashicorp/terraform/helper/schema" ) -func resourceAwsAlbListener() *schema.Resource { +func resourceAwsLbListener() *schema.Resource { return &schema.Resource{ - Create: resourceAwsAlbListenerCreate, - Read: resourceAwsAlbListenerRead, - Update: resourceAwsAlbListenerUpdate, - Delete: resourceAwsAlbListenerDelete, + Create: resourceAwsLbListenerCreate, + Read: resourceAwsLbListenerRead, + Update: resourceAwsLbListenerUpdate, + Delete: resourceAwsLbListenerDelete, Importer: &schema.ResourceImporter{ State: schema.ImportStatePassthrough, }, @@ -40,7 +40,7 @@ func resourceAwsAlbListener() *schema.Resource { "port": { Type: schema.TypeInt, Required: true, - ValidateFunc: validateAwsAlbListenerPort, + ValidateFunc: validateAwsLbListenerPort, }, "protocol": { @@ -50,7 +50,7 @@ func resourceAwsAlbListener() *schema.Resource { StateFunc: func(v interface{}) string { return strings.ToUpper(v.(string)) }, - ValidateFunc: validateAwsAlbListenerProtocol, + ValidateFunc: validateAwsLbListenerProtocol, }, "ssl_policy": { @@ -76,7 +76,7 @@ func resourceAwsAlbListener() *schema.Resource { "type": { Type: schema.TypeString, Required: true, - ValidateFunc: validateAwsAlbListenerActionType, + ValidateFunc: validateAwsLbListenerActionType, }, }, }, @@ -85,13 +85,13 @@ func resourceAwsAlbListener() *schema.Resource { } } -func resourceAwsAlbListenerCreate(d *schema.ResourceData, meta interface{}) error { +func resourceAwsLbListenerCreate(d *schema.ResourceData, meta interface{}) error { elbconn := meta.(*AWSClient).elbv2conn - albArn := d.Get("load_balancer_arn").(string) + lbArn := d.Get("load_balancer_arn").(string) params := &elbv2.CreateListenerInput{ - LoadBalancerArn: aws.String(albArn), + LoadBalancerArn: aws.String(lbArn), Port: aws.Int64(int64(d.Get("port").(int))), Protocol: aws.String(d.Get("protocol").(string)), } @@ -124,11 +124,11 @@ func resourceAwsAlbListenerCreate(d *schema.ResourceData, meta interface{}) erro err := resource.Retry(5*time.Minute, func() *resource.RetryError { var err error - log.Printf("[DEBUG] Creating ALB listener for ARN: %s", d.Get("load_balancer_arn").(string)) + log.Printf("[DEBUG] Creating LB listener for ARN: %s", d.Get("load_balancer_arn").(string)) resp, err = elbconn.CreateListener(params) if awsErr, ok := err.(awserr.Error); ok { if awsErr.Code() == "CertificateNotFound" { - log.Printf("[WARN] Got an error while trying to create ALB listener for ARN: %s: %s", albArn, err) + log.Printf("[WARN] Got an error while trying to create LB listener for ARN: %s: %s", lbArn, err) return resource.RetryableError(err) } } @@ -140,19 +140,19 @@ func resourceAwsAlbListenerCreate(d *schema.ResourceData, meta interface{}) erro }) if err != nil { - return errwrap.Wrapf("Error creating ALB Listener: {{err}}", err) + return errwrap.Wrapf("Error creating LB Listener: {{err}}", err) } if len(resp.Listeners) == 0 { - return errors.New("Error creating ALB Listener: no listeners returned in response") + return errors.New("Error creating LB Listener: no listeners returned in response") } d.SetId(*resp.Listeners[0].ListenerArn) - return resourceAwsAlbListenerRead(d, meta) + return resourceAwsLbListenerRead(d, meta) } -func resourceAwsAlbListenerRead(d *schema.ResourceData, meta interface{}) error { +func resourceAwsLbListenerRead(d *schema.ResourceData, meta interface{}) error { elbconn := meta.(*AWSClient).elbv2conn resp, err := elbconn.DescribeListeners(&elbv2.DescribeListenersInput{ @@ -198,7 +198,7 @@ func resourceAwsAlbListenerRead(d *schema.ResourceData, meta interface{}) error return nil } -func resourceAwsAlbListenerUpdate(d *schema.ResourceData, meta interface{}) error { +func resourceAwsLbListenerUpdate(d *schema.ResourceData, meta interface{}) error { elbconn := meta.(*AWSClient).elbv2conn params := &elbv2.ModifyListenerInput{ @@ -233,13 +233,13 @@ func resourceAwsAlbListenerUpdate(d *schema.ResourceData, meta interface{}) erro _, err := elbconn.ModifyListener(params) if err != nil { - return errwrap.Wrapf("Error modifying ALB Listener: {{err}}", err) + return errwrap.Wrapf("Error modifying LB Listener: {{err}}", err) } - return resourceAwsAlbListenerRead(d, meta) + return resourceAwsLbListenerRead(d, meta) } -func resourceAwsAlbListenerDelete(d *schema.ResourceData, meta interface{}) error { +func resourceAwsLbListenerDelete(d *schema.ResourceData, meta interface{}) error { elbconn := meta.(*AWSClient).elbv2conn _, err := elbconn.DeleteListener(&elbv2.DeleteListenerInput{ @@ -252,7 +252,7 @@ func resourceAwsAlbListenerDelete(d *schema.ResourceData, meta interface{}) erro return nil } -func validateAwsAlbListenerPort(v interface{}, k string) (ws []string, errors []error) { +func validateAwsLbListenerPort(v interface{}, k string) (ws []string, errors []error) { port := v.(int) if port < 1 || port > 65536 { errors = append(errors, fmt.Errorf("%q must be a valid port number (1-65536)", k)) @@ -260,17 +260,17 @@ func validateAwsAlbListenerPort(v interface{}, k string) (ws []string, errors [] return } -func validateAwsAlbListenerProtocol(v interface{}, k string) (ws []string, errors []error) { +func validateAwsLbListenerProtocol(v interface{}, k string) (ws []string, errors []error) { value := strings.ToLower(v.(string)) - if value == "http" || value == "https" { + if value == "http" || value == "https" || value == "tcp" { return } - errors = append(errors, fmt.Errorf("%q must be either %q or %q", k, "HTTP", "HTTPS")) + errors = append(errors, fmt.Errorf("%q must be either %q, %q or %q", k, "HTTP", "HTTPS", "TCP")) return } -func validateAwsAlbListenerActionType(v interface{}, k string) (ws []string, errors []error) { +func validateAwsLbListenerActionType(v interface{}, k string) (ws []string, errors []error) { value := strings.ToLower(v.(string)) if value != "forward" { errors = append(errors, fmt.Errorf("%q must have the value %q", k, "forward")) diff --git a/aws/resource_aws_alb_listener_rule.go b/aws/resource_aws_lb_listener_rule.go similarity index 82% rename from aws/resource_aws_alb_listener_rule.go rename to aws/resource_aws_lb_listener_rule.go index 3e3a880b860..020dc7d848d 100644 --- a/aws/resource_aws_alb_listener_rule.go +++ b/aws/resource_aws_lb_listener_rule.go @@ -14,12 +14,12 @@ import ( "github.com/hashicorp/terraform/helper/schema" ) -func resourceAwsAlbListenerRule() *schema.Resource { +func resourceAwsLbbListenerRule() *schema.Resource { return &schema.Resource{ - Create: resourceAwsAlbListenerRuleCreate, - Read: resourceAwsAlbListenerRuleRead, - Update: resourceAwsAlbListenerRuleUpdate, - Delete: resourceAwsAlbListenerRuleDelete, + Create: resourceAwsLbListenerRuleCreate, + Read: resourceAwsLbListenerRuleRead, + Update: resourceAwsLbListenerRuleUpdate, + Delete: resourceAwsLbListenerRuleDelete, Importer: &schema.ResourceImporter{ State: schema.ImportStatePassthrough, }, @@ -38,7 +38,7 @@ func resourceAwsAlbListenerRule() *schema.Resource { Type: schema.TypeInt, Required: true, ForceNew: true, - ValidateFunc: validateAwsAlbListenerRulePriority, + ValidateFunc: validateAwsLbListenerRulePriority, }, "action": { Type: schema.TypeList, @@ -52,7 +52,7 @@ func resourceAwsAlbListenerRule() *schema.Resource { "type": { Type: schema.TypeString, Required: true, - ValidateFunc: validateAwsAlbListenerActionType, + ValidateFunc: validateAwsLbListenerActionType, }, }, }, @@ -80,7 +80,7 @@ func resourceAwsAlbListenerRule() *schema.Resource { } } -func resourceAwsAlbListenerRuleCreate(d *schema.ResourceData, meta interface{}) error { +func resourceAwsLbListenerRuleCreate(d *schema.ResourceData, meta interface{}) error { elbconn := meta.(*AWSClient).elbv2conn params := &elbv2.CreateRuleInput{ @@ -114,19 +114,19 @@ func resourceAwsAlbListenerRuleCreate(d *schema.ResourceData, meta interface{}) resp, err := elbconn.CreateRule(params) if err != nil { - return errwrap.Wrapf("Error creating ALB Listener Rule: {{err}}", err) + return errwrap.Wrapf("Error creating LB Listener Rule: {{err}}", err) } if len(resp.Rules) == 0 { - return errors.New("Error creating ALB Listener Rule: no rules returned in response") + return errors.New("Error creating LB Listener Rule: no rules returned in response") } d.SetId(*resp.Rules[0].RuleArn) - return resourceAwsAlbListenerRuleRead(d, meta) + return resourceAwsLbListenerRuleRead(d, meta) } -func resourceAwsAlbListenerRuleRead(d *schema.ResourceData, meta interface{}) error { +func resourceAwsLbListenerRuleRead(d *schema.ResourceData, meta interface{}) error { elbconn := meta.(*AWSClient).elbv2conn resp, err := elbconn.DescribeRules(&elbv2.DescribeRulesInput{ @@ -150,7 +150,7 @@ func resourceAwsAlbListenerRuleRead(d *schema.ResourceData, meta interface{}) er d.Set("arn", rule.RuleArn) // The listener arn isn't in the response but can be derived from the rule arn - d.Set("listener_arn", albListenerARNFromRuleARN(*rule.RuleArn)) + d.Set("listener_arn", lbListenerARNFromRuleARN(*rule.RuleArn)) // Rules are evaluated in priority order, from the lowest value to the highest value. The default rule has the lowest priority. if *rule.Priority == "default" { @@ -188,7 +188,7 @@ func resourceAwsAlbListenerRuleRead(d *schema.ResourceData, meta interface{}) er return nil } -func resourceAwsAlbListenerRuleUpdate(d *schema.ResourceData, meta interface{}) error { +func resourceAwsLbListenerRuleUpdate(d *schema.ResourceData, meta interface{}) error { elbconn := meta.(*AWSClient).elbv2conn d.Partial(true) @@ -251,32 +251,32 @@ func resourceAwsAlbListenerRuleUpdate(d *schema.ResourceData, meta interface{}) if requestUpdate { resp, err := elbconn.ModifyRule(params) if err != nil { - return errwrap.Wrapf("Error modifying ALB Listener Rule: {{err}}", err) + return errwrap.Wrapf("Error modifying LB Listener Rule: {{err}}", err) } if len(resp.Rules) == 0 { - return errors.New("Error modifying creating ALB Listener Rule: no rules returned in response") + return errors.New("Error modifying creating LB Listener Rule: no rules returned in response") } } d.Partial(false) - return resourceAwsAlbListenerRuleRead(d, meta) + return resourceAwsLbListenerRuleRead(d, meta) } -func resourceAwsAlbListenerRuleDelete(d *schema.ResourceData, meta interface{}) error { +func resourceAwsLbListenerRuleDelete(d *schema.ResourceData, meta interface{}) error { elbconn := meta.(*AWSClient).elbv2conn _, err := elbconn.DeleteRule(&elbv2.DeleteRuleInput{ RuleArn: aws.String(d.Id()), }) if err != nil && !isRuleNotFound(err) { - return errwrap.Wrapf("Error deleting ALB Listener Rule: {{err}}", err) + return errwrap.Wrapf("Error deleting LB Listener Rule: {{err}}", err) } return nil } -func validateAwsAlbListenerRulePriority(v interface{}, k string) (ws []string, errors []error) { +func validateAwsLbListenerRulePriority(v interface{}, k string) (ws []string, errors []error) { value := v.(int) if value < 1 || value > 99999 { errors = append(errors, fmt.Errorf("%q must be in the range 1-99999", k)) @@ -298,10 +298,10 @@ func validateAwsListenerRuleField(v interface{}, k string) (ws []string, errors // (arn:aws:elasticloadbalancing:us-east-1:012345678912:listener)-rule(/app/name/0123456789abcdef/abcdef0123456789)/456789abcedf1234 // concat to become: // arn:aws:elasticloadbalancing:us-east-1:012345678912:listener/app/name/0123456789abcdef/abcdef0123456789 -var albListenerARNFromRuleARNRegexp = regexp.MustCompile(`^(arn:.+:listener)-rule(/.+)/[^/]+$`) +var lbListenerARNFromRuleARNRegexp = regexp.MustCompile(`^(arn:.+:listener)-rule(/.+)/[^/]+$`) -func albListenerARNFromRuleARN(ruleArn string) string { - if arnComponents := albListenerARNFromRuleARNRegexp.FindStringSubmatch(ruleArn); len(arnComponents) > 1 { +func lbListenerARNFromRuleARN(ruleArn string) string { + if arnComponents := lbListenerARNFromRuleARNRegexp.FindStringSubmatch(ruleArn); len(arnComponents) > 1 { return arnComponents[1] + arnComponents[2] } diff --git a/aws/resource_aws_alb_listener_rule_test.go b/aws/resource_aws_lb_listener_rule_test.go similarity index 60% rename from aws/resource_aws_alb_listener_rule_test.go rename to aws/resource_aws_lb_listener_rule_test.go index 457ca7216dc..1a4faa27057 100644 --- a/aws/resource_aws_alb_listener_rule_test.go +++ b/aws/resource_aws_lb_listener_rule_test.go @@ -14,7 +14,7 @@ import ( "github.com/hashicorp/terraform/terraform" ) -func TestALBListenerARNFromRuleARN(t *testing.T) { +func TestLBListenerARNFromRuleARN(t *testing.T) { cases := []struct { name string arn string @@ -48,28 +48,59 @@ func TestALBListenerARNFromRuleARN(t *testing.T) { } for _, tc := range cases { - actual := albListenerARNFromRuleARN(tc.arn) + actual := lbListenerARNFromRuleARN(tc.arn) if actual != tc.expected { t.Fatalf("incorrect arn returned: %q\nExpected: %s\n Got: %s", tc.name, tc.expected, actual) } } } -func TestAccAWSALBListenerRule_basic(t *testing.T) { +func TestAccAWSLBListenerRule_basic(t *testing.T) { var conf elbv2.Rule - albName := fmt.Sprintf("testrule-basic-%s", acctest.RandStringFromCharSet(13, acctest.CharSetAlphaNum)) + lbName := fmt.Sprintf("testrule-basic-%s", acctest.RandStringFromCharSet(13, acctest.CharSetAlphaNum)) + targetGroupName := fmt.Sprintf("testtargetgroup-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + IDRefreshName: "aws_lb_listener_rule.static", + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSLBListenerRuleDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSLBListenerRuleConfig_basic(lbName, targetGroupName), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSLBListenerRuleExists("aws_lb_listener_rule.static", &conf), + resource.TestCheckResourceAttrSet("aws_lb_listener_rule.static", "arn"), + resource.TestCheckResourceAttrSet("aws_lb_listener_rule.static", "listener_arn"), + resource.TestCheckResourceAttr("aws_lb_listener_rule.static", "priority", "100"), + resource.TestCheckResourceAttr("aws_lb_listener_rule.static", "action.#", "1"), + resource.TestCheckResourceAttr("aws_lb_listener_rule.static", "action.0.type", "forward"), + resource.TestCheckResourceAttrSet("aws_lb_listener_rule.static", "action.0.target_group_arn"), + resource.TestCheckResourceAttr("aws_lb_listener_rule.static", "condition.#", "1"), + resource.TestCheckResourceAttr("aws_lb_listener_rule.static", "condition.1366281676.field", "path-pattern"), + resource.TestCheckResourceAttr("aws_lb_listener_rule.static", "condition.1366281676.values.#", "1"), + resource.TestCheckResourceAttrSet("aws_lb_listener_rule.static", "condition.1366281676.values.0"), + ), + }, + }, + }) +} + +func TestAccAWSLBListenerRuleBackwardsCompatibility(t *testing.T) { + var conf elbv2.Rule + lbName := fmt.Sprintf("testrule-basic-%s", acctest.RandStringFromCharSet(13, acctest.CharSetAlphaNum)) targetGroupName := fmt.Sprintf("testtargetgroup-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, IDRefreshName: "aws_alb_listener_rule.static", Providers: testAccProviders, - CheckDestroy: testAccCheckAWSALBListenerRuleDestroy, + CheckDestroy: testAccCheckAWSLBListenerRuleDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSALBListenerRuleConfig_basic(albName, targetGroupName), + Config: testAccAWSLBListenerRuleConfigBackwardsCompatibility(lbName, targetGroupName), Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBListenerRuleExists("aws_alb_listener_rule.static", &conf), + testAccCheckAWSLBListenerRuleExists("aws_alb_listener_rule.static", &conf), resource.TestCheckResourceAttrSet("aws_alb_listener_rule.static", "arn"), resource.TestCheckResourceAttrSet("aws_alb_listener_rule.static", "listener_arn"), resource.TestCheckResourceAttr("aws_alb_listener_rule.static", "priority", "100"), @@ -86,81 +117,81 @@ func TestAccAWSALBListenerRule_basic(t *testing.T) { }) } -func TestAccAWSALBListenerRule_updateRulePriority(t *testing.T) { +func TestAccAWSLBListenerRule_updateRulePriority(t *testing.T) { var rule elbv2.Rule - albName := fmt.Sprintf("testrule-basic-%s", acctest.RandStringFromCharSet(13, acctest.CharSetAlphaNum)) + lbName := fmt.Sprintf("testrule-basic-%s", acctest.RandStringFromCharSet(13, acctest.CharSetAlphaNum)) targetGroupName := fmt.Sprintf("testtargetgroup-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, - IDRefreshName: "aws_alb_listener_rule.static", + IDRefreshName: "aws_lb_listener_rule.static", Providers: testAccProviders, - CheckDestroy: testAccCheckAWSALBListenerRuleDestroy, + CheckDestroy: testAccCheckAWSLBListenerRuleDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSALBListenerRuleConfig_basic(albName, targetGroupName), + Config: testAccAWSLBListenerRuleConfig_basic(lbName, targetGroupName), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSALBListenerRuleExists("aws_alb_listener_rule.static", &rule), - resource.TestCheckResourceAttr("aws_alb_listener_rule.static", "priority", "100"), + testAccCheckAWSLBListenerRuleExists("aws_lb_listener_rule.static", &rule), + resource.TestCheckResourceAttr("aws_lb_listener_rule.static", "priority", "100"), ), }, { - Config: testAccAWSALBListenerRuleConfig_updateRulePriority(albName, targetGroupName), + Config: testAccAWSLBListenerRuleConfig_updateRulePriority(lbName, targetGroupName), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSALBListenerRuleExists("aws_alb_listener_rule.static", &rule), - resource.TestCheckResourceAttr("aws_alb_listener_rule.static", "priority", "101"), + testAccCheckAWSLBListenerRuleExists("aws_lb_listener_rule.static", &rule), + resource.TestCheckResourceAttr("aws_lb_listener_rule.static", "priority", "101"), ), }, }, }) } -func TestAccAWSALBListenerRule_changeListenerRuleArnForcesNew(t *testing.T) { +func TestAccAWSLBListenerRule_changeListenerRuleArnForcesNew(t *testing.T) { var before, after elbv2.Rule - albName := fmt.Sprintf("testrule-basic-%s", acctest.RandStringFromCharSet(13, acctest.CharSetAlphaNum)) + lbName := fmt.Sprintf("testrule-basic-%s", acctest.RandStringFromCharSet(13, acctest.CharSetAlphaNum)) targetGroupName := fmt.Sprintf("testtargetgroup-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, - IDRefreshName: "aws_alb_listener_rule.static", + IDRefreshName: "aws_lb_listener_rule.static", Providers: testAccProviders, - CheckDestroy: testAccCheckAWSALBListenerRuleDestroy, + CheckDestroy: testAccCheckAWSLBListenerRuleDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSALBListenerRuleConfig_basic(albName, targetGroupName), + Config: testAccAWSLBListenerRuleConfig_basic(lbName, targetGroupName), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSALBListenerRuleExists("aws_alb_listener_rule.static", &before), + testAccCheckAWSLBListenerRuleExists("aws_lb_listener_rule.static", &before), ), }, { - Config: testAccAWSALBListenerRuleConfig_changeRuleArn(albName, targetGroupName), + Config: testAccAWSLBListenerRuleConfig_changeRuleArn(lbName, targetGroupName), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSALBListenerRuleExists("aws_alb_listener_rule.static", &after), - testAccCheckAWSAlbListenerRuleRecreated(t, &before, &after), + testAccCheckAWSLBListenerRuleExists("aws_lb_listener_rule.static", &after), + testAccCheckAWSLbListenerRuleRecreated(t, &before, &after), ), }, }, }) } -func TestAccAWSALBListenerRule_multipleConditionThrowsError(t *testing.T) { - albName := fmt.Sprintf("testrule-basic-%s", acctest.RandStringFromCharSet(13, acctest.CharSetAlphaNum)) +func TestAccAWSLBListenerRule_multipleConditionThrowsError(t *testing.T) { + lbName := fmt.Sprintf("testrule-basic-%s", acctest.RandStringFromCharSet(13, acctest.CharSetAlphaNum)) targetGroupName := fmt.Sprintf("testtargetgroup-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, - CheckDestroy: testAccCheckAWSALBListenerRuleDestroy, + CheckDestroy: testAccCheckAWSLBListenerRuleDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSALBListenerRuleConfig_multipleConditions(albName, targetGroupName), + Config: testAccAWSLBListenerRuleConfig_multipleConditions(lbName, targetGroupName), ExpectError: regexp.MustCompile(`attribute supports 1 item maximum`), }, }, }) } -func testAccCheckAWSAlbListenerRuleRecreated(t *testing.T, +func testAccCheckAWSLbListenerRuleRecreated(t *testing.T, before, after *elbv2.Rule) resource.TestCheckFunc { return func(s *terraform.State) error { if *before.RuleArn == *after.RuleArn { @@ -170,7 +201,7 @@ func testAccCheckAWSAlbListenerRuleRecreated(t *testing.T, } } -func testAccCheckAWSALBListenerRuleExists(n string, res *elbv2.Rule) resource.TestCheckFunc { +func testAccCheckAWSLBListenerRuleExists(n string, res *elbv2.Rule) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { @@ -201,11 +232,11 @@ func testAccCheckAWSALBListenerRuleExists(n string, res *elbv2.Rule) resource.Te } } -func testAccCheckAWSALBListenerRuleDestroy(s *terraform.State) error { +func testAccCheckAWSLBListenerRuleDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).elbv2conn for _, rs := range s.RootModule().Resources { - if rs.Type != "aws_alb_listener_rule" { + if rs.Type != "aws_lb_listener_rule" && rs.Type != "aws_alb_listener_rule" { continue } @@ -224,21 +255,21 @@ func testAccCheckAWSALBListenerRuleDestroy(s *terraform.State) error { if isRuleNotFound(err) { return nil } else { - return errwrap.Wrapf("Unexpected error checking ALB Listener Rule destroyed: {{err}}", err) + return errwrap.Wrapf("Unexpected error checking LB Listener Rule destroyed: {{err}}", err) } } return nil } -func testAccAWSALBListenerRuleConfig_multipleConditions(albName, targetGroupName string) string { - return fmt.Sprintf(`resource "aws_alb_listener_rule" "static" { - listener_arn = "${aws_alb_listener.front_end.arn}" +func testAccAWSLBListenerRuleConfig_multipleConditions(lbName, targetGroupName string) string { + return fmt.Sprintf(`resource "aws_lb_listener_rule" "static" { + listener_arn = "${aws_lb_listener.front_end.arn}" priority = 100 action { type = "forward" - target_group_arn = "${aws_alb_target_group.test.arn}" + target_group_arn = "${aws_lb_target_group.test.arn}" } condition { @@ -247,18 +278,18 @@ func testAccAWSALBListenerRuleConfig_multipleConditions(albName, targetGroupName } } -resource "aws_alb_listener" "front_end" { - load_balancer_arn = "${aws_alb.alb_test.id}" +resource "aws_lb_listener" "front_end" { + load_balancer_arn = "${aws_lb.alb_test.id}" protocol = "HTTP" port = "80" default_action { - target_group_arn = "${aws_alb_target_group.test.id}" + target_group_arn = "${aws_lb_target_group.test.id}" type = "forward" } } -resource "aws_alb" "alb_test" { +resource "aws_lb" "alb_test" { name = "%s" internal = true security_groups = ["${aws_security_group.alb_test.id}"] @@ -272,7 +303,118 @@ resource "aws_alb" "alb_test" { } } -resource "aws_alb_target_group" "test" { +resource "aws_lb_target_group" "test" { + name = "%s" + port = 8080 + protocol = "HTTP" + vpc_id = "${aws_vpc.alb_test.id}" + + health_check { + path = "/health" + interval = 60 + port = 8081 + protocol = "HTTP" + timeout = 3 + healthy_threshold = 3 + unhealthy_threshold = 3 + matcher = "200-299" + } +} + +variable "subnets" { + default = ["10.0.1.0/24", "10.0.2.0/24"] + type = "list" +} + +data "aws_availability_zones" "available" {} + +resource "aws_vpc" "alb_test" { + cidr_block = "10.0.0.0/16" + + tags { + TestName = "TestAccAWSALB_basic" + } +} + +resource "aws_subnet" "alb_test" { + count = 2 + vpc_id = "${aws_vpc.alb_test.id}" + cidr_block = "${element(var.subnets, count.index)}" + map_public_ip_on_launch = true + availability_zone = "${element(data.aws_availability_zones.available.names, count.index)}" + + tags { + TestName = "TestAccAWSALB_basic" + } +} + +resource "aws_security_group" "alb_test" { + name = "allow_all_alb_test" + description = "Used for ALB Testing" + vpc_id = "${aws_vpc.alb_test.id}" + + ingress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } + + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } + + tags { + TestName = "TestAccAWSALB_basic" + } +}`, lbName, targetGroupName) +} + +func testAccAWSLBListenerRuleConfig_basic(lbName, targetGroupName string) string { + return fmt.Sprintf(`resource "aws_lb_listener_rule" "static" { + listener_arn = "${aws_lb_listener.front_end.arn}" + priority = 100 + + action { + type = "forward" + target_group_arn = "${aws_lb_target_group.test.arn}" + } + + condition { + field = "path-pattern" + values = ["/static/*"] + } +} + +resource "aws_lb_listener" "front_end" { + load_balancer_arn = "${aws_lb.alb_test.id}" + protocol = "HTTP" + port = "80" + + default_action { + target_group_arn = "${aws_lb_target_group.test.id}" + type = "forward" + } +} + +resource "aws_lb" "alb_test" { + name = "%s" + internal = true + security_groups = ["${aws_security_group.alb_test.id}"] + subnets = ["${aws_subnet.alb_test.*.id}"] + + idle_timeout = 30 + enable_deletion_protection = false + + tags { + TestName = "TestAccAWSALB_basic" + } +} + +resource "aws_lb_target_group" "test" { name = "%s" port = 8080 protocol = "HTTP" @@ -339,10 +481,10 @@ resource "aws_security_group" "alb_test" { tags { TestName = "TestAccAWSALB_basic" } -}`, albName, targetGroupName) +}`, lbName, targetGroupName) } -func testAccAWSALBListenerRuleConfig_basic(albName, targetGroupName string) string { +func testAccAWSLBListenerRuleConfigBackwardsCompatibility(lbName, targetGroupName string) string { return fmt.Sprintf(`resource "aws_alb_listener_rule" "static" { listener_arn = "${aws_alb_listener.front_end.arn}" priority = 100 @@ -450,18 +592,18 @@ resource "aws_security_group" "alb_test" { tags { TestName = "TestAccAWSALB_basic" } -}`, albName, targetGroupName) +}`, lbName, targetGroupName) } -func testAccAWSALBListenerRuleConfig_updateRulePriority(albName, targetGroupName string) string { +func testAccAWSLBListenerRuleConfig_updateRulePriority(lbName, targetGroupName string) string { return fmt.Sprintf(` -resource "aws_alb_listener_rule" "static" { - listener_arn = "${aws_alb_listener.front_end.arn}" +resource "aws_lb_listener_rule" "static" { + listener_arn = "${aws_lb_listener.front_end.arn}" priority = 101 action { type = "forward" - target_group_arn = "${aws_alb_target_group.test.arn}" + target_group_arn = "${aws_lb_target_group.test.arn}" } condition { @@ -470,18 +612,18 @@ resource "aws_alb_listener_rule" "static" { } } -resource "aws_alb_listener" "front_end" { - load_balancer_arn = "${aws_alb.alb_test.id}" +resource "aws_lb_listener" "front_end" { + load_balancer_arn = "${aws_lb.alb_test.id}" protocol = "HTTP" port = "80" default_action { - target_group_arn = "${aws_alb_target_group.test.id}" + target_group_arn = "${aws_lb_target_group.test.id}" type = "forward" } } -resource "aws_alb" "alb_test" { +resource "aws_lb" "alb_test" { name = "%s" internal = true security_groups = ["${aws_security_group.alb_test.id}"] @@ -495,7 +637,7 @@ resource "aws_alb" "alb_test" { } } -resource "aws_alb_target_group" "test" { +resource "aws_lb_target_group" "test" { name = "%s" port = 8080 protocol = "HTTP" @@ -562,18 +704,18 @@ resource "aws_security_group" "alb_test" { tags { TestName = "TestAccAWSALB_basic" } -}`, albName, targetGroupName) +}`, lbName, targetGroupName) } -func testAccAWSALBListenerRuleConfig_changeRuleArn(albName, targetGroupName string) string { +func testAccAWSLBListenerRuleConfig_changeRuleArn(lbName, targetGroupName string) string { return fmt.Sprintf(` -resource "aws_alb_listener_rule" "static" { - listener_arn = "${aws_alb_listener.front_end_ruleupdate.arn}" +resource "aws_lb_listener_rule" "static" { + listener_arn = "${aws_lb_listener.front_end_ruleupdate.arn}" priority = 101 action { type = "forward" - target_group_arn = "${aws_alb_target_group.test.arn}" + target_group_arn = "${aws_lb_target_group.test.arn}" } condition { @@ -582,29 +724,29 @@ resource "aws_alb_listener_rule" "static" { } } -resource "aws_alb_listener" "front_end" { - load_balancer_arn = "${aws_alb.alb_test.id}" +resource "aws_lb_listener" "front_end" { + load_balancer_arn = "${aws_lb.alb_test.id}" protocol = "HTTP" port = "80" default_action { - target_group_arn = "${aws_alb_target_group.test.id}" + target_group_arn = "${aws_lb_target_group.test.id}" type = "forward" } } -resource "aws_alb_listener" "front_end_ruleupdate" { - load_balancer_arn = "${aws_alb.alb_test.id}" +resource "aws_lb_listener" "front_end_ruleupdate" { + load_balancer_arn = "${aws_lb.alb_test.id}" protocol = "HTTP" port = "8080" default_action { - target_group_arn = "${aws_alb_target_group.test.id}" + target_group_arn = "${aws_lb_target_group.test.id}" type = "forward" } } -resource "aws_alb" "alb_test" { +resource "aws_lb" "alb_test" { name = "%s" internal = true security_groups = ["${aws_security_group.alb_test.id}"] @@ -618,7 +760,7 @@ resource "aws_alb" "alb_test" { } } -resource "aws_alb_target_group" "test" { +resource "aws_lb_target_group" "test" { name = "%s" port = 8080 protocol = "HTTP" @@ -685,5 +827,5 @@ resource "aws_security_group" "alb_test" { tags { TestName = "TestAccAWSALB_basic" } -}`, albName, targetGroupName) +}`, lbName, targetGroupName) } diff --git a/aws/resource_aws_alb_listener_test.go b/aws/resource_aws_lb_listener_test.go similarity index 62% rename from aws/resource_aws_alb_listener_test.go rename to aws/resource_aws_lb_listener_test.go index 6fdd84c28b8..741ffa67cd4 100644 --- a/aws/resource_aws_alb_listener_test.go +++ b/aws/resource_aws_lb_listener_test.go @@ -15,21 +15,49 @@ import ( "github.com/hashicorp/terraform/terraform" ) -func TestAccAWSALBListener_basic(t *testing.T) { +func TestAccAWSLBListener_basic(t *testing.T) { var conf elbv2.Listener - albName := fmt.Sprintf("testlistener-basic-%s", acctest.RandStringFromCharSet(13, acctest.CharSetAlphaNum)) + lbName := fmt.Sprintf("testlistener-basic-%s", acctest.RandStringFromCharSet(13, acctest.CharSetAlphaNum)) + targetGroupName := fmt.Sprintf("testtargetgroup-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + IDRefreshName: "aws_lb_listener.front_end", + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSLBListenerDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSLBListenerConfig_basic(lbName, targetGroupName), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSLBListenerExists("aws_lb_listener.front_end", &conf), + resource.TestCheckResourceAttrSet("aws_lb_listener.front_end", "load_balancer_arn"), + resource.TestCheckResourceAttrSet("aws_lb_listener.front_end", "arn"), + resource.TestCheckResourceAttr("aws_lb_listener.front_end", "protocol", "HTTP"), + resource.TestCheckResourceAttr("aws_lb_listener.front_end", "port", "80"), + resource.TestCheckResourceAttr("aws_lb_listener.front_end", "default_action.#", "1"), + resource.TestCheckResourceAttr("aws_lb_listener.front_end", "default_action.0.type", "forward"), + resource.TestCheckResourceAttrSet("aws_lb_listener.front_end", "default_action.0.target_group_arn"), + ), + }, + }, + }) +} + +func TestAccAWSLBListenerBackwardsCompatibility(t *testing.T) { + var conf elbv2.Listener + lbName := fmt.Sprintf("testlistener-basic-%s", acctest.RandStringFromCharSet(13, acctest.CharSetAlphaNum)) targetGroupName := fmt.Sprintf("testtargetgroup-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, IDRefreshName: "aws_alb_listener.front_end", Providers: testAccProviders, - CheckDestroy: testAccCheckAWSALBListenerDestroy, + CheckDestroy: testAccCheckAWSLBListenerDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSALBListenerConfig_basic(albName, targetGroupName), + Config: testAccAWSLBListenerConfigBackwardsCompatibility(lbName, targetGroupName), Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBListenerExists("aws_alb_listener.front_end", &conf), + testAccCheckAWSLBListenerExists("aws_alb_listener.front_end", &conf), resource.TestCheckResourceAttrSet("aws_alb_listener.front_end", "load_balancer_arn"), resource.TestCheckResourceAttrSet("aws_alb_listener.front_end", "arn"), resource.TestCheckResourceAttr("aws_alb_listener.front_end", "protocol", "HTTP"), @@ -43,37 +71,37 @@ func TestAccAWSALBListener_basic(t *testing.T) { }) } -func TestAccAWSALBListener_https(t *testing.T) { +func TestAccAWSLBListener_https(t *testing.T) { var conf elbv2.Listener - albName := fmt.Sprintf("testlistener-https-%s", acctest.RandStringFromCharSet(13, acctest.CharSetAlphaNum)) + lbName := fmt.Sprintf("testlistener-https-%s", acctest.RandStringFromCharSet(13, acctest.CharSetAlphaNum)) targetGroupName := fmt.Sprintf("testtargetgroup-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, - IDRefreshName: "aws_alb_listener.front_end", + IDRefreshName: "aws_lb_listener.front_end", Providers: testAccProviders, - CheckDestroy: testAccCheckAWSALBListenerDestroy, + CheckDestroy: testAccCheckAWSLBListenerDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSALBListenerConfig_https(albName, targetGroupName), + Config: testAccAWSLBListenerConfig_https(lbName, targetGroupName), Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBListenerExists("aws_alb_listener.front_end", &conf), - resource.TestCheckResourceAttrSet("aws_alb_listener.front_end", "load_balancer_arn"), - resource.TestCheckResourceAttrSet("aws_alb_listener.front_end", "arn"), - resource.TestCheckResourceAttr("aws_alb_listener.front_end", "protocol", "HTTPS"), - resource.TestCheckResourceAttr("aws_alb_listener.front_end", "port", "443"), - resource.TestCheckResourceAttr("aws_alb_listener.front_end", "default_action.#", "1"), - resource.TestCheckResourceAttr("aws_alb_listener.front_end", "default_action.0.type", "forward"), - resource.TestCheckResourceAttrSet("aws_alb_listener.front_end", "default_action.0.target_group_arn"), - resource.TestCheckResourceAttrSet("aws_alb_listener.front_end", "certificate_arn"), - resource.TestCheckResourceAttr("aws_alb_listener.front_end", "ssl_policy", "ELBSecurityPolicy-2015-05"), + testAccCheckAWSLBListenerExists("aws_lb_listener.front_end", &conf), + resource.TestCheckResourceAttrSet("aws_lb_listener.front_end", "load_balancer_arn"), + resource.TestCheckResourceAttrSet("aws_lb_listener.front_end", "arn"), + resource.TestCheckResourceAttr("aws_lb_listener.front_end", "protocol", "HTTPS"), + resource.TestCheckResourceAttr("aws_lb_listener.front_end", "port", "443"), + resource.TestCheckResourceAttr("aws_lb_listener.front_end", "default_action.#", "1"), + resource.TestCheckResourceAttr("aws_lb_listener.front_end", "default_action.0.type", "forward"), + resource.TestCheckResourceAttrSet("aws_lb_listener.front_end", "default_action.0.target_group_arn"), + resource.TestCheckResourceAttrSet("aws_lb_listener.front_end", "certificate_arn"), + resource.TestCheckResourceAttr("aws_lb_listener.front_end", "ssl_policy", "ELBSecurityPolicy-2015-05"), ), }, }, }) } -func testAccCheckAWSALBListenerExists(n string, res *elbv2.Listener) resource.TestCheckFunc { +func testAccCheckAWSLBListenerExists(n string, res *elbv2.Listener) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { @@ -104,11 +132,11 @@ func testAccCheckAWSALBListenerExists(n string, res *elbv2.Listener) resource.Te } } -func testAccCheckAWSALBListenerDestroy(s *terraform.State) error { +func testAccCheckAWSLBListenerDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).elbv2conn for _, rs := range s.RootModule().Resources { - if rs.Type != "aws_alb_listener" { + if rs.Type != "aws_lb_listener" && rs.Type != "aws_alb_listener" { continue } @@ -127,14 +155,110 @@ func testAccCheckAWSALBListenerDestroy(s *terraform.State) error { if isListenerNotFound(err) { return nil } else { - return errwrap.Wrapf("Unexpected error checking ALB Listener destroyed: {{err}}", err) + return errwrap.Wrapf("Unexpected error checking LB Listener destroyed: {{err}}", err) } } return nil } -func testAccAWSALBListenerConfig_basic(albName, targetGroupName string) string { +func testAccAWSLBListenerConfig_basic(lbName, targetGroupName string) string { + return fmt.Sprintf(`resource "aws_lb_listener" "front_end" { + load_balancer_arn = "${aws_lb.alb_test.id}" + protocol = "HTTP" + port = "80" + + default_action { + target_group_arn = "${aws_lb_target_group.test.id}" + type = "forward" + } +} + +resource "aws_lb" "alb_test" { + name = "%s" + internal = true + security_groups = ["${aws_security_group.alb_test.id}"] + subnets = ["${aws_subnet.alb_test.*.id}"] + + idle_timeout = 30 + enable_deletion_protection = false + + tags { + TestName = "TestAccAWSALB_basic" + } +} + +resource "aws_lb_target_group" "test" { + name = "%s" + port = 8080 + protocol = "HTTP" + vpc_id = "${aws_vpc.alb_test.id}" + + health_check { + path = "/health" + interval = 60 + port = 8081 + protocol = "HTTP" + timeout = 3 + healthy_threshold = 3 + unhealthy_threshold = 3 + matcher = "200-299" + } +} + +variable "subnets" { + default = ["10.0.1.0/24", "10.0.2.0/24"] + type = "list" +} + +data "aws_availability_zones" "available" {} + +resource "aws_vpc" "alb_test" { + cidr_block = "10.0.0.0/16" + + tags { + TestName = "TestAccAWSALB_basic" + } +} + +resource "aws_subnet" "alb_test" { + count = 2 + vpc_id = "${aws_vpc.alb_test.id}" + cidr_block = "${element(var.subnets, count.index)}" + map_public_ip_on_launch = true + availability_zone = "${element(data.aws_availability_zones.available.names, count.index)}" + + tags { + TestName = "TestAccAWSALB_basic" + } +} + +resource "aws_security_group" "alb_test" { + name = "allow_all_alb_test" + description = "Used for ALB Testing" + vpc_id = "${aws_vpc.alb_test.id}" + + ingress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } + + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } + + tags { + TestName = "TestAccAWSALB_basic" + } +}`, lbName, targetGroupName) +} + +func testAccAWSLBListenerConfigBackwardsCompatibility(lbName, targetGroupName string) string { return fmt.Sprintf(`resource "aws_alb_listener" "front_end" { load_balancer_arn = "${aws_alb.alb_test.id}" protocol = "HTTP" @@ -227,24 +351,24 @@ resource "aws_security_group" "alb_test" { tags { TestName = "TestAccAWSALB_basic" } -}`, albName, targetGroupName) +}`, lbName, targetGroupName) } -func testAccAWSALBListenerConfig_https(albName, targetGroupName string) string { - return fmt.Sprintf(`resource "aws_alb_listener" "front_end" { - load_balancer_arn = "${aws_alb.alb_test.id}" +func testAccAWSLBListenerConfig_https(lbName, targetGroupName string) string { + return fmt.Sprintf(`resource "aws_lb_listener" "front_end" { + load_balancer_arn = "${aws_lb.alb_test.id}" protocol = "HTTPS" port = "443" ssl_policy = "ELBSecurityPolicy-2015-05" certificate_arn = "${aws_iam_server_certificate.test_cert.arn}" default_action { - target_group_arn = "${aws_alb_target_group.test.id}" + target_group_arn = "${aws_lb_target_group.test.id}" type = "forward" } } -resource "aws_alb" "alb_test" { +resource "aws_lb" "alb_test" { name = "%s" internal = false security_groups = ["${aws_security_group.alb_test.id}"] @@ -258,7 +382,7 @@ resource "aws_alb" "alb_test" { } } -resource "aws_alb_target_group" "test" { +resource "aws_lb_target_group" "test" { name = "%s" port = 8080 protocol = "HTTP" @@ -389,5 +513,5 @@ dg+Sd4Wjm89UQoUUoiIcstY7FPbqfBtYKfh4RYHAHV2BwDFqzZCM -----END RSA PRIVATE KEY----- EOF } -`, albName, targetGroupName, rand.New(rand.NewSource(time.Now().UnixNano())).Int()) +`, lbName, targetGroupName, rand.New(rand.NewSource(time.Now().UnixNano())).Int()) } diff --git a/aws/resource_aws_alb_target_group.go b/aws/resource_aws_lb_target_group.go similarity index 80% rename from aws/resource_aws_alb_target_group.go rename to aws/resource_aws_lb_target_group.go index 5dea1b0cc89..9cab96019fe 100644 --- a/aws/resource_aws_alb_target_group.go +++ b/aws/resource_aws_lb_target_group.go @@ -16,12 +16,12 @@ import ( "github.com/hashicorp/terraform/helper/schema" ) -func resourceAwsAlbTargetGroup() *schema.Resource { +func resourceAwsLbTargetGroup() *schema.Resource { return &schema.Resource{ - Create: resourceAwsAlbTargetGroupCreate, - Read: resourceAwsAlbTargetGroupRead, - Update: resourceAwsAlbTargetGroupUpdate, - Delete: resourceAwsAlbTargetGroupDelete, + Create: resourceAwsLbTargetGroupCreate, + Read: resourceAwsLbTargetGroupRead, + Update: resourceAwsLbTargetGroupUpdate, + Delete: resourceAwsLbTargetGroupDelete, Importer: &schema.ResourceImporter{ State: schema.ImportStatePassthrough, }, @@ -43,27 +43,27 @@ func resourceAwsAlbTargetGroup() *schema.Resource { Computed: true, ForceNew: true, ConflictsWith: []string{"name_prefix"}, - ValidateFunc: validateAwsAlbTargetGroupName, + ValidateFunc: validateAwsLbTargetGroupName, }, "name_prefix": { Type: schema.TypeString, Optional: true, ForceNew: true, - ValidateFunc: validateAwsAlbTargetGroupNamePrefix, + ValidateFunc: validateAwsLbTargetGroupNamePrefix, }, "port": { Type: schema.TypeInt, Required: true, ForceNew: true, - ValidateFunc: validateAwsAlbTargetGroupPort, + ValidateFunc: validateAwsLbTargetGroupPort, }, "protocol": { Type: schema.TypeString, Required: true, ForceNew: true, - ValidateFunc: validateAwsAlbTargetGroupProtocol, + ValidateFunc: validateAwsLbTargetGroupProtocol, }, "vpc_id": { @@ -76,7 +76,7 @@ func resourceAwsAlbTargetGroup() *schema.Resource { Type: schema.TypeInt, Optional: true, Default: 300, - ValidateFunc: validateAwsAlbTargetGroupDeregistrationDelay, + ValidateFunc: validateAwsLbTargetGroupDeregistrationDelay, }, "stickiness": { @@ -94,13 +94,13 @@ func resourceAwsAlbTargetGroup() *schema.Resource { "type": { Type: schema.TypeString, Required: true, - ValidateFunc: validateAwsAlbTargetGroupStickinessType, + ValidateFunc: validateAwsLbTargetGroupStickinessType, }, "cookie_duration": { Type: schema.TypeInt, Optional: true, Default: 86400, - ValidateFunc: validateAwsAlbTargetGroupStickinessCookieDuration, + ValidateFunc: validateAwsLbTargetGroupStickinessCookieDuration, }, }, }, @@ -123,14 +123,14 @@ func resourceAwsAlbTargetGroup() *schema.Resource { Type: schema.TypeString, Optional: true, Default: "/", - ValidateFunc: validateAwsAlbTargetGroupHealthCheckPath, + ValidateFunc: validateAwsLbTargetGroupHealthCheckPath, }, "port": { Type: schema.TypeString, Optional: true, Default: "traffic-port", - ValidateFunc: validateAwsAlbTargetGroupHealthCheckPort, + ValidateFunc: validateAwsLbTargetGroupHealthCheckPort, }, "protocol": { @@ -140,21 +140,21 @@ func resourceAwsAlbTargetGroup() *schema.Resource { StateFunc: func(v interface{}) string { return strings.ToUpper(v.(string)) }, - ValidateFunc: validateAwsAlbTargetGroupHealthCheckProtocol, + ValidateFunc: validateAwsLbTargetGroupHealthCheckProtocol, }, "timeout": { Type: schema.TypeInt, Optional: true, Default: 5, - ValidateFunc: validateAwsAlbTargetGroupHealthCheckTimeout, + ValidateFunc: validateAwsLbTargetGroupHealthCheckTimeout, }, "healthy_threshold": { Type: schema.TypeInt, Optional: true, Default: 5, - ValidateFunc: validateAwsAlbTargetGroupHealthCheckHealthyThreshold, + ValidateFunc: validateAwsLbTargetGroupHealthCheckHealthyThreshold, }, "matcher": { @@ -167,7 +167,7 @@ func resourceAwsAlbTargetGroup() *schema.Resource { Type: schema.TypeInt, Optional: true, Default: 2, - ValidateFunc: validateAwsAlbTargetGroupHealthCheckHealthyThreshold, + ValidateFunc: validateAwsLbTargetGroupHealthCheckHealthyThreshold, }, }, }, @@ -178,7 +178,7 @@ func resourceAwsAlbTargetGroup() *schema.Resource { } } -func resourceAwsAlbTargetGroupCreate(d *schema.ResourceData, meta interface{}) error { +func resourceAwsLbTargetGroupCreate(d *schema.ResourceData, meta interface{}) error { elbconn := meta.(*AWSClient).elbv2conn var groupName string @@ -214,20 +214,20 @@ func resourceAwsAlbTargetGroupCreate(d *schema.ResourceData, meta interface{}) e resp, err := elbconn.CreateTargetGroup(params) if err != nil { - return errwrap.Wrapf("Error creating ALB Target Group: {{err}}", err) + return errwrap.Wrapf("Error creating LB Target Group: {{err}}", err) } if len(resp.TargetGroups) == 0 { - return errors.New("Error creating ALB Target Group: no groups returned in response") + return errors.New("Error creating LB Target Group: no groups returned in response") } targetGroupArn := resp.TargetGroups[0].TargetGroupArn d.SetId(*targetGroupArn) - return resourceAwsAlbTargetGroupUpdate(d, meta) + return resourceAwsLbTargetGroupUpdate(d, meta) } -func resourceAwsAlbTargetGroupRead(d *schema.ResourceData, meta interface{}) error { +func resourceAwsLbTargetGroupRead(d *schema.ResourceData, meta interface{}) error { elbconn := meta.(*AWSClient).elbv2conn resp, err := elbconn.DescribeTargetGroups(&elbv2.DescribeTargetGroupsInput{ @@ -246,14 +246,14 @@ func resourceAwsAlbTargetGroupRead(d *schema.ResourceData, meta interface{}) err return fmt.Errorf("Error retrieving Target Group %q", d.Id()) } - return flattenAwsAlbTargetGroupResource(d, meta, resp.TargetGroups[0]) + return flattenAwsLbTargetGroupResource(d, meta, resp.TargetGroups[0]) } -func resourceAwsAlbTargetGroupUpdate(d *schema.ResourceData, meta interface{}) error { +func resourceAwsLbTargetGroupUpdate(d *schema.ResourceData, meta interface{}) error { elbconn := meta.(*AWSClient).elbv2conn if err := setElbV2Tags(elbconn, d); err != nil { - return errwrap.Wrapf("Error Modifying Tags on ALB Target Group: {{err}}", err) + return errwrap.Wrapf("Error Modifying Tags on LB Target Group: {{err}}", err) } if d.HasChange("health_check") { @@ -335,10 +335,10 @@ func resourceAwsAlbTargetGroupUpdate(d *schema.ResourceData, meta interface{}) e } } - return resourceAwsAlbTargetGroupRead(d, meta) + return resourceAwsLbTargetGroupRead(d, meta) } -func resourceAwsAlbTargetGroupDelete(d *schema.ResourceData, meta interface{}) error { +func resourceAwsLbTargetGroupDelete(d *schema.ResourceData, meta interface{}) error { elbconn := meta.(*AWSClient).elbv2conn _, err := elbconn.DeleteTargetGroup(&elbv2.DeleteTargetGroupInput{ @@ -356,7 +356,7 @@ func isTargetGroupNotFound(err error) bool { return ok && elberr.Code() == "TargetGroupNotFound" } -func validateAwsAlbTargetGroupHealthCheckPath(v interface{}, k string) (ws []string, errors []error) { +func validateAwsLbTargetGroupHealthCheckPath(v interface{}, k string) (ws []string, errors []error) { value := v.(string) if len(value) > 1024 { errors = append(errors, fmt.Errorf( @@ -365,7 +365,7 @@ func validateAwsAlbTargetGroupHealthCheckPath(v interface{}, k string) (ws []str return } -func validateAwsAlbTargetGroupHealthCheckPort(v interface{}, k string) (ws []string, errors []error) { +func validateAwsLbTargetGroupHealthCheckPort(v interface{}, k string) (ws []string, errors []error) { value := v.(string) if value == "traffic-port" { @@ -384,7 +384,7 @@ func validateAwsAlbTargetGroupHealthCheckPort(v interface{}, k string) (ws []str return } -func validateAwsAlbTargetGroupHealthCheckHealthyThreshold(v interface{}, k string) (ws []string, errors []error) { +func validateAwsLbTargetGroupHealthCheckHealthyThreshold(v interface{}, k string) (ws []string, errors []error) { value := v.(int) if value < 2 || value > 10 { errors = append(errors, fmt.Errorf("%q must be an integer between 2 and 10", k)) @@ -392,7 +392,7 @@ func validateAwsAlbTargetGroupHealthCheckHealthyThreshold(v interface{}, k strin return } -func validateAwsAlbTargetGroupHealthCheckTimeout(v interface{}, k string) (ws []string, errors []error) { +func validateAwsLbTargetGroupHealthCheckTimeout(v interface{}, k string) (ws []string, errors []error) { value := v.(int) if value < 2 || value > 60 { errors = append(errors, fmt.Errorf("%q must be an integer between 2 and 60", k)) @@ -400,7 +400,7 @@ func validateAwsAlbTargetGroupHealthCheckTimeout(v interface{}, k string) (ws [] return } -func validateAwsAlbTargetGroupHealthCheckProtocol(v interface{}, k string) (ws []string, errors []error) { +func validateAwsLbTargetGroupHealthCheckProtocol(v interface{}, k string) (ws []string, errors []error) { value := strings.ToLower(v.(string)) if value == "http" || value == "https" { return @@ -410,7 +410,7 @@ func validateAwsAlbTargetGroupHealthCheckProtocol(v interface{}, k string) (ws [ return } -func validateAwsAlbTargetGroupPort(v interface{}, k string) (ws []string, errors []error) { +func validateAwsLbTargetGroupPort(v interface{}, k string) (ws []string, errors []error) { port := v.(int) if port < 1 || port > 65536 { errors = append(errors, fmt.Errorf("%q must be a valid port number (1-65536)", k)) @@ -418,7 +418,7 @@ func validateAwsAlbTargetGroupPort(v interface{}, k string) (ws []string, errors return } -func validateAwsAlbTargetGroupProtocol(v interface{}, k string) (ws []string, errors []error) { +func validateAwsLbTargetGroupProtocol(v interface{}, k string) (ws []string, errors []error) { protocol := strings.ToLower(v.(string)) if protocol == "http" || protocol == "https" { return @@ -428,7 +428,7 @@ func validateAwsAlbTargetGroupProtocol(v interface{}, k string) (ws []string, er return } -func validateAwsAlbTargetGroupDeregistrationDelay(v interface{}, k string) (ws []string, errors []error) { +func validateAwsLbTargetGroupDeregistrationDelay(v interface{}, k string) (ws []string, errors []error) { delay := v.(int) if delay < 0 || delay > 3600 { errors = append(errors, fmt.Errorf("%q must be in the range 0-3600 seconds", k)) @@ -436,7 +436,7 @@ func validateAwsAlbTargetGroupDeregistrationDelay(v interface{}, k string) (ws [ return } -func validateAwsAlbTargetGroupStickinessType(v interface{}, k string) (ws []string, errors []error) { +func validateAwsLbTargetGroupStickinessType(v interface{}, k string) (ws []string, errors []error) { stickinessType := v.(string) if stickinessType != "lb_cookie" { errors = append(errors, fmt.Errorf("%q must have the value %q", k, "lb_cookie")) @@ -444,7 +444,7 @@ func validateAwsAlbTargetGroupStickinessType(v interface{}, k string) (ws []stri return } -func validateAwsAlbTargetGroupStickinessCookieDuration(v interface{}, k string) (ws []string, errors []error) { +func validateAwsLbTargetGroupStickinessCookieDuration(v interface{}, k string) (ws []string, errors []error) { duration := v.(int) if duration < 1 || duration > 604800 { errors = append(errors, fmt.Errorf("%q must be a between 1 second and 1 week (1-604800 seconds))", k)) @@ -452,7 +452,7 @@ func validateAwsAlbTargetGroupStickinessCookieDuration(v interface{}, k string) return } -func albTargetGroupSuffixFromARN(arn *string) string { +func lbTargetGroupSuffixFromARN(arn *string) string { if arn == nil { return "" } @@ -466,12 +466,12 @@ func albTargetGroupSuffixFromARN(arn *string) string { return "" } -// flattenAwsAlbTargetGroupResource takes a *elbv2.TargetGroup and populates all respective resource fields. -func flattenAwsAlbTargetGroupResource(d *schema.ResourceData, meta interface{}, targetGroup *elbv2.TargetGroup) error { +// flattenAwsLbTargetGroupResource takes a *elbv2.TargetGroup and populates all respective resource fields. +func flattenAwsLbTargetGroupResource(d *schema.ResourceData, meta interface{}, targetGroup *elbv2.TargetGroup) error { elbconn := meta.(*AWSClient).elbv2conn d.Set("arn", targetGroup.TargetGroupArn) - d.Set("arn_suffix", albTargetGroupSuffixFromARN(targetGroup.TargetGroupArn)) + d.Set("arn_suffix", lbTargetGroupSuffixFromARN(targetGroup.TargetGroupArn)) d.Set("name", targetGroup.TargetGroupName) d.Set("port", targetGroup.Port) d.Set("protocol", targetGroup.Protocol) diff --git a/aws/resource_aws_alb_target_group_attachment.go b/aws/resource_aws_lb_target_group_attachment.go similarity index 85% rename from aws/resource_aws_alb_target_group_attachment.go rename to aws/resource_aws_lb_target_group_attachment.go index 55a3b739239..bfa54d8892c 100644 --- a/aws/resource_aws_alb_target_group_attachment.go +++ b/aws/resource_aws_lb_target_group_attachment.go @@ -12,11 +12,11 @@ import ( "github.com/hashicorp/terraform/helper/schema" ) -func resourceAwsAlbTargetGroupAttachment() *schema.Resource { +func resourceAwsLbTargetGroupAttachment() *schema.Resource { return &schema.Resource{ - Create: resourceAwsAlbAttachmentCreate, - Read: resourceAwsAlbAttachmentRead, - Delete: resourceAwsAlbAttachmentDelete, + Create: resourceAwsLbAttachmentCreate, + Read: resourceAwsLbAttachmentRead, + Delete: resourceAwsLbAttachmentDelete, Schema: map[string]*schema.Schema{ "target_group_arn": { @@ -40,7 +40,7 @@ func resourceAwsAlbTargetGroupAttachment() *schema.Resource { } } -func resourceAwsAlbAttachmentCreate(d *schema.ResourceData, meta interface{}) error { +func resourceAwsLbAttachmentCreate(d *schema.ResourceData, meta interface{}) error { elbconn := meta.(*AWSClient).elbv2conn target := &elbv2.TargetDescription{ @@ -69,7 +69,7 @@ func resourceAwsAlbAttachmentCreate(d *schema.ResourceData, meta interface{}) er return nil } -func resourceAwsAlbAttachmentDelete(d *schema.ResourceData, meta interface{}) error { +func resourceAwsLbAttachmentDelete(d *schema.ResourceData, meta interface{}) error { elbconn := meta.(*AWSClient).elbv2conn target := &elbv2.TargetDescription{ @@ -95,9 +95,9 @@ func resourceAwsAlbAttachmentDelete(d *schema.ResourceData, meta interface{}) er return nil } -// resourceAwsAlbAttachmentRead requires all of the fields in order to describe the correct +// resourceAwsLbAttachmentRead requires all of the fields in order to describe the correct // target, so there is no work to do beyond ensuring that the target and group still exist. -func resourceAwsAlbAttachmentRead(d *schema.ResourceData, meta interface{}) error { +func resourceAwsLbAttachmentRead(d *schema.ResourceData, meta interface{}) error { elbconn := meta.(*AWSClient).elbv2conn target := &elbv2.TargetDescription{ diff --git a/aws/resource_aws_alb_target_group_attachment_test.go b/aws/resource_aws_lb_target_group_attachment_test.go similarity index 59% rename from aws/resource_aws_alb_target_group_attachment_test.go rename to aws/resource_aws_lb_target_group_attachment_test.go index 6ab8cab7364..4617c55fd00 100644 --- a/aws/resource_aws_alb_target_group_attachment_test.go +++ b/aws/resource_aws_lb_target_group_attachment_test.go @@ -14,45 +14,64 @@ import ( "github.com/hashicorp/terraform/terraform" ) -func TestAccAWSALBTargetGroupAttachment_basic(t *testing.T) { +func TestAccAWSLBTargetGroupAttachment_basic(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_alb_target_group.test", + IDRefreshName: "aws_lb_target_group.test", Providers: testAccProviders, - CheckDestroy: testAccCheckAWSALBTargetGroupAttachmentDestroy, + CheckDestroy: testAccCheckAWSLBTargetGroupAttachmentDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSALBTargetGroupAttachmentConfig_basic(targetGroupName), + Config: testAccAWSLBTargetGroupAttachmentConfig_basic(targetGroupName), Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBTargetGroupAttachmentExists("aws_alb_target_group_attachment.test"), + testAccCheckAWSLBTargetGroupAttachmentExists("aws_lb_target_group_attachment.test"), ), }, }, }) } -func TestAccAWSALBTargetGroupAttachment_withoutPort(t *testing.T) { +func TestAccAWSLBTargetGroupAttachmentBackwardsCompatibility(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_alb_target_group.test", Providers: testAccProviders, - CheckDestroy: testAccCheckAWSALBTargetGroupAttachmentDestroy, + CheckDestroy: testAccCheckAWSLBTargetGroupAttachmentDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSALBTargetGroupAttachmentConfigWithoutPort(targetGroupName), + Config: testAccAWSLBTargetGroupAttachmentConfigBackwardsCompatibility(targetGroupName), Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBTargetGroupAttachmentExists("aws_alb_target_group_attachment.test"), + testAccCheckAWSLBTargetGroupAttachmentExists("aws_alb_target_group_attachment.test"), ), }, }, }) } -func testAccCheckAWSALBTargetGroupAttachmentExists(n string) resource.TestCheckFunc { +func TestAccAWSLBTargetGroupAttachment_withoutPort(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: testAccAWSLBTargetGroupAttachmentConfigWithoutPort(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] if !ok { @@ -93,11 +112,11 @@ func testAccCheckAWSALBTargetGroupAttachmentExists(n string) resource.TestCheckF } } -func testAccCheckAWSALBTargetGroupAttachmentDestroy(s *terraform.State) error { +func testAccCheckAWSLBTargetGroupAttachmentDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).elbv2conn for _, rs := range s.RootModule().Resources { - if rs.Type != "aws_alb_target_group_attachment" { + if rs.Type != "aws_lb_target_group_attachment" && rs.Type != "aws_alb_target_group_attachment" { continue } @@ -126,17 +145,17 @@ func testAccCheckAWSALBTargetGroupAttachmentDestroy(s *terraform.State) error { if isTargetGroupNotFound(err) || isInvalidTarget(err) { return nil } else { - return errwrap.Wrapf("Unexpected error checking ALB destroyed: {{err}}", err) + return errwrap.Wrapf("Unexpected error checking LB destroyed: {{err}}", err) } } return nil } -func testAccAWSALBTargetGroupAttachmentConfigWithoutPort(targetGroupName string) string { +func testAccAWSLBTargetGroupAttachmentConfigWithoutPort(targetGroupName string) string { return fmt.Sprintf(` -resource "aws_alb_target_group_attachment" "test" { - target_group_arn = "${aws_alb_target_group.test.arn}" +resource "aws_lb_target_group_attachment" "test" { + target_group_arn = "${aws_lb_target_group.test.arn}" target_id = "${aws_instance.test.id}" } @@ -146,7 +165,60 @@ resource "aws_instance" "test" { subnet_id = "${aws_subnet.subnet.id}" } -resource "aws_alb_target_group" "test" { +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 + 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 = "testAccAWSLBTargetGroupAttachmentConfigWithoutPort" + } +}`, targetGroupName) +} + +func testAccAWSLBTargetGroupAttachmentConfig_basic(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.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" @@ -180,12 +252,12 @@ resource "aws_subnet" "subnet" { resource "aws_vpc" "test" { cidr_block = "10.0.0.0/16" tags { - Name = "testAccAWSALBTargetGroupAttachmentConfigWithoutPort" + Name = "testAccAWSLBTargetGroupAttachmentConfig_basic" } }`, targetGroupName) } -func testAccAWSALBTargetGroupAttachmentConfig_basic(targetGroupName string) string { +func testAccAWSLBTargetGroupAttachmentConfigBackwardsCompatibility(targetGroupName string) string { return fmt.Sprintf(` resource "aws_alb_target_group_attachment" "test" { target_group_arn = "${aws_alb_target_group.test.arn}" @@ -233,7 +305,7 @@ resource "aws_subnet" "subnet" { resource "aws_vpc" "test" { cidr_block = "10.0.0.0/16" tags { - Name = "testAccAWSALBTargetGroupAttachmentConfig_basic" + Name = "testAccAWSLBTargetGroupAttachmentConfig_basic" } }`, targetGroupName) } diff --git a/aws/resource_aws_lb_target_group_test.go b/aws/resource_aws_lb_target_group_test.go new file mode 100644 index 00000000000..029056c40f5 --- /dev/null +++ b/aws/resource_aws_lb_target_group_test.go @@ -0,0 +1,868 @@ +package aws + +import ( + "errors" + "fmt" + "regexp" + "testing" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/elbv2" + "github.com/hashicorp/errwrap" + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestLBTargetGroupCloudwatchSuffixFromARN(t *testing.T) { + cases := []struct { + name string + arn *string + suffix string + }{ + { + name: "valid suffix", + arn: aws.String(`arn:aws:elasticloadbalancing:us-east-1:123456:targetgroup/my-targets/73e2d6bc24d8a067`), + suffix: `targetgroup/my-targets/73e2d6bc24d8a067`, + }, + { + name: "no suffix", + arn: aws.String(`arn:aws:elasticloadbalancing:us-east-1:123456:targetgroup`), + suffix: ``, + }, + { + name: "nil ARN", + arn: nil, + suffix: ``, + }, + } + + for _, tc := range cases { + actual := lbTargetGroupSuffixFromARN(tc.arn) + if actual != tc.suffix { + t.Fatalf("bad suffix: %q\nExpected: %s\n Got: %s", tc.name, tc.suffix, actual) + } + } +} + +func TestAccAWSLBTargetGroup_basic(t *testing.T) { + var conf elbv2.TargetGroup + 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: testAccCheckAWSLBTargetGroupDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSLBTargetGroupConfig_basic(targetGroupName), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSLBTargetGroupExists("aws_lb_target_group.test", &conf), + resource.TestCheckResourceAttrSet("aws_lb_target_group.test", "arn"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "name", targetGroupName), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "port", "443"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "protocol", "HTTPS"), + resource.TestCheckResourceAttrSet("aws_lb_target_group.test", "vpc_id"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "deregistration_delay", "200"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "stickiness.#", "1"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "stickiness.0.enabled", "true"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "stickiness.0.type", "lb_cookie"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "stickiness.0.cookie_duration", "10000"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.#", "1"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.path", "/health"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.interval", "60"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.port", "8081"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.protocol", "HTTP"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.timeout", "3"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.healthy_threshold", "3"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.unhealthy_threshold", "3"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.matcher", "200-299"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "tags.%", "1"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "tags.TestName", "TestAccAWSLBTargetGroup_basic"), + ), + }, + }, + }) +} + +func TestAccAWSLBTargetGroupBackwardsCompatibility(t *testing.T) { + var conf elbv2.TargetGroup + targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + IDRefreshName: "aws_alb_target_group.test", + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSLBTargetGroupDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSLBTargetGroupConfigBackwardsCompatibility(targetGroupName), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSLBTargetGroupExists("aws_alb_target_group.test", &conf), + resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "arn"), + resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupName), + resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "443"), + resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTPS"), + resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "vpc_id"), + resource.TestCheckResourceAttr("aws_alb_target_group.test", "deregistration_delay", "200"), + resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.#", "1"), + resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.enabled", "true"), + resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.type", "lb_cookie"), + resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.cookie_duration", "10000"), + resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.#", "1"), + resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.path", "/health"), + resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.interval", "60"), + resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.port", "8081"), + resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.protocol", "HTTP"), + resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.timeout", "3"), + resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.healthy_threshold", "3"), + resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.unhealthy_threshold", "3"), + resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.matcher", "200-299"), + resource.TestCheckResourceAttr("aws_alb_target_group.test", "tags.%", "1"), + resource.TestCheckResourceAttr("aws_alb_target_group.test", "tags.TestName", "TestAccAWSLBTargetGroup_basic"), + ), + }, + }, + }) +} + +func TestAccAWSLBTargetGroup_namePrefix(t *testing.T) { + var conf elbv2.TargetGroup + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + IDRefreshName: "aws_lb_target_group.test", + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSLBTargetGroupDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSLBTargetGroupConfig_namePrefix, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSLBTargetGroupExists("aws_lb_target_group.test", &conf), + resource.TestMatchResourceAttr("aws_lb_target_group.test", "name", regexp.MustCompile("^tf-")), + ), + }, + }, + }) +} + +func TestAccAWSLBTargetGroup_generatedName(t *testing.T) { + var conf elbv2.TargetGroup + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + IDRefreshName: "aws_lb_target_group.test", + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSLBTargetGroupDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSLBTargetGroupConfig_generatedName, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSLBTargetGroupExists("aws_lb_target_group.test", &conf), + ), + }, + }, + }) +} + +func TestAccAWSLBTargetGroup_changeNameForceNew(t *testing.T) { + var before, after elbv2.TargetGroup + targetGroupNameBefore := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) + targetGroupNameAfter := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(4, acctest.CharSetAlphaNum)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + IDRefreshName: "aws_lb_target_group.test", + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSLBTargetGroupDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSLBTargetGroupConfig_basic(targetGroupNameBefore), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSLBTargetGroupExists("aws_lb_target_group.test", &before), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "name", targetGroupNameBefore), + ), + }, + { + Config: testAccAWSLBTargetGroupConfig_basic(targetGroupNameAfter), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSLBTargetGroupExists("aws_lb_target_group.test", &after), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "name", targetGroupNameAfter), + ), + }, + }, + }) +} + +func TestAccAWSLBTargetGroup_changeProtocolForceNew(t *testing.T) { + var before, after elbv2.TargetGroup + 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: testAccCheckAWSLBTargetGroupDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSLBTargetGroupConfig_basic(targetGroupName), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSLBTargetGroupExists("aws_lb_target_group.test", &before), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "protocol", "HTTPS"), + ), + }, + { + Config: testAccAWSLBTargetGroupConfig_updatedProtocol(targetGroupName), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSLBTargetGroupExists("aws_lb_target_group.test", &after), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "protocol", "HTTP"), + ), + }, + }, + }) +} + +func TestAccAWSLBTargetGroup_changePortForceNew(t *testing.T) { + var before, after elbv2.TargetGroup + 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: testAccCheckAWSLBTargetGroupDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSLBTargetGroupConfig_basic(targetGroupName), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSLBTargetGroupExists("aws_lb_target_group.test", &before), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "port", "443"), + ), + }, + { + Config: testAccAWSLBTargetGroupConfig_updatedPort(targetGroupName), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSLBTargetGroupExists("aws_lb_target_group.test", &after), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "port", "442"), + ), + }, + }, + }) +} + +func TestAccAWSLBTargetGroup_changeVpcForceNew(t *testing.T) { + var before, after elbv2.TargetGroup + 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: testAccCheckAWSLBTargetGroupDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSLBTargetGroupConfig_basic(targetGroupName), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSLBTargetGroupExists("aws_lb_target_group.test", &before), + ), + }, + { + Config: testAccAWSLBTargetGroupConfig_updatedVpc(targetGroupName), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSLBTargetGroupExists("aws_lb_target_group.test", &after), + ), + }, + }, + }) +} + +func TestAccAWSLBTargetGroup_tags(t *testing.T) { + var conf elbv2.TargetGroup + 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: testAccCheckAWSLBTargetGroupDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSLBTargetGroupConfig_basic(targetGroupName), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSLBTargetGroupExists("aws_lb_target_group.test", &conf), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "tags.%", "1"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "tags.TestName", "TestAccAWSLBTargetGroup_basic"), + ), + }, + { + Config: testAccAWSLBTargetGroupConfig_updateTags(targetGroupName), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSLBTargetGroupExists("aws_lb_target_group.test", &conf), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "tags.%", "2"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "tags.Environment", "Production"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "tags.Type", "ALB Target Group"), + ), + }, + }, + }) +} + +func TestAccAWSLBTargetGroup_updateHealthCheck(t *testing.T) { + var conf elbv2.TargetGroup + 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: testAccCheckAWSLBTargetGroupDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSLBTargetGroupConfig_basic(targetGroupName), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSLBTargetGroupExists("aws_lb_target_group.test", &conf), + resource.TestCheckResourceAttrSet("aws_lb_target_group.test", "arn"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "name", targetGroupName), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "port", "443"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "protocol", "HTTPS"), + resource.TestCheckResourceAttrSet("aws_lb_target_group.test", "vpc_id"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "deregistration_delay", "200"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "stickiness.#", "1"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "stickiness.0.type", "lb_cookie"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "stickiness.0.cookie_duration", "10000"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.#", "1"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.path", "/health"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.interval", "60"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.port", "8081"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.protocol", "HTTP"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.timeout", "3"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.healthy_threshold", "3"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.unhealthy_threshold", "3"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.matcher", "200-299"), + ), + }, + { + Config: testAccAWSLBTargetGroupConfig_updateHealthCheck(targetGroupName), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSLBTargetGroupExists("aws_lb_target_group.test", &conf), + resource.TestCheckResourceAttrSet("aws_lb_target_group.test", "arn"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "name", targetGroupName), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "port", "443"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "protocol", "HTTPS"), + resource.TestCheckResourceAttrSet("aws_lb_target_group.test", "vpc_id"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "deregistration_delay", "200"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "stickiness.#", "1"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "stickiness.0.type", "lb_cookie"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "stickiness.0.cookie_duration", "10000"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.#", "1"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.path", "/health2"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.interval", "30"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.port", "8082"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.protocol", "HTTPS"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.timeout", "4"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.healthy_threshold", "4"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.unhealthy_threshold", "4"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.matcher", "200"), + ), + }, + }, + }) +} + +func TestAccAWSLBTargetGroup_updateSticknessEnabled(t *testing.T) { + var conf elbv2.TargetGroup + 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: testAccCheckAWSLBTargetGroupDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSLBTargetGroupConfig_stickiness(targetGroupName, false, false), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSLBTargetGroupExists("aws_lb_target_group.test", &conf), + resource.TestCheckResourceAttrSet("aws_lb_target_group.test", "arn"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "name", targetGroupName), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "port", "443"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "protocol", "HTTPS"), + resource.TestCheckResourceAttrSet("aws_lb_target_group.test", "vpc_id"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "deregistration_delay", "200"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.#", "1"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.path", "/health2"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.interval", "30"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.port", "8082"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.protocol", "HTTPS"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.timeout", "4"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.healthy_threshold", "4"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.unhealthy_threshold", "4"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.matcher", "200"), + ), + }, + { + Config: testAccAWSLBTargetGroupConfig_stickiness(targetGroupName, true, true), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSLBTargetGroupExists("aws_lb_target_group.test", &conf), + resource.TestCheckResourceAttrSet("aws_lb_target_group.test", "arn"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "name", targetGroupName), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "port", "443"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "protocol", "HTTPS"), + resource.TestCheckResourceAttrSet("aws_lb_target_group.test", "vpc_id"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "deregistration_delay", "200"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "stickiness.#", "1"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "stickiness.0.enabled", "true"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "stickiness.0.type", "lb_cookie"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "stickiness.0.cookie_duration", "10000"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.#", "1"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.path", "/health2"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.interval", "30"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.port", "8082"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.protocol", "HTTPS"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.timeout", "4"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.healthy_threshold", "4"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.unhealthy_threshold", "4"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.matcher", "200"), + ), + }, + { + Config: testAccAWSLBTargetGroupConfig_stickiness(targetGroupName, true, false), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSLBTargetGroupExists("aws_lb_target_group.test", &conf), + resource.TestCheckResourceAttrSet("aws_lb_target_group.test", "arn"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "name", targetGroupName), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "port", "443"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "protocol", "HTTPS"), + resource.TestCheckResourceAttrSet("aws_lb_target_group.test", "vpc_id"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "deregistration_delay", "200"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "stickiness.#", "1"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "stickiness.0.enabled", "false"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "stickiness.0.type", "lb_cookie"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "stickiness.0.cookie_duration", "10000"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.#", "1"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.path", "/health2"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.interval", "30"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.port", "8082"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.protocol", "HTTPS"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.timeout", "4"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.healthy_threshold", "4"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.unhealthy_threshold", "4"), + resource.TestCheckResourceAttr("aws_lb_target_group.test", "health_check.0.matcher", "200"), + ), + }, + }, + }) +} + +func testAccCheckAWSLBTargetGroupExists(n string, res *elbv2.TargetGroup) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return errors.New("No Target Group ID is set") + } + + conn := testAccProvider.Meta().(*AWSClient).elbv2conn + + describe, err := conn.DescribeTargetGroups(&elbv2.DescribeTargetGroupsInput{ + TargetGroupArns: []*string{aws.String(rs.Primary.ID)}, + }) + + if err != nil { + return err + } + + if len(describe.TargetGroups) != 1 || + *describe.TargetGroups[0].TargetGroupArn != rs.Primary.ID { + return errors.New("Target Group not found") + } + + *res = *describe.TargetGroups[0] + return nil + } +} + +func testAccCheckAWSLBTargetGroupDestroy(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).elbv2conn + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_lb_target_group" && rs.Type != "aws_alb_target_group" { + continue + } + + describe, err := conn.DescribeTargetGroups(&elbv2.DescribeTargetGroupsInput{ + TargetGroupArns: []*string{aws.String(rs.Primary.ID)}, + }) + + if err == nil { + if len(describe.TargetGroups) != 0 && + *describe.TargetGroups[0].TargetGroupArn == rs.Primary.ID { + return fmt.Errorf("Target Group %q still exists", rs.Primary.ID) + } + } + + // Verify the error + if isTargetGroupNotFound(err) { + return nil + } else { + return errwrap.Wrapf("Unexpected error checking ALB destroyed: {{err}}", err) + } + } + + return nil +} + +func testAccAWSLBTargetGroupConfig_basic(targetGroupName string) string { + return fmt.Sprintf(`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 + port = 8081 + protocol = "HTTP" + timeout = 3 + healthy_threshold = 3 + unhealthy_threshold = 3 + matcher = "200-299" + } + + tags { + TestName = "TestAccAWSLBTargetGroup_basic" + } +} + +resource "aws_vpc" "test" { + cidr_block = "10.0.0.0/16" + + tags { + TestName = "TestAccAWSLBTargetGroup_basic" + } +}`, targetGroupName) +} + +func testAccAWSLBTargetGroupConfigBackwardsCompatibility(targetGroupName string) string { + return fmt.Sprintf(`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 + port = 8081 + protocol = "HTTP" + timeout = 3 + healthy_threshold = 3 + unhealthy_threshold = 3 + matcher = "200-299" + } + + tags { + TestName = "TestAccAWSLBTargetGroup_basic" + } +} + +resource "aws_vpc" "test" { + cidr_block = "10.0.0.0/16" + + tags { + TestName = "TestAccAWSLBTargetGroup_basic" + } +}`, targetGroupName) +} + +func testAccAWSLBTargetGroupConfig_updatedPort(targetGroupName string) string { + return fmt.Sprintf(`resource "aws_lb_target_group" "test" { + name = "%s" + port = 442 + protocol = "HTTPS" + vpc_id = "${aws_vpc.test.id}" + + 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" + } + + tags { + TestName = "TestAccAWSLBTargetGroup_basic" + } +} + +resource "aws_vpc" "test" { + cidr_block = "10.0.0.0/16" + + tags { + TestName = "TestAccAWSLBTargetGroup_basic" + } +}`, targetGroupName) +} + +func testAccAWSLBTargetGroupConfig_updatedProtocol(targetGroupName string) string { + return fmt.Sprintf(`resource "aws_lb_target_group" "test" { + name = "%s" + port = 443 + protocol = "HTTP" + vpc_id = "${aws_vpc.test2.id}" + + 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" + } + + tags { + TestName = "TestAccAWSLBTargetGroup_basic" + } +} + +resource "aws_vpc" "test2" { + cidr_block = "10.10.0.0/16" + + tags { + TestName = "TestAccAWSLBTargetGroup_basic" + } +} + +resource "aws_vpc" "test" { + cidr_block = "10.0.0.0/16" + + tags { + TestName = "TestAccAWSLBTargetGroup_basic" + } +}`, targetGroupName) +} + +func testAccAWSLBTargetGroupConfig_updatedVpc(targetGroupName string) string { + return fmt.Sprintf(`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 + port = 8081 + protocol = "HTTP" + timeout = 3 + healthy_threshold = 3 + unhealthy_threshold = 3 + matcher = "200-299" + } + + tags { + TestName = "TestAccAWSLBTargetGroup_basic" + } +} + +resource "aws_vpc" "test" { + cidr_block = "10.0.0.0/16" + + tags { + TestName = "TestAccAWSLBTargetGroup_basic" + } +}`, targetGroupName) +} + +func testAccAWSLBTargetGroupConfig_updateTags(targetGroupName string) string { + return fmt.Sprintf(`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 + port = 8081 + protocol = "HTTP" + timeout = 3 + healthy_threshold = 3 + unhealthy_threshold = 3 + matcher = "200-299" + } + + tags { + Environment = "Production" + Type = "ALB Target Group" + } +} + +resource "aws_vpc" "test" { + cidr_block = "10.0.0.0/16" + + tags { + TestName = "TestAccAWSLBTargetGroup_basic" + } +}`, targetGroupName) +} + +func testAccAWSLBTargetGroupConfig_updateHealthCheck(targetGroupName string) string { + return fmt.Sprintf(`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 = "/health2" + interval = 30 + port = 8082 + protocol = "HTTPS" + timeout = 4 + healthy_threshold = 4 + unhealthy_threshold = 4 + matcher = "200" + } +} + +resource "aws_vpc" "test" { + cidr_block = "10.0.0.0/16" + + tags { + TestName = "TestAccAWSLBTargetGroup_basic" + } +}`, targetGroupName) +} + +func testAccAWSLBTargetGroupConfig_stickiness(targetGroupName string, addStickinessBlock bool, enabled bool) string { + var stickinessBlock string + + if addStickinessBlock { + stickinessBlock = fmt.Sprintf(`stickiness { + enabled = "%t" + type = "lb_cookie" + cookie_duration = 10000 + }`, enabled) + } + + return fmt.Sprintf(`resource "aws_lb_target_group" "test" { + name = "%s" + port = 443 + protocol = "HTTPS" + vpc_id = "${aws_vpc.test.id}" + + deregistration_delay = 200 + + %s + + health_check { + path = "/health2" + interval = 30 + port = 8082 + protocol = "HTTPS" + timeout = 4 + healthy_threshold = 4 + unhealthy_threshold = 4 + matcher = "200" + } +} + +resource "aws_vpc" "test" { + cidr_block = "10.0.0.0/16" + + tags { + TestName = "TestAccAWSALBTargetGroup_stickiness" + } +}`, targetGroupName, stickinessBlock) +} + +const testAccAWSLBTargetGroupConfig_namePrefix = ` +resource "aws_lb_target_group" "test" { + name_prefix = "tf-" + port = 80 + protocol = "HTTP" + vpc_id = "${aws_vpc.test.id}" +} + +resource "aws_vpc" "test" { + cidr_block = "10.0.0.0/16" + tags { + Name = "testAccAWSLBTargetGroupConfig_namePrefix" + } +} +` + +const testAccAWSLBTargetGroupConfig_generatedName = ` +resource "aws_lb_target_group" "test" { + port = 80 + protocol = "HTTP" + vpc_id = "${aws_vpc.test.id}" +} + +resource "aws_vpc" "test" { + cidr_block = "10.0.0.0/16" + tags { + Name = "testAccAWSLBTargetGroupConfig_generatedName" + } +} +` diff --git a/aws/resource_aws_alb_test.go b/aws/resource_aws_lb_test.go similarity index 55% rename from aws/resource_aws_alb_test.go rename to aws/resource_aws_lb_test.go index 5e766d74b1d..c568449b151 100644 --- a/aws/resource_aws_alb_test.go +++ b/aws/resource_aws_lb_test.go @@ -14,7 +14,7 @@ import ( "github.com/hashicorp/terraform/terraform" ) -func TestALBCloudwatchSuffixFromARN(t *testing.T) { +func TestLBCloudwatchSuffixFromARN(t *testing.T) { cases := []struct { name string arn *string @@ -38,101 +38,166 @@ func TestALBCloudwatchSuffixFromARN(t *testing.T) { } for _, tc := range cases { - actual := albSuffixFromARN(tc.arn) + actual := lbSuffixFromARN(tc.arn) if actual != tc.suffix { t.Fatalf("bad suffix: %q\nExpected: %s\n Got: %s", tc.name, tc.suffix, actual) } } } -func TestAccAWSALB_basic(t *testing.T) { +func TestAccAWSLB_basic(t *testing.T) { var conf elbv2.LoadBalancer - albName := fmt.Sprintf("testaccawsalb-basic-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) + lbName := fmt.Sprintf("testaccawslb-basic-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, - IDRefreshName: "aws_alb.alb_test", + IDRefreshName: "aws_lb.lb_test", Providers: testAccProviders, - CheckDestroy: testAccCheckAWSALBDestroy, + CheckDestroy: testAccCheckAWSLBDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSALBConfig_basic(albName), + Config: testAccAWSLBConfig_basic(lbName), Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBExists("aws_alb.alb_test", &conf), - resource.TestCheckResourceAttr("aws_alb.alb_test", "name", albName), - resource.TestCheckResourceAttr("aws_alb.alb_test", "internal", "true"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "subnets.#", "2"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "security_groups.#", "1"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "tags.%", "1"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "tags.TestName", "TestAccAWSALB_basic"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "enable_deletion_protection", "false"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "idle_timeout", "30"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "ip_address_type", "ipv4"), - resource.TestCheckResourceAttrSet("aws_alb.alb_test", "vpc_id"), - resource.TestCheckResourceAttrSet("aws_alb.alb_test", "zone_id"), - resource.TestCheckResourceAttrSet("aws_alb.alb_test", "dns_name"), - resource.TestCheckResourceAttrSet("aws_alb.alb_test", "arn"), + testAccCheckAWSLBExists("aws_lb.lb_test", &conf), + resource.TestCheckResourceAttr("aws_lb.lb_test", "name", lbName), + resource.TestCheckResourceAttr("aws_lb.lb_test", "internal", "true"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "subnets.#", "2"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "security_groups.#", "1"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "tags.%", "1"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "tags.TestName", "TestAccAWSALB_basic"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "enable_deletion_protection", "false"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "idle_timeout", "30"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "ip_address_type", "ipv4"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "load_balancer_type", "application"), + resource.TestCheckResourceAttrSet("aws_lb.lb_test", "vpc_id"), + resource.TestCheckResourceAttrSet("aws_lb.lb_test", "zone_id"), + resource.TestCheckResourceAttrSet("aws_lb.lb_test", "dns_name"), + resource.TestCheckResourceAttrSet("aws_lb.lb_test", "arn"), ), }, }, }) } -func TestAccAWSALB_generatedName(t *testing.T) { +func TestAccAWSLB_networkLoadbalancer(t *testing.T) { var conf elbv2.LoadBalancer + lbName := fmt.Sprintf("testaccawslb-basic-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, - IDRefreshName: "aws_alb.alb_test", + IDRefreshName: "aws_lb.lb_test", Providers: testAccProviders, - CheckDestroy: testAccCheckAWSALBDestroy, + CheckDestroy: testAccCheckAWSLBDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSALBConfig_generatedName(), + Config: testAccAWSLBConfig_networkLoadbalancer(lbName), Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBExists("aws_alb.alb_test", &conf), - resource.TestCheckResourceAttrSet("aws_alb.alb_test", "name"), + testAccCheckAWSLBExists("aws_lb.lb_test", &conf), + resource.TestCheckResourceAttr("aws_lb.lb_test", "name", lbName), + resource.TestCheckResourceAttr("aws_lb.lb_test", "internal", "true"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "tags.%", "1"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "tags.TestName", "TestAccAWSALB_basic"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "enable_deletion_protection", "false"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "ip_address_type", "ipv4"), + resource.TestCheckResourceAttrSet("aws_lb.lb_test", "zone_id"), + resource.TestCheckResourceAttrSet("aws_lb.lb_test", "dns_name"), + resource.TestCheckResourceAttrSet("aws_lb.lb_test", "arn"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "load_balancer_type", "network"), ), }, }, }) } -func TestAccAWSALB_generatesNameForZeroValue(t *testing.T) { +func TestAccAWSLBBackwardsCompatibility(t *testing.T) { var conf elbv2.LoadBalancer + lbName := fmt.Sprintf("testaccawslb-basic-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, - IDRefreshName: "aws_alb.alb_test", + IDRefreshName: "aws_alb.lb_test", Providers: testAccProviders, - CheckDestroy: testAccCheckAWSALBDestroy, + CheckDestroy: testAccCheckAWSLBDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSALBConfig_zeroValueName(), + Config: testAccAWSLBConfigBackwardsCompatibility(lbName), Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBExists("aws_alb.alb_test", &conf), - resource.TestCheckResourceAttrSet("aws_alb.alb_test", "name"), + testAccCheckAWSLBExists("aws_alb.lb_test", &conf), + resource.TestCheckResourceAttr("aws_alb.lb_test", "name", lbName), + resource.TestCheckResourceAttr("aws_alb.lb_test", "internal", "true"), + resource.TestCheckResourceAttr("aws_alb.lb_test", "subnets.#", "2"), + resource.TestCheckResourceAttr("aws_alb.lb_test", "security_groups.#", "1"), + resource.TestCheckResourceAttr("aws_alb.lb_test", "tags.%", "1"), + resource.TestCheckResourceAttr("aws_alb.lb_test", "tags.TestName", "TestAccAWSALB_basic"), + resource.TestCheckResourceAttr("aws_alb.lb_test", "enable_deletion_protection", "false"), + resource.TestCheckResourceAttr("aws_alb.lb_test", "idle_timeout", "30"), + resource.TestCheckResourceAttr("aws_alb.lb_test", "ip_address_type", "ipv4"), + resource.TestCheckResourceAttr("aws_alb.lb_test", "load_balancer_type", "application"), + resource.TestCheckResourceAttrSet("aws_alb.lb_test", "vpc_id"), + resource.TestCheckResourceAttrSet("aws_alb.lb_test", "zone_id"), + resource.TestCheckResourceAttrSet("aws_alb.lb_test", "dns_name"), + resource.TestCheckResourceAttrSet("aws_alb.lb_test", "arn"), ), }, }, }) } -func TestAccAWSALB_namePrefix(t *testing.T) { +func TestAccAWSLB_generatedName(t *testing.T) { var conf elbv2.LoadBalancer resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, - IDRefreshName: "aws_alb.alb_test", + IDRefreshName: "aws_lb.lb_test", Providers: testAccProviders, - CheckDestroy: testAccCheckAWSALBDestroy, + CheckDestroy: testAccCheckAWSLBDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSALBConfig_namePrefix(), + Config: testAccAWSLBConfig_generatedName(), Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBExists("aws_alb.alb_test", &conf), - resource.TestCheckResourceAttrSet("aws_alb.alb_test", "name"), - resource.TestMatchResourceAttr("aws_alb.alb_test", "name", + testAccCheckAWSLBExists("aws_lb.lb_test", &conf), + resource.TestCheckResourceAttrSet("aws_lb.lb_test", "name"), + ), + }, + }, + }) +} + +func TestAccAWSLB_generatesNameForZeroValue(t *testing.T) { + var conf elbv2.LoadBalancer + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + IDRefreshName: "aws_lb.lb_test", + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSLBDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSLBConfig_zeroValueName(), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSLBExists("aws_lb.lb_test", &conf), + resource.TestCheckResourceAttrSet("aws_lb.lb_test", "name"), + ), + }, + }, + }) +} + +func TestAccAWSLB_namePrefix(t *testing.T) { + var conf elbv2.LoadBalancer + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + IDRefreshName: "aws_lb.lb_test", + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSLBDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSLBConfig_namePrefix(), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckAWSLBExists("aws_lb.lb_test", &conf), + resource.TestCheckResourceAttrSet("aws_lb.lb_test", "name"), + resource.TestMatchResourceAttr("aws_lb.lb_test", "name", regexp.MustCompile("^tf-lb-")), ), }, @@ -140,117 +205,117 @@ func TestAccAWSALB_namePrefix(t *testing.T) { }) } -func TestAccAWSALB_tags(t *testing.T) { +func TestAccAWSLB_tags(t *testing.T) { var conf elbv2.LoadBalancer - albName := fmt.Sprintf("testaccawsalb-basic-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) + lbName := fmt.Sprintf("testaccawslb-basic-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, - IDRefreshName: "aws_alb.alb_test", + IDRefreshName: "aws_lb.lb_test", Providers: testAccProviders, - CheckDestroy: testAccCheckAWSALBDestroy, + CheckDestroy: testAccCheckAWSLBDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSALBConfig_basic(albName), + Config: testAccAWSLBConfig_basic(lbName), Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBExists("aws_alb.alb_test", &conf), - resource.TestCheckResourceAttr("aws_alb.alb_test", "tags.%", "1"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "tags.TestName", "TestAccAWSALB_basic"), + testAccCheckAWSLBExists("aws_lb.lb_test", &conf), + resource.TestCheckResourceAttr("aws_lb.lb_test", "tags.%", "1"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "tags.TestName", "TestAccAWSALB_basic"), ), }, { - Config: testAccAWSALBConfig_updatedTags(albName), + Config: testAccAWSLBConfig_updatedTags(lbName), Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBExists("aws_alb.alb_test", &conf), - resource.TestCheckResourceAttr("aws_alb.alb_test", "tags.%", "2"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "tags.Type", "Sample Type Tag"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "tags.Environment", "Production"), + testAccCheckAWSLBExists("aws_lb.lb_test", &conf), + resource.TestCheckResourceAttr("aws_lb.lb_test", "tags.%", "2"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "tags.Type", "Sample Type Tag"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "tags.Environment", "Production"), ), }, }, }) } -func TestAccAWSALB_updatedSecurityGroups(t *testing.T) { +func TestAccAWSLB_updatedSecurityGroups(t *testing.T) { var pre, post elbv2.LoadBalancer - albName := fmt.Sprintf("testaccawsalb-basic-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) + lbName := fmt.Sprintf("testaccawslb-basic-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, - IDRefreshName: "aws_alb.alb_test", + IDRefreshName: "aws_lb.lb_test", Providers: testAccProviders, - CheckDestroy: testAccCheckAWSALBDestroy, + CheckDestroy: testAccCheckAWSLBDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSALBConfig_basic(albName), + Config: testAccAWSLBConfig_basic(lbName), Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBExists("aws_alb.alb_test", &pre), - resource.TestCheckResourceAttr("aws_alb.alb_test", "security_groups.#", "1"), + testAccCheckAWSLBExists("aws_lb.lb_test", &pre), + resource.TestCheckResourceAttr("aws_lb.lb_test", "security_groups.#", "1"), ), }, { - Config: testAccAWSALBConfig_updateSecurityGroups(albName), + Config: testAccAWSLBConfig_updateSecurityGroups(lbName), Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBExists("aws_alb.alb_test", &post), - resource.TestCheckResourceAttr("aws_alb.alb_test", "security_groups.#", "2"), - testAccCheckAWSAlbARNs(&pre, &post), + testAccCheckAWSLBExists("aws_lb.lb_test", &post), + resource.TestCheckResourceAttr("aws_lb.lb_test", "security_groups.#", "2"), + testAccCheckAWSlbARNs(&pre, &post), ), }, }, }) } -func TestAccAWSALB_updatedSubnets(t *testing.T) { +func TestAccAWSLB_updatedSubnets(t *testing.T) { var pre, post elbv2.LoadBalancer - albName := fmt.Sprintf("testaccawsalb-basic-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) + lbName := fmt.Sprintf("testaccawslb-basic-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, - IDRefreshName: "aws_alb.alb_test", + IDRefreshName: "aws_lb.lb_test", Providers: testAccProviders, - CheckDestroy: testAccCheckAWSALBDestroy, + CheckDestroy: testAccCheckAWSLBDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSALBConfig_basic(albName), + Config: testAccAWSLBConfig_basic(lbName), Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBExists("aws_alb.alb_test", &pre), - resource.TestCheckResourceAttr("aws_alb.alb_test", "subnets.#", "2"), + testAccCheckAWSLBExists("aws_lb.lb_test", &pre), + resource.TestCheckResourceAttr("aws_lb.lb_test", "subnets.#", "2"), ), }, { - Config: testAccAWSALBConfig_updateSubnets(albName), + Config: testAccAWSLBConfig_updateSubnets(lbName), Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBExists("aws_alb.alb_test", &post), - resource.TestCheckResourceAttr("aws_alb.alb_test", "subnets.#", "3"), - testAccCheckAWSAlbARNs(&pre, &post), + testAccCheckAWSLBExists("aws_lb.lb_test", &post), + resource.TestCheckResourceAttr("aws_lb.lb_test", "subnets.#", "3"), + testAccCheckAWSlbARNs(&pre, &post), ), }, }, }) } -func TestAccAWSALB_updatedIpAddressType(t *testing.T) { +func TestAccAWSLB_updatedIpAddressType(t *testing.T) { var pre, post elbv2.LoadBalancer - albName := fmt.Sprintf("testaccawsalb-basic-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) + lbName := fmt.Sprintf("testaccawslb-basic-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, - IDRefreshName: "aws_alb.alb_test", + IDRefreshName: "aws_lb.lb_test", Providers: testAccProviders, - CheckDestroy: testAccCheckAWSALBDestroy, + CheckDestroy: testAccCheckAWSLBDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSALBConfigWithIpAddressType(albName), + Config: testAccAWSLBConfigWithIpAddressType(lbName), Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBExists("aws_alb.alb_test", &pre), - resource.TestCheckResourceAttr("aws_alb.alb_test", "ip_address_type", "ipv4"), + testAccCheckAWSLBExists("aws_lb.lb_test", &pre), + resource.TestCheckResourceAttr("aws_lb.lb_test", "ip_address_type", "ipv4"), ), }, { - Config: testAccAWSALBConfigWithIpAddressTypeUpdated(albName), + Config: testAccAWSLBConfigWithIpAddressTypeUpdated(lbName), Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBExists("aws_alb.alb_test", &post), - resource.TestCheckResourceAttr("aws_alb.alb_test", "ip_address_type", "dualstack"), + testAccCheckAWSLBExists("aws_lb.lb_test", &post), + resource.TestCheckResourceAttr("aws_lb.lb_test", "ip_address_type", "dualstack"), ), }, }, @@ -260,123 +325,123 @@ func TestAccAWSALB_updatedIpAddressType(t *testing.T) { // TestAccAWSALB_noSecurityGroup regression tests the issue in #8264, // where if an ALB is created without a security group, a default one // is assigned. -func TestAccAWSALB_noSecurityGroup(t *testing.T) { +func TestAccAWSLB_noSecurityGroup(t *testing.T) { var conf elbv2.LoadBalancer - albName := fmt.Sprintf("testaccawsalb-nosg-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) + lbName := fmt.Sprintf("testaccawslb-nosg-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, - IDRefreshName: "aws_alb.alb_test", + IDRefreshName: "aws_lb.lb_test", Providers: testAccProviders, - CheckDestroy: testAccCheckAWSALBDestroy, + CheckDestroy: testAccCheckAWSLBDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSALBConfig_nosg(albName), + Config: testAccAWSLBConfig_nosg(lbName), Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBExists("aws_alb.alb_test", &conf), - resource.TestCheckResourceAttr("aws_alb.alb_test", "name", albName), - resource.TestCheckResourceAttr("aws_alb.alb_test", "internal", "true"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "subnets.#", "2"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "security_groups.#", "1"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "tags.%", "1"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "tags.TestName", "TestAccAWSALB_basic"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "enable_deletion_protection", "false"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "idle_timeout", "30"), - resource.TestCheckResourceAttrSet("aws_alb.alb_test", "vpc_id"), - resource.TestCheckResourceAttrSet("aws_alb.alb_test", "zone_id"), - resource.TestCheckResourceAttrSet("aws_alb.alb_test", "dns_name"), + testAccCheckAWSLBExists("aws_lb.lb_test", &conf), + resource.TestCheckResourceAttr("aws_lb.lb_test", "name", lbName), + resource.TestCheckResourceAttr("aws_lb.lb_test", "internal", "true"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "subnets.#", "2"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "security_groups.#", "1"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "tags.%", "1"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "tags.TestName", "TestAccAWSALB_basic"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "enable_deletion_protection", "false"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "idle_timeout", "30"), + resource.TestCheckResourceAttrSet("aws_lb.lb_test", "vpc_id"), + resource.TestCheckResourceAttrSet("aws_lb.lb_test", "zone_id"), + resource.TestCheckResourceAttrSet("aws_lb.lb_test", "dns_name"), ), }, }, }) } -func TestAccAWSALB_accesslogs(t *testing.T) { +func TestAccAWSLB_accesslogs(t *testing.T) { var conf elbv2.LoadBalancer - bucketName := fmt.Sprintf("testaccawsalbaccesslogs-%s", acctest.RandStringFromCharSet(6, acctest.CharSetAlphaNum)) - albName := fmt.Sprintf("testaccawsalbaccesslog-%s", acctest.RandStringFromCharSet(4, acctest.CharSetAlpha)) + bucketName := fmt.Sprintf("testaccawslbaccesslogs-%s", acctest.RandStringFromCharSet(6, acctest.CharSetAlphaNum)) + lbName := fmt.Sprintf("testaccawslbaccesslog-%s", acctest.RandStringFromCharSet(4, acctest.CharSetAlpha)) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, - IDRefreshName: "aws_alb.alb_test", + IDRefreshName: "aws_lb.lb_test", Providers: testAccProviders, - CheckDestroy: testAccCheckAWSALBDestroy, + CheckDestroy: testAccCheckAWSLBDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSALBConfig_basic(albName), + Config: testAccAWSLBConfig_basic(lbName), Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBExists("aws_alb.alb_test", &conf), - resource.TestCheckResourceAttr("aws_alb.alb_test", "name", albName), - resource.TestCheckResourceAttr("aws_alb.alb_test", "internal", "true"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "subnets.#", "2"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "security_groups.#", "1"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "tags.%", "1"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "tags.TestName", "TestAccAWSALB_basic"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "enable_deletion_protection", "false"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "idle_timeout", "30"), - resource.TestCheckResourceAttrSet("aws_alb.alb_test", "vpc_id"), - resource.TestCheckResourceAttrSet("aws_alb.alb_test", "zone_id"), - resource.TestCheckResourceAttrSet("aws_alb.alb_test", "dns_name"), - resource.TestCheckResourceAttrSet("aws_alb.alb_test", "arn"), + testAccCheckAWSLBExists("aws_lb.lb_test", &conf), + resource.TestCheckResourceAttr("aws_lb.lb_test", "name", lbName), + resource.TestCheckResourceAttr("aws_lb.lb_test", "internal", "true"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "subnets.#", "2"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "security_groups.#", "1"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "tags.%", "1"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "tags.TestName", "TestAccAWSALB_basic"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "enable_deletion_protection", "false"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "idle_timeout", "30"), + resource.TestCheckResourceAttrSet("aws_lb.lb_test", "vpc_id"), + resource.TestCheckResourceAttrSet("aws_lb.lb_test", "zone_id"), + resource.TestCheckResourceAttrSet("aws_lb.lb_test", "dns_name"), + resource.TestCheckResourceAttrSet("aws_lb.lb_test", "arn"), ), }, { - Config: testAccAWSALBConfig_accessLogs(true, albName, bucketName), + Config: testAccAWSLBConfig_accessLogs(true, lbName, bucketName), Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBExists("aws_alb.alb_test", &conf), - resource.TestCheckResourceAttr("aws_alb.alb_test", "name", albName), - resource.TestCheckResourceAttr("aws_alb.alb_test", "internal", "true"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "subnets.#", "2"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "security_groups.#", "1"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "tags.%", "1"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "tags.TestName", "TestAccAWSALB_basic1"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "enable_deletion_protection", "false"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "idle_timeout", "50"), - resource.TestCheckResourceAttrSet("aws_alb.alb_test", "vpc_id"), - resource.TestCheckResourceAttrSet("aws_alb.alb_test", "zone_id"), - resource.TestCheckResourceAttrSet("aws_alb.alb_test", "dns_name"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "access_logs.#", "1"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "access_logs.0.bucket", bucketName), - resource.TestCheckResourceAttr("aws_alb.alb_test", "access_logs.0.prefix", "testAccAWSALBConfig_accessLogs"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "access_logs.0.enabled", "true"), - resource.TestCheckResourceAttrSet("aws_alb.alb_test", "arn"), + testAccCheckAWSLBExists("aws_lb.lb_test", &conf), + resource.TestCheckResourceAttr("aws_lb.lb_test", "name", lbName), + resource.TestCheckResourceAttr("aws_lb.lb_test", "internal", "true"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "subnets.#", "2"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "security_groups.#", "1"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "tags.%", "1"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "tags.TestName", "TestAccAWSALB_basic1"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "enable_deletion_protection", "false"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "idle_timeout", "50"), + resource.TestCheckResourceAttrSet("aws_lb.lb_test", "vpc_id"), + resource.TestCheckResourceAttrSet("aws_lb.lb_test", "zone_id"), + resource.TestCheckResourceAttrSet("aws_lb.lb_test", "dns_name"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "access_logs.#", "1"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "access_logs.0.bucket", bucketName), + resource.TestCheckResourceAttr("aws_lb.lb_test", "access_logs.0.prefix", "testAccAWSALBConfig_accessLogs"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "access_logs.0.enabled", "true"), + resource.TestCheckResourceAttrSet("aws_lb.lb_test", "arn"), ), }, { - Config: testAccAWSALBConfig_accessLogs(false, albName, bucketName), + Config: testAccAWSLBConfig_accessLogs(false, lbName, bucketName), Check: resource.ComposeAggregateTestCheckFunc( - testAccCheckAWSALBExists("aws_alb.alb_test", &conf), - resource.TestCheckResourceAttr("aws_alb.alb_test", "name", albName), - resource.TestCheckResourceAttr("aws_alb.alb_test", "internal", "true"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "subnets.#", "2"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "security_groups.#", "1"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "tags.%", "1"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "tags.TestName", "TestAccAWSALB_basic1"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "enable_deletion_protection", "false"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "idle_timeout", "50"), - resource.TestCheckResourceAttrSet("aws_alb.alb_test", "vpc_id"), - resource.TestCheckResourceAttrSet("aws_alb.alb_test", "zone_id"), - resource.TestCheckResourceAttrSet("aws_alb.alb_test", "dns_name"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "access_logs.#", "1"), - resource.TestCheckResourceAttr("aws_alb.alb_test", "access_logs.0.enabled", "false"), - resource.TestCheckResourceAttrSet("aws_alb.alb_test", "arn"), + testAccCheckAWSLBExists("aws_lb.lb_test", &conf), + resource.TestCheckResourceAttr("aws_lb.lb_test", "name", lbName), + resource.TestCheckResourceAttr("aws_lb.lb_test", "internal", "true"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "subnets.#", "2"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "security_groups.#", "1"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "tags.%", "1"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "tags.TestName", "TestAccAWSALB_basic1"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "enable_deletion_protection", "false"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "idle_timeout", "50"), + resource.TestCheckResourceAttrSet("aws_lb.lb_test", "vpc_id"), + resource.TestCheckResourceAttrSet("aws_lb.lb_test", "zone_id"), + resource.TestCheckResourceAttrSet("aws_lb.lb_test", "dns_name"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "access_logs.#", "1"), + resource.TestCheckResourceAttr("aws_lb.lb_test", "access_logs.0.enabled", "false"), + resource.TestCheckResourceAttrSet("aws_lb.lb_test", "arn"), ), }, }, }) } -func testAccCheckAWSAlbARNs(pre, post *elbv2.LoadBalancer) resource.TestCheckFunc { +func testAccCheckAWSlbARNs(pre, post *elbv2.LoadBalancer) resource.TestCheckFunc { return func(s *terraform.State) error { if *pre.LoadBalancerArn != *post.LoadBalancerArn { - return errors.New("ALB has been recreated. ARNs are different") + return errors.New("LB has been recreated. ARNs are different") } return nil } } -func testAccCheckAWSALBExists(n string, res *elbv2.LoadBalancer) resource.TestCheckFunc { +func testAccCheckAWSLBExists(n string, res *elbv2.LoadBalancer) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { @@ -384,7 +449,7 @@ func testAccCheckAWSALBExists(n string, res *elbv2.LoadBalancer) resource.TestCh } if rs.Primary.ID == "" { - return errors.New("No ALB ID is set") + return errors.New("No LB ID is set") } conn := testAccProvider.Meta().(*AWSClient).elbv2conn @@ -399,7 +464,7 @@ func testAccCheckAWSALBExists(n string, res *elbv2.LoadBalancer) resource.TestCh if len(describe.LoadBalancers) != 1 || *describe.LoadBalancers[0].LoadBalancerArn != rs.Primary.ID { - return errors.New("ALB not found") + return errors.New("LB not found") } *res = *describe.LoadBalancers[0] @@ -407,11 +472,11 @@ func testAccCheckAWSALBExists(n string, res *elbv2.LoadBalancer) resource.TestCh } } -func testAccCheckAWSALBDestroy(s *terraform.State) error { +func testAccCheckAWSLBDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).elbv2conn for _, rs := range s.RootModule().Resources { - if rs.Type != "aws_alb" { + if rs.Type != "aws_lb" && rs.Type != "aws_alb" { continue } @@ -422,7 +487,7 @@ func testAccCheckAWSALBDestroy(s *terraform.State) error { if err == nil { if len(describe.LoadBalancers) != 0 && *describe.LoadBalancers[0].LoadBalancerArn == rs.Primary.ID { - return fmt.Errorf("ALB %q still exists", rs.Primary.ID) + return fmt.Errorf("LB %q still exists", rs.Primary.ID) } } @@ -430,15 +495,15 @@ func testAccCheckAWSALBDestroy(s *terraform.State) error { if isLoadBalancerNotFound(err) { return nil } else { - return errwrap.Wrapf("Unexpected error checking ALB destroyed: {{err}}", err) + return errwrap.Wrapf("Unexpected error checking LB destroyed: {{err}}", err) } } return nil } -func testAccAWSALBConfigWithIpAddressTypeUpdated(albName string) string { - return fmt.Sprintf(`resource "aws_alb" "alb_test" { +func testAccAWSLBConfigWithIpAddressTypeUpdated(lbName string) string { + return fmt.Sprintf(`resource "aws_lb" "lb_test" { name = "%s" security_groups = ["${aws_security_group.alb_test.id}"] subnets = ["${aws_subnet.alb_test_1.id}", "${aws_subnet.alb_test_2.id}"] @@ -453,18 +518,18 @@ func testAccAWSALBConfigWithIpAddressTypeUpdated(albName string) string { } } -resource "aws_alb_listener" "test" { - load_balancer_arn = "${aws_alb.alb_test.id}" +resource "aws_lb_listener" "test" { + load_balancer_arn = "${aws_lb.lb_test.id}" protocol = "HTTP" port = "80" default_action { - target_group_arn = "${aws_alb_target_group.test.id}" + target_group_arn = "${aws_lb_target_group.test.id}" type = "forward" } } -resource "aws_alb_target_group" "test" { +resource "aws_lb_target_group" "test" { name = "%s" port = 80 protocol = "HTTP" @@ -545,11 +610,11 @@ resource "aws_security_group" "alb_test" { tags { TestName = "TestAccAWSALB_basic" } -}`, albName, albName) +}`, lbName, lbName) } -func testAccAWSALBConfigWithIpAddressType(albName string) string { - return fmt.Sprintf(`resource "aws_alb" "alb_test" { +func testAccAWSLBConfigWithIpAddressType(lbName string) string { + return fmt.Sprintf(`resource "aws_lb" "lb_test" { name = "%s" security_groups = ["${aws_security_group.alb_test.id}"] subnets = ["${aws_subnet.alb_test_1.id}", "${aws_subnet.alb_test_2.id}"] @@ -564,18 +629,18 @@ func testAccAWSALBConfigWithIpAddressType(albName string) string { } } -resource "aws_alb_listener" "test" { - load_balancer_arn = "${aws_alb.alb_test.id}" +resource "aws_lb_listener" "test" { + load_balancer_arn = "${aws_lb.lb_test.id}" protocol = "HTTP" port = "80" default_action { - target_group_arn = "${aws_alb_target_group.test.id}" + target_group_arn = "${aws_lb_target_group.test.id}" type = "forward" } } -resource "aws_alb_target_group" "test" { +resource "aws_lb_target_group" "test" { name = "%s" port = 80 protocol = "HTTP" @@ -656,11 +721,117 @@ resource "aws_security_group" "alb_test" { tags { TestName = "TestAccAWSALB_basic" } -}`, albName, albName) +}`, lbName, lbName) +} + +func testAccAWSLBConfig_basic(lbName string) string { + return fmt.Sprintf(`resource "aws_lb" "lb_test" { + name = "%s" + internal = true + security_groups = ["${aws_security_group.alb_test.id}"] + subnets = ["${aws_subnet.alb_test.*.id}"] + + idle_timeout = 30 + enable_deletion_protection = false + + tags { + TestName = "TestAccAWSALB_basic" + } +} + +variable "subnets" { + default = ["10.0.1.0/24", "10.0.2.0/24"] + type = "list" +} + +data "aws_availability_zones" "available" {} + +resource "aws_vpc" "alb_test" { + cidr_block = "10.0.0.0/16" + + tags { + TestName = "TestAccAWSALB_basic" + } +} + +resource "aws_subnet" "alb_test" { + count = 2 + vpc_id = "${aws_vpc.alb_test.id}" + cidr_block = "${element(var.subnets, count.index)}" + map_public_ip_on_launch = true + availability_zone = "${element(data.aws_availability_zones.available.names, count.index)}" + + tags { + TestName = "TestAccAWSALB_basic" + } +} + +resource "aws_security_group" "alb_test" { + name = "allow_all_alb_test" + description = "Used for ALB Testing" + vpc_id = "${aws_vpc.alb_test.id}" + + ingress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } + + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } + + tags { + TestName = "TestAccAWSALB_basic" + } +}`, lbName) +} + +func testAccAWSLBConfig_networkLoadbalancer(lbName string) string { + return fmt.Sprintf(`resource "aws_lb" "lb_test" { + name = "%s" + internal = true + load_balancer_type = "network" + + enable_deletion_protection = false + + subnet_mapping { + subnet_id = "${aws_subnet.alb_test.id}" + } + + tags { + TestName = "TestAccAWSALB_basic" + } +} + +resource "aws_vpc" "alb_test" { + cidr_block = "10.10.0.0/16" + + tags { + TestName = "TestAccAWSALB_basic" + } +} + +resource "aws_subnet" "alb_test" { + vpc_id = "${aws_vpc.alb_test.id}" + cidr_block = "10.10.0.0/21" + map_public_ip_on_launch = true + availability_zone = "us-west-2a" + + tags { + TestName = "TestAccAWSALB_basic" + } +} + +`, lbName) } -func testAccAWSALBConfig_basic(albName string) string { - return fmt.Sprintf(`resource "aws_alb" "alb_test" { +func testAccAWSLBConfigBackwardsCompatibility(lbName string) string { + return fmt.Sprintf(`resource "aws_alb" "lb_test" { name = "%s" internal = true security_groups = ["${aws_security_group.alb_test.id}"] @@ -723,11 +894,11 @@ resource "aws_security_group" "alb_test" { tags { TestName = "TestAccAWSALB_basic" } -}`, albName) +}`, lbName) } -func testAccAWSALBConfig_updateSubnets(albName string) string { - return fmt.Sprintf(`resource "aws_alb" "alb_test" { +func testAccAWSLBConfig_updateSubnets(lbName string) string { + return fmt.Sprintf(`resource "aws_lb" "lb_test" { name = "%s" internal = true security_groups = ["${aws_security_group.alb_test.id}"] @@ -790,12 +961,12 @@ resource "aws_security_group" "alb_test" { tags { TestName = "TestAccAWSALB_basic" } -}`, albName) +}`, lbName) } -func testAccAWSALBConfig_generatedName() string { +func testAccAWSLBConfig_generatedName() string { return fmt.Sprintf(` -resource "aws_alb" "alb_test" { +resource "aws_lb" "lb_test" { internal = true security_groups = ["${aws_security_group.alb_test.id}"] subnets = ["${aws_subnet.alb_test.*.id}"] @@ -868,9 +1039,9 @@ resource "aws_security_group" "alb_test" { }`) } -func testAccAWSALBConfig_zeroValueName() string { +func testAccAWSLBConfig_zeroValueName() string { return fmt.Sprintf(` -resource "aws_alb" "alb_test" { +resource "aws_lb" "lb_test" { name = "" internal = true security_groups = ["${aws_security_group.alb_test.id}"] @@ -944,9 +1115,9 @@ resource "aws_security_group" "alb_test" { }`) } -func testAccAWSALBConfig_namePrefix() string { +func testAccAWSLBConfig_namePrefix() string { return fmt.Sprintf(` -resource "aws_alb" "alb_test" { +resource "aws_lb" "lb_test" { name_prefix = "tf-lb-" internal = true security_groups = ["${aws_security_group.alb_test.id}"] @@ -1011,8 +1182,8 @@ resource "aws_security_group" "alb_test" { } }`) } -func testAccAWSALBConfig_updatedTags(albName string) string { - return fmt.Sprintf(`resource "aws_alb" "alb_test" { +func testAccAWSLBConfig_updatedTags(lbName string) string { + return fmt.Sprintf(`resource "aws_lb" "lb_test" { name = "%s" internal = true security_groups = ["${aws_security_group.alb_test.id}"] @@ -1076,11 +1247,11 @@ resource "aws_security_group" "alb_test" { tags { TestName = "TestAccAWSALB_basic" } -}`, albName) +}`, lbName) } -func testAccAWSALBConfig_accessLogs(enabled bool, albName, bucketName string) string { - return fmt.Sprintf(`resource "aws_alb" "alb_test" { +func testAccAWSLBConfig_accessLogs(enabled bool, lbName, bucketName string) string { + return fmt.Sprintf(`resource "aws_lb" "lb_test" { name = "%s" internal = true security_groups = ["${aws_security_group.alb_test.id}"] @@ -1187,11 +1358,11 @@ resource "aws_security_group" "alb_test" { tags { TestName = "TestAccAWSALB_basic" } -}`, albName, enabled, bucketName) +}`, lbName, enabled, bucketName) } -func testAccAWSALBConfig_nosg(albName string) string { - return fmt.Sprintf(`resource "aws_alb" "alb_test" { +func testAccAWSLBConfig_nosg(lbName string) string { + return fmt.Sprintf(`resource "aws_lb" "lb_test" { name = "%s" internal = true subnets = ["${aws_subnet.alb_test.*.id}"] @@ -1229,11 +1400,11 @@ resource "aws_subnet" "alb_test" { tags { TestName = "TestAccAWSALB_basic" } -}`, albName) +}`, lbName) } -func testAccAWSALBConfig_updateSecurityGroups(albName string) string { - return fmt.Sprintf(`resource "aws_alb" "alb_test" { +func testAccAWSLBConfig_updateSecurityGroups(lbName string) string { + return fmt.Sprintf(`resource "aws_lb" "lb_test" { name = "%s" internal = true security_groups = ["${aws_security_group.alb_test.id}", "${aws_security_group.alb_test_2.id}"] @@ -1313,5 +1484,5 @@ resource "aws_security_group" "alb_test" { tags { TestName = "TestAccAWSALB_basic" } -}`, albName) +}`, lbName) } diff --git a/aws/validators.go b/aws/validators.go index b173cd1b9ab..72ded25b3a4 100644 --- a/aws/validators.go +++ b/aws/validators.go @@ -1321,7 +1321,7 @@ func validateDbOptionGroupNamePrefix(v interface{}, k string) (ws []string, erro return } -func validateAwsAlbTargetGroupName(v interface{}, k string) (ws []string, errors []error) { +func validateAwsLbTargetGroupName(v interface{}, k string) (ws []string, errors []error) { name := v.(string) if len(name) > 32 { errors = append(errors, fmt.Errorf("%q (%q) cannot be longer than '32' characters", k, name)) @@ -1329,7 +1329,7 @@ func validateAwsAlbTargetGroupName(v interface{}, k string) (ws []string, errors return } -func validateAwsAlbTargetGroupNamePrefix(v interface{}, k string) (ws []string, errors []error) { +func validateAwsLbTargetGroupNamePrefix(v interface{}, k string) (ws []string, errors []error) { name := v.(string) if len(name) > 32 { errors = append(errors, fmt.Errorf("%q (%q) cannot be longer than '6' characters", k, name)) diff --git a/website/aws.erb b/website/aws.erb index 6ce5ffea3c3..bb1c556b4b2 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -18,13 +18,13 @@ aws_acm_certificate > - aws_alb + aws_alb > - aws_alb_listener + aws_alb_listener > - aws_alb_target_group + aws_alb_target_group > aws_ami @@ -140,6 +140,15 @@ > aws_kms_secret + > + aws_lb + + > + aws_lb_listener + + > + aws_lb_target_group + > aws_partition @@ -509,29 +518,28 @@ - - > + > EC2 Resources + > + Load Balancing Resources + + + > ECS Resources diff --git a/website/docs/d/alb_listener.html.markdown b/website/docs/d/alb_listener.html.markdown deleted file mode 100644 index 8237a847c7f..00000000000 --- a/website/docs/d/alb_listener.html.markdown +++ /dev/null @@ -1,38 +0,0 @@ ---- -layout: "aws" -page_title: "AWS: aws_alb_listener" -sidebar_current: "docs-aws-datasource-alb-listener" -description: |- - Provides an Application Load Balancer Listener data source. ---- - -# aws\_alb\_listener - -Provides information about an Application Load Balancer Listener. - -This data source can prove useful when a module accepts an ALB Listener as an -input variable and needs to know the ALB it is attached to, or other -information specific to the listener in question. - -## Example Usage - -```hcl -variable "listener_arn" { - type = "string" -} - -data "aws_alb_listener" "listener" { - arn = "${var.listener_arn}" -} -``` - -## Argument Reference - -The following arguments are supported: - -* `arn` - (Required) The ARN of the listener. - -## Attributes Reference - -See the [ALB Listener Resource](/docs/providers/aws/r/alb_listener.html) for details -on the returned attributes - they are identical. diff --git a/website/docs/d/alb_target_group.html.markdown b/website/docs/d/alb_target_group.html.markdown deleted file mode 100644 index 88405c8fb2b..00000000000 --- a/website/docs/d/alb_target_group.html.markdown +++ /dev/null @@ -1,48 +0,0 @@ ---- -layout: "aws" -page_title: "AWS: aws_alb_target_group" -sidebar_current: "docs-aws-datasource-alb-target-group" -description: |- - Provides an Application Load Balancer Target Group data source. ---- - -# aws\_alb\_target\_group - -Provides information about an Application Load Balancer Target Group. - -This data source can prove useful when a module accepts an ALB Target Group as an -input variable and needs to know its attributes. It can also be used to get the ARN of -an ALB Target Group for use in other resources, given ALB Target Group name. - -## Example Usage - -```hcl -variable "alb_tg_arn" { - type = "string" - default = "" -} - -variable "alb_tg_name" { - type = "string" - default = "" -} - -data "aws_alb_target_group" "test" { - arn = "${var.alb_tg_arn}" - name = "${var.alb_tg_name}" -} -``` - -## Argument Reference - -The following arguments are supported: - -* `arn` - (Optional) The full ARN of the target group. -* `name` - (Optional) The unique name of the target group. - -~> **NOTE**: When both `arn` and `name` are specified, `arn` takes precedence. - -## Attributes Reference - -See the [ALB Target Group Resource](/docs/providers/aws/r/alb_target_group.html) for details -on the returned attributes - they are identical. diff --git a/website/docs/d/alb.html.markdown b/website/docs/d/lb.html.markdown similarity index 53% rename from website/docs/d/alb.html.markdown rename to website/docs/d/lb.html.markdown index 00ef6a4e3e2..d50991d97d9 100644 --- a/website/docs/d/alb.html.markdown +++ b/website/docs/d/lb.html.markdown @@ -1,35 +1,37 @@ --- layout: "aws" -page_title: "AWS: aws_alb" -sidebar_current: "docs-aws-datasource-alb-x" +page_title: "AWS: aws_lb" +sidebar_current: "docs-aws-datasource-lb-x" description: |- - Provides an Application Load Balancer data source. + Provides a Load Balancer data source. --- -# aws\_alb +# aws_lb -Provides information about an Application Load Balancer. +~> **Note:** `aws_alb` is known as `aws_lb`. The functionality is identical. -This data source can prove useful when a module accepts an ALB as an input +Provides information about a Load Balancer. + +This data source can prove useful when a module accepts an LB as an input variable and needs to, for example, determine the security groups associated with it, etc. ## Example Usage ```hcl -variable "alb_arn" { +variable "lb_arn" { type = "string" default = "" } -variable "alb_name" { +variable "lb_name" { type = "string" default = "" } -data "aws_alb" "test" { - arn = "${var.alb_arn}" - name = "${var.alb_name}" +data "aws_lb" "test" { + arn = "${var.lb_arn}" + name = "${var.lb_name}" } ``` @@ -44,5 +46,5 @@ The following arguments are supported: ## Attributes Reference -See the [ALB Resource](/docs/providers/aws/r/alb.html) for details on the +See the [LB Resource](/docs/providers/aws/r/lb.html) for details on the returned attributes - they are identical. diff --git a/website/docs/d/lb_listener.html.markdown b/website/docs/d/lb_listener.html.markdown new file mode 100644 index 00000000000..b5ab57051c5 --- /dev/null +++ b/website/docs/d/lb_listener.html.markdown @@ -0,0 +1,40 @@ +--- +layout: "aws" +page_title: "AWS: aws_lb_listener" +sidebar_current: "docs-aws-datasource-lb-listener" +description: |- + Provides a Load Balancer Listener data source. +--- + +# aws_lb_listener + +~> **Note:** `aws_alb_listener` is known as `aws_lb_listener`. The functionality is identical. + +Provides information about a Load Balancer Listener. + +This data source can prove useful when a module accepts an LB Listener as an +input variable and needs to know the LB it is attached to, or other +information specific to the listener in question. + +## Example Usage + +```hcl +variable "listener_arn" { + type = "string" +} + +data "aws_lb_listener" "listener" { + arn = "${var.listener_arn}" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `arn` - (Required) The ARN of the listener. + +## Attributes Reference + +See the [LB Listener Resource](/docs/providers/aws/r/lb_listener.html) for details +on the returned attributes - they are identical. diff --git a/website/docs/d/lb_target_group.html.markdown b/website/docs/d/lb_target_group.html.markdown new file mode 100644 index 00000000000..c78a0e3e577 --- /dev/null +++ b/website/docs/d/lb_target_group.html.markdown @@ -0,0 +1,50 @@ +--- +layout: "aws" +page_title: "AWS: aws_lb_target_group" +sidebar_current: "docs-aws-datasource-lb-target-group" +description: |- + Provides a Load Balancer Target Group data source. +--- + +# aws_lb_target_group + +~> **Note:** `aws_alb_target_group` is known as `aws_lb_target_group`. The functionality is identical. + +Provides information about a Load Balancer Target Group. + +This data source can prove useful when a module accepts an LB Target Group as an +input variable and needs to know its attributes. It can also be used to get the ARN of +an LB Target Group for use in other resources, given LB Target Group name. + +## Example Usage + +```hcl +variable "lb_tg_arn" { + type = "string" + default = "" +} + +variable "lb_tg_name" { + type = "string" + default = "" +} + +data "aws_lb_target_group" "test" { + arn = "${var.lb_tg_arn}" + name = "${var.lb_tg_name}" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `arn` - (Optional) The full ARN of the target group. +* `name` - (Optional) The unique name of the target group. + +~> **NOTE**: When both `arn` and `name` are specified, `arn` takes precedence. + +## Attributes Reference + +See the [LB Target Group Resource](/docs/providers/aws/r/lb_target_group.html) for details +on the returned attributes - they are identical. diff --git a/website/docs/r/alb.html.markdown b/website/docs/r/lb.html.markdown similarity index 53% rename from website/docs/r/alb.html.markdown rename to website/docs/r/lb.html.markdown index 74f03aad932..c155a15e099 100644 --- a/website/docs/r/alb.html.markdown +++ b/website/docs/r/lb.html.markdown @@ -1,34 +1,32 @@ --- layout: "aws" -page_title: "AWS: aws_alb" -sidebar_current: "docs-aws-resource-alb" +page_title: "AWS: aws_lb" +sidebar_current: "docs-aws-resource-elbv2" description: |- - Provides an Application Load Balancer resource. + Provides a Load Balancer resource. --- -# aws\_alb +# aws_lb -Provides an Application Load Balancer resource. +Provides a Load Balancer resource. -The official AWS CLI calls this "elbv2" while their documentation calls it -an Application Load Balancer. Terraform uses "ALB" but they mean the same -thing. +~> **Note:** `aws_alb` is know as `aws_lb`. The functionality is identical. ## Example Usage ```hcl -# Create a new load balancer -resource "aws_alb" "test" { - name = "test-alb-tf" +# Create a new application load balancer +resource "aws_lb" "test" { + name = "test-lb-tf" internal = false - security_groups = ["${aws_security_group.alb_sg.id}"] + security_groups = ["${aws_security_group.lb_sg.id}"] subnets = ["${aws_subnet.public.*.id}"] enable_deletion_protection = true access_logs { - bucket = "${aws_s3_bucket.alb_logs.bucket}" - prefix = "test-alb" + bucket = "${aws_s3_bucket.lb_logs.bucket}" + prefix = "test-lb" } tags { @@ -37,31 +35,43 @@ resource "aws_alb" "test" { } ``` +```hcl +# Create a new network load balancer + +``` + ## Argument Reference The following arguments are supported: -* `name` - (Optional) The name of the ALB. This name must be unique within your AWS account, can have a maximum of 32 characters, +* `name` - (Optional) The name of the LB. This name must be unique within your AWS account, can have a maximum of 32 characters, must contain only alphanumeric characters or hyphens, and must not begin or end with a hyphen. If not specified, Terraform will autogenerate a name beginning with `tf-lb`. * `name_prefix` - (Optional) Creates a unique name beginning with the specified prefix. Conflicts with `name`. -* `internal` - (Optional) If true, the ALB will be internal. -* `security_groups` - (Optional) A list of security group IDs to assign to the ALB. +* `internal` - (Optional) If true, the LB will be internal. +* `load_balancer_type` - (Optional) The type of load balancer to create. Possible values are `application` or `network`. The default value is `application`. +* `security_groups` - (Optional) A list of security group IDs to assign to the LB. * `access_logs` - (Optional) An Access Logs block. Access Logs documented below. -* `subnets` - (Required) A list of subnet IDs to attach to the ALB. +* `subnets` - (Optional) A list of subnet IDs to attach to the LB. +* `subnet_mapping` - (Optional) A subnet mapping block as documented below. * `idle_timeout` - (Optional) The time in seconds that the connection is allowed to be idle. Default: 60. * `enable_deletion_protection` - (Optional) If true, deletion of the load balancer will be disabled via the AWS API. This will prevent Terraform from deleting the load balancer. Defaults to `false`. * `ip_address_type` - (Optional) The type of IP addresses used by the subnets for your load balancer. The possible values are `ipv4` and `dualstack` * `tags` - (Optional) A mapping of tags to assign to the resource. -~> **NOTE::** Please note that internal ALBs can only use `ipv4` as the ip_address_type. You can only change to `dualstack` ip_address_type if the selected subnets are IPv6 enabled. +~> **NOTE::** Please note that internal LBs can only use `ipv4` as the ip_address_type. You can only change to `dualstack` ip_address_type if the selected subnets are IPv6 enabled. Access Logs (`access_logs`) support the following: * `bucket` - (Required) The S3 bucket name to store the logs in. * `prefix` - (Optional) The S3 bucket prefix. Logs are stored in the root if not configured. -* `enabled` - (Optional) Boolean to enable / disable `access_logs`. Default is `true` +* `enabled` - (Optional) Boolean to enable / disable `access_logs`. + +Subnet Mapping (`subnet_mapping`) blocks support the following: + +* `subnet_id` - (Required) The id of the subnet of which to attach to the load balancer. You can specify only one subnet per Availability Zone. +* `allocation_id` - (Optional) The allocation ID of the Elastic IP address. ## Attributes Reference @@ -76,17 +86,17 @@ The following attributes are exported in addition to the arguments listed above: ## Timeouts -`aws_alb` provides the following +`aws_lb` provides the following [Timeouts](/docs/configuration/resources.html#timeouts) configuration options: -- `create` - (Default `10 minutes`) Used for Creating ALB -- `update` - (Default `10 minutes`) Used for ALB modifications -- `delete` - (Default `10 minutes`) Used for destroying ALB +- `create` - (Default `10 minutes`) Used for Creating LB +- `update` - (Default `10 minutes`) Used for LB modifications +- `delete` - (Default `10 minutes`) Used for destroying LB ## Import -ALBs can be imported using their ARN, e.g. +LBs can be imported using their ARN, e.g. ``` -$ terraform import aws_alb.bar arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188 +$ terraform import aws_lb.bar arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188 ``` diff --git a/website/docs/r/alb_listener.html.markdown b/website/docs/r/lb_listener.html.markdown similarity index 70% rename from website/docs/r/alb_listener.html.markdown rename to website/docs/r/lb_listener.html.markdown index 53e75d7959a..f9398e05680 100644 --- a/website/docs/r/alb_listener.html.markdown +++ b/website/docs/r/lb_listener.html.markdown @@ -1,36 +1,38 @@ --- layout: "aws" -page_title: "AWS: aws_alb_listener" -sidebar_current: "docs-aws-resource-alb-listener" +page_title: "AWS: aws_lb_listener" +sidebar_current: "docs-aws-resource-elbv2-listener" description: |- - Provides an Application Load Balancer Listener resource. + Provides a Load Balancer Listener resource. --- -# aws\_alb\_listener +# aws_lb_listener -Provides an Application Load Balancer Listener resource. +Provides a Load Balancer Listener resource. + +~> **Note:** `aws_alb_listener` is known as `aws_lb_listener`. The functionality is identical. ## Example Usage ```hcl # Create a new load balancer -resource "aws_alb" "front_end" { +resource "aws_lb" "front_end" { # ... } -resource "aws_alb_target_group" "front_end" { +resource "aws_lb_target_group" "front_end" { # ... } -resource "aws_alb_listener" "front_end" { - load_balancer_arn = "${aws_alb.front_end.arn}" +resource "aws_lb_listener" "front_end" { + load_balancer_arn = "${aws_lb.front_end.arn}" port = "443" protocol = "HTTPS" ssl_policy = "ELBSecurityPolicy-2015-05" certificate_arn = "arn:aws:iam::187416307283:server-certificate/test_cert_rab3wuqwgja25ct3n4jdj2tzu4" default_action { - target_group_arn = "${aws_alb_target_group.front_end.arn}" + target_group_arn = "${aws_lb_target_group.front_end.arn}" type = "forward" } } @@ -64,5 +66,5 @@ The following attributes are exported in addition to the arguments listed above: Listeners can be imported using their ARN, e.g. ``` -$ terraform import aws_alb_listener.front_end arn:aws:elasticloadbalancing:us-west-2:187416307283:listener/app/front-end-alb/8e4497da625e2d8a/9ab28ade35828f96 +$ terraform import aws_lb_listener.front_end arn:aws:elasticloadbalancing:us-west-2:187416307283:listener/app/front-end-alb/8e4497da625e2d8a/9ab28ade35828f96 ``` diff --git a/website/docs/r/alb_listener_rule.html.markdown b/website/docs/r/lb_listener_rule.html.markdown similarity index 64% rename from website/docs/r/alb_listener_rule.html.markdown rename to website/docs/r/lb_listener_rule.html.markdown index 6bfe263c7aa..72a2b3c1cb7 100644 --- a/website/docs/r/alb_listener_rule.html.markdown +++ b/website/docs/r/lb_listener_rule.html.markdown @@ -1,34 +1,36 @@ --- layout: "aws" -page_title: "AWS: aws_alb_listener_rule" -sidebar_current: "docs-aws-resource-alb-listener-rule" +page_title: "AWS: aws_lb_listener_rule" +sidebar_current: "docs-aws-resource-elbv2-listener-rule" description: |- - Provides an Application Load Balancer Listener Rule resource. + Provides a Load Balancer Listener Rule resource. --- -# aws\_alb\_listener\_rule +# aws_lb_listener_rule -Provides an Application Load Balancer Listener Rule resource. +Provides a Load Balancer Listener Rule resource. + +~> **Note:** `aws_alb_listener_rule` is know as `aws_lb_listener_rule`. The functionality is identical. ## Example Usage ```hcl # Create a new load balancer -resource "aws_alb" "front_end" { +resource "aws_lb" "front_end" { # ... } -resource "aws_alb_listener" "front_end" { +resource "aws_lb_listener" "front_end" { # Other parameters } -resource "aws_alb_listener_rule" "static" { - listener_arn = "${aws_alb_listener.front_end.arn}" +resource "aws_lb_listener_rule" "static" { + listener_arn = "${aws_lb_listener.front_end.arn}" priority = 100 action { type = "forward" - target_group_arn = "${aws_alb_target_group.static.arn}" + target_group_arn = "${aws_lb_target_group.static.arn}" } condition { @@ -37,13 +39,13 @@ resource "aws_alb_listener_rule" "static" { } } -resource "aws_alb_listener_rule" "host_based_routing" { - listener_arn = "${aws_alb_listener.front_end.arn}" +resource "aws_lb_listener_rule" "host_based_routing" { + listener_arn = "${aws_lb_listener.front_end.arn}" priority = 99 action { type = "forward" - target_group_arn = "${aws_alb_target_group.static.arn}" + target_group_arn = "${aws_lb_target_group.static.arn}" } condition { @@ -85,5 +87,5 @@ The following attributes are exported in addition to the arguments listed above: Rules can be imported using their ARN, e.g. ``` -$ terraform import aws_alb_listener_rule.front_end arn:aws:elasticloadbalancing:us-west-2:187416307283:listener-rule/app/test/8e4497da625e2d8a/9ab28ade35828f96/67b3d2d36dd7c26b +$ terraform import aws_lb_listener_rule.front_end arn:aws:elasticloadbalancing:us-west-2:187416307283:listener-rule/app/test/8e4497da625e2d8a/9ab28ade35828f96/67b3d2d36dd7c26b ``` diff --git a/website/docs/r/alb_target_group.html.markdown b/website/docs/r/lb_target_group.html.markdown similarity index 85% rename from website/docs/r/alb_target_group.html.markdown rename to website/docs/r/lb_target_group.html.markdown index c554a06d502..15590ce58b0 100644 --- a/website/docs/r/alb_target_group.html.markdown +++ b/website/docs/r/lb_target_group.html.markdown @@ -1,22 +1,22 @@ --- layout: "aws" -page_title: "AWS: aws_alb_target_group" -sidebar_current: "docs-aws-resource-alb-target-group" +page_title: "AWS: aws_lb_target_group" +sidebar_current: "docs-aws-resource-elbv2-target-group" description: |- - Provides a Target Group resource for use with Application Load - Balancers. + Provides a Target Group resource for use with Load Balancers. --- -# aws\_alb\_target\_group +# aws_lb_target_group -Provides a Target Group resource for use with Application Load Balancer -resources. +Provides a Target Group resource for use with Load Balancer resources. + +~> **Note:** `aws_alb_target_group` is know as `aws_lb_target_group`. The functionality is identical. ## Example Usage ```hcl -resource "aws_alb_target_group" "test" { - name = "tf-example-alb-tg" +resource "aws_lb_target_group" "test" { + name = "tf-example-lb-tg" port = 80 protocol = "HTTP" vpc_id = "${aws_vpc.main.id}" @@ -71,5 +71,5 @@ The following attributes are exported in addition to the arguments listed above: Target Groups can be imported using their ARN, e.g. ``` -$ terraform import aws_alb_target_group.app_front_end arn:aws:elasticloadbalancing:us-west-2:187416307283:targetgroup/app-front-end/20cfe21448b66314 +$ terraform import aws_lb_target_group.app_front_end arn:aws:elasticloadbalancing:us-west-2:187416307283:targetgroup/app-front-end/20cfe21448b66314 ``` diff --git a/website/docs/r/alb_target_group_attachment.html.markdown b/website/docs/r/lb_target_group_attachment.html.markdown similarity index 68% rename from website/docs/r/alb_target_group_attachment.html.markdown rename to website/docs/r/lb_target_group_attachment.html.markdown index a0cac724118..9770ed30131 100644 --- a/website/docs/r/alb_target_group_attachment.html.markdown +++ b/website/docs/r/lb_target_group_attachment.html.markdown @@ -1,27 +1,29 @@ --- layout: "aws" -page_title: "AWS: aws_alb_target_group_attachment" -sidebar_current: "docs-aws-resource-alb-target-group-attachment" +page_title: "AWS: aws_lb_target_group_attachment" +sidebar_current: "docs-aws-resource-elbv2-target-group-attachment" description: |- - Provides the ability to register instances and containers with an ALB + Provides the ability to register instances and containers with a LB target group --- -# aws\_alb\_target\_group\_attachment +# aws_lb_target_group_attachment -Provides the ability to register instances and containers with an ALB +Provides the ability to register instances and containers with a LB target group +~> **Note:** `aws_alb_target_group_attachment` is known as `aws_lb_target_group_attachment`. The functionality is identical. + ## Example Usage ```hcl -resource "aws_alb_target_group_attachment" "test" { - target_group_arn = "${aws_alb_target_group.test.arn}" +resource "aws_lb_target_group_attachment" "test" { + target_group_arn = "${aws_lb_target_group.test.arn}" target_id = "${aws_instance.test.id}" port = 80 } -resource "aws_alb_target_group" "test" { +resource "aws_lb_target_group" "test" { // Other arguments }