github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_alb_target_group_test.go (about) 1 package aws 2 3 import ( 4 "errors" 5 "fmt" 6 "testing" 7 8 "github.com/aws/aws-sdk-go/aws" 9 "github.com/aws/aws-sdk-go/service/elbv2" 10 "github.com/hashicorp/errwrap" 11 "github.com/hashicorp/terraform/helper/acctest" 12 "github.com/hashicorp/terraform/helper/resource" 13 "github.com/hashicorp/terraform/terraform" 14 ) 15 16 func TestALBTargetGroupCloudwatchSuffixFromARN(t *testing.T) { 17 cases := []struct { 18 name string 19 arn *string 20 suffix string 21 }{ 22 { 23 name: "valid suffix", 24 arn: aws.String(`arn:aws:elasticloadbalancing:us-east-1:123456:targetgroup/my-targets/73e2d6bc24d8a067`), 25 suffix: `targetgroup/my-targets/73e2d6bc24d8a067`, 26 }, 27 { 28 name: "no suffix", 29 arn: aws.String(`arn:aws:elasticloadbalancing:us-east-1:123456:targetgroup`), 30 suffix: ``, 31 }, 32 { 33 name: "nil ARN", 34 arn: nil, 35 suffix: ``, 36 }, 37 } 38 39 for _, tc := range cases { 40 actual := albTargetGroupSuffixFromARN(tc.arn) 41 if actual != tc.suffix { 42 t.Fatalf("bad suffix: %q\nExpected: %s\n Got: %s", tc.name, tc.suffix, actual) 43 } 44 } 45 } 46 47 func TestAccAWSALBTargetGroup_basic(t *testing.T) { 48 var conf elbv2.TargetGroup 49 targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 50 51 resource.Test(t, resource.TestCase{ 52 PreCheck: func() { testAccPreCheck(t) }, 53 IDRefreshName: "aws_alb_target_group.test", 54 Providers: testAccProviders, 55 CheckDestroy: testAccCheckAWSALBTargetGroupDestroy, 56 Steps: []resource.TestStep{ 57 { 58 Config: testAccAWSALBTargetGroupConfig_basic(targetGroupName), 59 Check: resource.ComposeAggregateTestCheckFunc( 60 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf), 61 resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "arn"), 62 resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupName), 63 resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "443"), 64 resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTPS"), 65 resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "vpc_id"), 66 resource.TestCheckResourceAttr("aws_alb_target_group.test", "deregistration_delay", "200"), 67 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.#", "1"), 68 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.enabled", "true"), 69 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.type", "lb_cookie"), 70 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.cookie_duration", "10000"), 71 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.#", "1"), 72 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.path", "/health"), 73 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.interval", "60"), 74 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.port", "8081"), 75 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.protocol", "HTTP"), 76 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.timeout", "3"), 77 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.healthy_threshold", "3"), 78 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.unhealthy_threshold", "3"), 79 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.matcher", "200-299"), 80 ), 81 }, 82 }, 83 }) 84 } 85 86 func TestAccAWSALBTargetGroup_changeNameForceNew(t *testing.T) { 87 var before, after elbv2.TargetGroup 88 targetGroupNameBefore := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 89 targetGroupNameAfter := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(4, acctest.CharSetAlphaNum)) 90 91 resource.Test(t, resource.TestCase{ 92 PreCheck: func() { testAccPreCheck(t) }, 93 IDRefreshName: "aws_alb_target_group.test", 94 Providers: testAccProviders, 95 CheckDestroy: testAccCheckAWSALBTargetGroupDestroy, 96 Steps: []resource.TestStep{ 97 { 98 Config: testAccAWSALBTargetGroupConfig_basic(targetGroupNameBefore), 99 Check: resource.ComposeAggregateTestCheckFunc( 100 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &before), 101 resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupNameBefore), 102 ), 103 }, 104 { 105 Config: testAccAWSALBTargetGroupConfig_basic(targetGroupNameAfter), 106 Check: resource.ComposeAggregateTestCheckFunc( 107 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &after), 108 resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupNameAfter), 109 ), 110 }, 111 }, 112 }) 113 } 114 115 func TestAccAWSALBTargetGroup_changeProtocolForceNew(t *testing.T) { 116 var before, after elbv2.TargetGroup 117 targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 118 119 resource.Test(t, resource.TestCase{ 120 PreCheck: func() { testAccPreCheck(t) }, 121 IDRefreshName: "aws_alb_target_group.test", 122 Providers: testAccProviders, 123 CheckDestroy: testAccCheckAWSALBTargetGroupDestroy, 124 Steps: []resource.TestStep{ 125 { 126 Config: testAccAWSALBTargetGroupConfig_basic(targetGroupName), 127 Check: resource.ComposeAggregateTestCheckFunc( 128 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &before), 129 resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTPS"), 130 ), 131 }, 132 { 133 Config: testAccAWSALBTargetGroupConfig_updatedProtocol(targetGroupName), 134 Check: resource.ComposeAggregateTestCheckFunc( 135 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &after), 136 resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTP"), 137 ), 138 }, 139 }, 140 }) 141 } 142 143 func TestAccAWSALBTargetGroup_changePortForceNew(t *testing.T) { 144 var before, after elbv2.TargetGroup 145 targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 146 147 resource.Test(t, resource.TestCase{ 148 PreCheck: func() { testAccPreCheck(t) }, 149 IDRefreshName: "aws_alb_target_group.test", 150 Providers: testAccProviders, 151 CheckDestroy: testAccCheckAWSALBTargetGroupDestroy, 152 Steps: []resource.TestStep{ 153 { 154 Config: testAccAWSALBTargetGroupConfig_basic(targetGroupName), 155 Check: resource.ComposeAggregateTestCheckFunc( 156 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &before), 157 resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "443"), 158 ), 159 }, 160 { 161 Config: testAccAWSALBTargetGroupConfig_updatedPort(targetGroupName), 162 Check: resource.ComposeAggregateTestCheckFunc( 163 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &after), 164 resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "442"), 165 ), 166 }, 167 }, 168 }) 169 } 170 171 func TestAccAWSALBTargetGroup_changeVpcForceNew(t *testing.T) { 172 var before, after elbv2.TargetGroup 173 targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 174 175 resource.Test(t, resource.TestCase{ 176 PreCheck: func() { testAccPreCheck(t) }, 177 IDRefreshName: "aws_alb_target_group.test", 178 Providers: testAccProviders, 179 CheckDestroy: testAccCheckAWSALBTargetGroupDestroy, 180 Steps: []resource.TestStep{ 181 { 182 Config: testAccAWSALBTargetGroupConfig_basic(targetGroupName), 183 Check: resource.ComposeAggregateTestCheckFunc( 184 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &before), 185 ), 186 }, 187 { 188 Config: testAccAWSALBTargetGroupConfig_updatedVpc(targetGroupName), 189 Check: resource.ComposeAggregateTestCheckFunc( 190 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &after), 191 ), 192 }, 193 }, 194 }) 195 } 196 197 func TestAccAWSALBTargetGroup_tags(t *testing.T) { 198 var conf elbv2.TargetGroup 199 targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 200 201 resource.Test(t, resource.TestCase{ 202 PreCheck: func() { testAccPreCheck(t) }, 203 IDRefreshName: "aws_alb_target_group.test", 204 Providers: testAccProviders, 205 CheckDestroy: testAccCheckAWSALBTargetGroupDestroy, 206 Steps: []resource.TestStep{ 207 { 208 Config: testAccAWSALBTargetGroupConfig_basic(targetGroupName), 209 Check: resource.ComposeAggregateTestCheckFunc( 210 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf), 211 resource.TestCheckResourceAttr("aws_alb_target_group.test", "tags.%", "1"), 212 resource.TestCheckResourceAttr("aws_alb_target_group.test", "tags.TestName", "TestAccAWSALBTargetGroup_basic"), 213 ), 214 }, 215 { 216 Config: testAccAWSALBTargetGroupConfig_updateTags(targetGroupName), 217 Check: resource.ComposeAggregateTestCheckFunc( 218 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf), 219 resource.TestCheckResourceAttr("aws_alb_target_group.test", "tags.%", "2"), 220 resource.TestCheckResourceAttr("aws_alb_target_group.test", "tags.Environment", "Production"), 221 resource.TestCheckResourceAttr("aws_alb_target_group.test", "tags.Type", "ALB Target Group"), 222 ), 223 }, 224 }, 225 }) 226 } 227 228 func TestAccAWSALBTargetGroup_updateHealthCheck(t *testing.T) { 229 var conf elbv2.TargetGroup 230 targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 231 232 resource.Test(t, resource.TestCase{ 233 PreCheck: func() { testAccPreCheck(t) }, 234 IDRefreshName: "aws_alb_target_group.test", 235 Providers: testAccProviders, 236 CheckDestroy: testAccCheckAWSALBTargetGroupDestroy, 237 Steps: []resource.TestStep{ 238 { 239 Config: testAccAWSALBTargetGroupConfig_basic(targetGroupName), 240 Check: resource.ComposeAggregateTestCheckFunc( 241 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf), 242 resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "arn"), 243 resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupName), 244 resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "443"), 245 resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTPS"), 246 resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "vpc_id"), 247 resource.TestCheckResourceAttr("aws_alb_target_group.test", "deregistration_delay", "200"), 248 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.#", "1"), 249 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.type", "lb_cookie"), 250 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.cookie_duration", "10000"), 251 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.#", "1"), 252 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.path", "/health"), 253 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.interval", "60"), 254 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.port", "8081"), 255 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.protocol", "HTTP"), 256 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.timeout", "3"), 257 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.healthy_threshold", "3"), 258 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.unhealthy_threshold", "3"), 259 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.matcher", "200-299"), 260 ), 261 }, 262 { 263 Config: testAccAWSALBTargetGroupConfig_updateHealthCheck(targetGroupName), 264 Check: resource.ComposeAggregateTestCheckFunc( 265 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf), 266 resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "arn"), 267 resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupName), 268 resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "443"), 269 resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTPS"), 270 resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "vpc_id"), 271 resource.TestCheckResourceAttr("aws_alb_target_group.test", "deregistration_delay", "200"), 272 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.#", "1"), 273 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.type", "lb_cookie"), 274 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.cookie_duration", "10000"), 275 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.#", "1"), 276 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.path", "/health2"), 277 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.interval", "30"), 278 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.port", "8082"), 279 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.protocol", "HTTPS"), 280 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.timeout", "4"), 281 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.healthy_threshold", "4"), 282 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.unhealthy_threshold", "4"), 283 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.matcher", "200"), 284 ), 285 }, 286 }, 287 }) 288 } 289 290 func TestAccAWSALBTargetGroup_updateSticknessEnabled(t *testing.T) { 291 var conf elbv2.TargetGroup 292 targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 293 294 resource.Test(t, resource.TestCase{ 295 PreCheck: func() { testAccPreCheck(t) }, 296 IDRefreshName: "aws_alb_target_group.test", 297 Providers: testAccProviders, 298 CheckDestroy: testAccCheckAWSALBTargetGroupDestroy, 299 Steps: []resource.TestStep{ 300 { 301 Config: testAccAWSALBTargetGroupConfig_stickiness(targetGroupName, false, false), 302 Check: resource.ComposeAggregateTestCheckFunc( 303 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf), 304 resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "arn"), 305 resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupName), 306 resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "443"), 307 resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTPS"), 308 resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "vpc_id"), 309 resource.TestCheckResourceAttr("aws_alb_target_group.test", "deregistration_delay", "200"), 310 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.#", "1"), 311 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.path", "/health2"), 312 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.interval", "30"), 313 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.port", "8082"), 314 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.protocol", "HTTPS"), 315 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.timeout", "4"), 316 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.healthy_threshold", "4"), 317 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.unhealthy_threshold", "4"), 318 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.matcher", "200"), 319 ), 320 }, 321 { 322 Config: testAccAWSALBTargetGroupConfig_stickiness(targetGroupName, true, true), 323 Check: resource.ComposeAggregateTestCheckFunc( 324 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf), 325 resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "arn"), 326 resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupName), 327 resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "443"), 328 resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTPS"), 329 resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "vpc_id"), 330 resource.TestCheckResourceAttr("aws_alb_target_group.test", "deregistration_delay", "200"), 331 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.#", "1"), 332 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.enabled", "true"), 333 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.type", "lb_cookie"), 334 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.cookie_duration", "10000"), 335 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.#", "1"), 336 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.path", "/health2"), 337 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.interval", "30"), 338 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.port", "8082"), 339 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.protocol", "HTTPS"), 340 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.timeout", "4"), 341 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.healthy_threshold", "4"), 342 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.unhealthy_threshold", "4"), 343 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.matcher", "200"), 344 ), 345 }, 346 { 347 Config: testAccAWSALBTargetGroupConfig_stickiness(targetGroupName, true, false), 348 Check: resource.ComposeAggregateTestCheckFunc( 349 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf), 350 resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "arn"), 351 resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupName), 352 resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "443"), 353 resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTPS"), 354 resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "vpc_id"), 355 resource.TestCheckResourceAttr("aws_alb_target_group.test", "deregistration_delay", "200"), 356 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.#", "1"), 357 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.enabled", "false"), 358 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.type", "lb_cookie"), 359 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.cookie_duration", "10000"), 360 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.#", "1"), 361 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.path", "/health2"), 362 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.interval", "30"), 363 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.port", "8082"), 364 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.protocol", "HTTPS"), 365 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.timeout", "4"), 366 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.healthy_threshold", "4"), 367 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.unhealthy_threshold", "4"), 368 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.matcher", "200"), 369 ), 370 }, 371 }, 372 }) 373 } 374 375 func testAccCheckAWSALBTargetGroupExists(n string, res *elbv2.TargetGroup) resource.TestCheckFunc { 376 return func(s *terraform.State) error { 377 rs, ok := s.RootModule().Resources[n] 378 if !ok { 379 return fmt.Errorf("Not found: %s", n) 380 } 381 382 if rs.Primary.ID == "" { 383 return errors.New("No Target Group ID is set") 384 } 385 386 conn := testAccProvider.Meta().(*AWSClient).elbv2conn 387 388 describe, err := conn.DescribeTargetGroups(&elbv2.DescribeTargetGroupsInput{ 389 TargetGroupArns: []*string{aws.String(rs.Primary.ID)}, 390 }) 391 392 if err != nil { 393 return err 394 } 395 396 if len(describe.TargetGroups) != 1 || 397 *describe.TargetGroups[0].TargetGroupArn != rs.Primary.ID { 398 return errors.New("Target Group not found") 399 } 400 401 *res = *describe.TargetGroups[0] 402 return nil 403 } 404 } 405 406 func testAccCheckAWSALBTargetGroupDestroy(s *terraform.State) error { 407 conn := testAccProvider.Meta().(*AWSClient).elbv2conn 408 409 for _, rs := range s.RootModule().Resources { 410 if rs.Type != "aws_alb_target_group" { 411 continue 412 } 413 414 describe, err := conn.DescribeTargetGroups(&elbv2.DescribeTargetGroupsInput{ 415 TargetGroupArns: []*string{aws.String(rs.Primary.ID)}, 416 }) 417 418 if err == nil { 419 if len(describe.TargetGroups) != 0 && 420 *describe.TargetGroups[0].TargetGroupArn == rs.Primary.ID { 421 return fmt.Errorf("Target Group %q still exists", rs.Primary.ID) 422 } 423 } 424 425 // Verify the error 426 if isTargetGroupNotFound(err) { 427 return nil 428 } else { 429 return errwrap.Wrapf("Unexpected error checking ALB destroyed: {{err}}", err) 430 } 431 } 432 433 return nil 434 } 435 436 func testAccAWSALBTargetGroupConfig_basic(targetGroupName string) string { 437 return fmt.Sprintf(`resource "aws_alb_target_group" "test" { 438 name = "%s" 439 port = 443 440 protocol = "HTTPS" 441 vpc_id = "${aws_vpc.test.id}" 442 443 deregistration_delay = 200 444 445 stickiness { 446 type = "lb_cookie" 447 cookie_duration = 10000 448 } 449 450 health_check { 451 path = "/health" 452 interval = 60 453 port = 8081 454 protocol = "HTTP" 455 timeout = 3 456 healthy_threshold = 3 457 unhealthy_threshold = 3 458 matcher = "200-299" 459 } 460 461 tags { 462 TestName = "TestAccAWSALBTargetGroup_basic" 463 } 464 } 465 466 resource "aws_vpc" "test" { 467 cidr_block = "10.0.0.0/16" 468 469 tags { 470 TestName = "TestAccAWSALBTargetGroup_basic" 471 } 472 }`, targetGroupName) 473 } 474 475 func testAccAWSALBTargetGroupConfig_updatedPort(targetGroupName string) string { 476 return fmt.Sprintf(`resource "aws_alb_target_group" "test" { 477 name = "%s" 478 port = 442 479 protocol = "HTTPS" 480 vpc_id = "${aws_vpc.test.id}" 481 482 deregistration_delay = 200 483 484 stickiness { 485 type = "lb_cookie" 486 cookie_duration = 10000 487 } 488 489 health_check { 490 path = "/health" 491 interval = 60 492 port = 8081 493 protocol = "HTTP" 494 timeout = 3 495 healthy_threshold = 3 496 unhealthy_threshold = 3 497 matcher = "200-299" 498 } 499 500 tags { 501 TestName = "TestAccAWSALBTargetGroup_basic" 502 } 503 } 504 505 resource "aws_vpc" "test" { 506 cidr_block = "10.0.0.0/16" 507 508 tags { 509 TestName = "TestAccAWSALBTargetGroup_basic" 510 } 511 }`, targetGroupName) 512 } 513 514 func testAccAWSALBTargetGroupConfig_updatedProtocol(targetGroupName string) string { 515 return fmt.Sprintf(`resource "aws_alb_target_group" "test" { 516 name = "%s" 517 port = 443 518 protocol = "HTTP" 519 vpc_id = "${aws_vpc.test2.id}" 520 521 deregistration_delay = 200 522 523 stickiness { 524 type = "lb_cookie" 525 cookie_duration = 10000 526 } 527 528 health_check { 529 path = "/health" 530 interval = 60 531 port = 8081 532 protocol = "HTTP" 533 timeout = 3 534 healthy_threshold = 3 535 unhealthy_threshold = 3 536 matcher = "200-299" 537 } 538 539 tags { 540 TestName = "TestAccAWSALBTargetGroup_basic" 541 } 542 } 543 544 resource "aws_vpc" "test2" { 545 cidr_block = "10.10.0.0/16" 546 547 tags { 548 TestName = "TestAccAWSALBTargetGroup_basic" 549 } 550 } 551 552 resource "aws_vpc" "test" { 553 cidr_block = "10.0.0.0/16" 554 555 tags { 556 TestName = "TestAccAWSALBTargetGroup_basic" 557 } 558 }`, targetGroupName) 559 } 560 561 func testAccAWSALBTargetGroupConfig_updatedVpc(targetGroupName string) string { 562 return fmt.Sprintf(`resource "aws_alb_target_group" "test" { 563 name = "%s" 564 port = 443 565 protocol = "HTTPS" 566 vpc_id = "${aws_vpc.test.id}" 567 568 deregistration_delay = 200 569 570 stickiness { 571 type = "lb_cookie" 572 cookie_duration = 10000 573 } 574 575 health_check { 576 path = "/health" 577 interval = 60 578 port = 8081 579 protocol = "HTTP" 580 timeout = 3 581 healthy_threshold = 3 582 unhealthy_threshold = 3 583 matcher = "200-299" 584 } 585 586 tags { 587 TestName = "TestAccAWSALBTargetGroup_basic" 588 } 589 } 590 591 resource "aws_vpc" "test" { 592 cidr_block = "10.0.0.0/16" 593 594 tags { 595 TestName = "TestAccAWSALBTargetGroup_basic" 596 } 597 }`, targetGroupName) 598 } 599 600 func testAccAWSALBTargetGroupConfig_updateTags(targetGroupName string) string { 601 return fmt.Sprintf(`resource "aws_alb_target_group" "test" { 602 name = "%s" 603 port = 443 604 protocol = "HTTPS" 605 vpc_id = "${aws_vpc.test.id}" 606 607 deregistration_delay = 200 608 609 stickiness { 610 type = "lb_cookie" 611 cookie_duration = 10000 612 } 613 614 health_check { 615 path = "/health" 616 interval = 60 617 port = 8081 618 protocol = "HTTP" 619 timeout = 3 620 healthy_threshold = 3 621 unhealthy_threshold = 3 622 matcher = "200-299" 623 } 624 625 tags { 626 Environment = "Production" 627 Type = "ALB Target Group" 628 } 629 } 630 631 resource "aws_vpc" "test" { 632 cidr_block = "10.0.0.0/16" 633 634 tags { 635 TestName = "TestAccAWSALBTargetGroup_basic" 636 } 637 }`, targetGroupName) 638 } 639 640 func testAccAWSALBTargetGroupConfig_updateHealthCheck(targetGroupName string) string { 641 return fmt.Sprintf(`resource "aws_alb_target_group" "test" { 642 name = "%s" 643 port = 443 644 protocol = "HTTPS" 645 vpc_id = "${aws_vpc.test.id}" 646 647 deregistration_delay = 200 648 649 stickiness { 650 type = "lb_cookie" 651 cookie_duration = 10000 652 } 653 654 health_check { 655 path = "/health2" 656 interval = 30 657 port = 8082 658 protocol = "HTTPS" 659 timeout = 4 660 healthy_threshold = 4 661 unhealthy_threshold = 4 662 matcher = "200" 663 } 664 } 665 666 resource "aws_vpc" "test" { 667 cidr_block = "10.0.0.0/16" 668 669 tags { 670 TestName = "TestAccAWSALBTargetGroup_basic" 671 } 672 }`, targetGroupName) 673 } 674 675 func testAccAWSALBTargetGroupConfig_stickiness(targetGroupName string, addStickinessBlock bool, enabled bool) string { 676 var stickinessBlock string 677 678 if addStickinessBlock { 679 stickinessBlock = fmt.Sprintf(`stickiness { 680 enabled = "%t" 681 type = "lb_cookie" 682 cookie_duration = 10000 683 }`, enabled) 684 } 685 686 return fmt.Sprintf(`resource "aws_alb_target_group" "test" { 687 name = "%s" 688 port = 443 689 protocol = "HTTPS" 690 vpc_id = "${aws_vpc.test.id}" 691 692 deregistration_delay = 200 693 694 %s 695 696 health_check { 697 path = "/health2" 698 interval = 30 699 port = 8082 700 protocol = "HTTPS" 701 timeout = 4 702 healthy_threshold = 4 703 unhealthy_threshold = 4 704 matcher = "200" 705 } 706 } 707 708 resource "aws_vpc" "test" { 709 cidr_block = "10.0.0.0/16" 710 711 tags { 712 TestName = "TestAccAWSALBTargetGroup_stickiness" 713 } 714 }`, targetGroupName, stickinessBlock) 715 }