github.com/dougneal/terraform@v0.6.15-0.20170330092735-b6a3840768a4/builtin/providers/aws/resource_aws_autoscaling_group_test.go (about) 1 package aws 2 3 import ( 4 "errors" 5 "fmt" 6 "reflect" 7 "regexp" 8 "sort" 9 "strings" 10 "testing" 11 12 "github.com/aws/aws-sdk-go/aws" 13 "github.com/aws/aws-sdk-go/aws/awserr" 14 "github.com/aws/aws-sdk-go/service/autoscaling" 15 "github.com/aws/aws-sdk-go/service/elbv2" 16 "github.com/hashicorp/terraform/helper/acctest" 17 "github.com/hashicorp/terraform/helper/resource" 18 "github.com/hashicorp/terraform/terraform" 19 ) 20 21 func TestAccAWSAutoScalingGroup_basic(t *testing.T) { 22 var group autoscaling.Group 23 var lc autoscaling.LaunchConfiguration 24 25 randName := fmt.Sprintf("terraform-test-%s", acctest.RandString(10)) 26 27 resource.Test(t, resource.TestCase{ 28 PreCheck: func() { testAccPreCheck(t) }, 29 IDRefreshName: "aws_autoscaling_group.bar", 30 IDRefreshIgnore: []string{"force_delete", "metrics_granularity", "wait_for_capacity_timeout"}, 31 Providers: testAccProviders, 32 CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, 33 Steps: []resource.TestStep{ 34 resource.TestStep{ 35 Config: testAccAWSAutoScalingGroupConfig(randName), 36 Check: resource.ComposeTestCheckFunc( 37 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 38 testAccCheckAWSAutoScalingGroupHealthyCapacity(&group, 2), 39 testAccCheckAWSAutoScalingGroupAttributes(&group, randName), 40 resource.TestCheckResourceAttr( 41 "aws_autoscaling_group.bar", "availability_zones.2487133097", "us-west-2a"), 42 resource.TestCheckResourceAttr( 43 "aws_autoscaling_group.bar", "name", randName), 44 resource.TestCheckResourceAttr( 45 "aws_autoscaling_group.bar", "max_size", "5"), 46 resource.TestCheckResourceAttr( 47 "aws_autoscaling_group.bar", "min_size", "2"), 48 resource.TestCheckResourceAttr( 49 "aws_autoscaling_group.bar", "health_check_grace_period", "300"), 50 resource.TestCheckResourceAttr( 51 "aws_autoscaling_group.bar", "health_check_type", "ELB"), 52 resource.TestCheckResourceAttr( 53 "aws_autoscaling_group.bar", "desired_capacity", "4"), 54 resource.TestCheckResourceAttr( 55 "aws_autoscaling_group.bar", "force_delete", "true"), 56 resource.TestCheckResourceAttr( 57 "aws_autoscaling_group.bar", "termination_policies.0", "OldestInstance"), 58 resource.TestCheckResourceAttr( 59 "aws_autoscaling_group.bar", "termination_policies.1", "ClosestToNextInstanceHour"), 60 resource.TestCheckResourceAttr( 61 "aws_autoscaling_group.bar", "protect_from_scale_in", "false"), 62 ), 63 }, 64 65 resource.TestStep{ 66 Config: testAccAWSAutoScalingGroupConfigUpdate(randName), 67 Check: resource.ComposeTestCheckFunc( 68 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 69 testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.new", &lc), 70 resource.TestCheckResourceAttr( 71 "aws_autoscaling_group.bar", "desired_capacity", "5"), 72 resource.TestCheckResourceAttr( 73 "aws_autoscaling_group.bar", "termination_policies.0", "ClosestToNextInstanceHour"), 74 resource.TestCheckResourceAttr( 75 "aws_autoscaling_group.bar", "protect_from_scale_in", "true"), 76 testLaunchConfigurationName("aws_autoscaling_group.bar", &lc), 77 testAccCheckAutoscalingTags(&group.Tags, "Bar", map[string]interface{}{ 78 "value": "bar-foo", 79 "propagate_at_launch": true, 80 }), 81 ), 82 }, 83 }, 84 }) 85 } 86 87 func TestAccAWSAutoScalingGroup_namePrefix(t *testing.T) { 88 nameRegexp := regexp.MustCompile("^test-") 89 90 resource.Test(t, resource.TestCase{ 91 PreCheck: func() { testAccPreCheck(t) }, 92 Providers: testAccProviders, 93 CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, 94 Steps: []resource.TestStep{ 95 resource.TestStep{ 96 Config: testAccAWSAutoScalingGroupConfig_namePrefix, 97 Check: resource.ComposeTestCheckFunc( 98 resource.TestMatchResourceAttr( 99 "aws_autoscaling_group.test", "name", nameRegexp), 100 resource.TestCheckResourceAttrSet( 101 "aws_autoscaling_group.test", "arn"), 102 ), 103 }, 104 }, 105 }) 106 } 107 108 func TestAccAWSAutoScalingGroup_autoGeneratedName(t *testing.T) { 109 asgNameRegexp := regexp.MustCompile("^tf-asg-") 110 111 resource.Test(t, resource.TestCase{ 112 PreCheck: func() { testAccPreCheck(t) }, 113 Providers: testAccProviders, 114 CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, 115 Steps: []resource.TestStep{ 116 resource.TestStep{ 117 Config: testAccAWSAutoScalingGroupConfig_autoGeneratedName, 118 Check: resource.ComposeTestCheckFunc( 119 resource.TestMatchResourceAttr( 120 "aws_autoscaling_group.bar", "name", asgNameRegexp), 121 resource.TestCheckResourceAttrSet( 122 "aws_autoscaling_group.bar", "arn"), 123 ), 124 }, 125 }, 126 }) 127 } 128 129 func TestAccAWSAutoScalingGroup_terminationPolicies(t *testing.T) { 130 resource.Test(t, resource.TestCase{ 131 PreCheck: func() { testAccPreCheck(t) }, 132 Providers: testAccProviders, 133 CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, 134 Steps: []resource.TestStep{ 135 resource.TestStep{ 136 Config: testAccAWSAutoScalingGroupConfig_terminationPoliciesEmpty, 137 Check: resource.ComposeTestCheckFunc( 138 resource.TestCheckResourceAttr( 139 "aws_autoscaling_group.bar", "termination_policies.#", "0"), 140 ), 141 }, 142 143 resource.TestStep{ 144 Config: testAccAWSAutoScalingGroupConfig_terminationPoliciesUpdate, 145 Check: resource.ComposeTestCheckFunc( 146 resource.TestCheckResourceAttr( 147 "aws_autoscaling_group.bar", "termination_policies.#", "1"), 148 resource.TestCheckResourceAttr( 149 "aws_autoscaling_group.bar", "termination_policies.0", "OldestInstance"), 150 ), 151 }, 152 153 resource.TestStep{ 154 Config: testAccAWSAutoScalingGroupConfig_terminationPoliciesExplicitDefault, 155 Check: resource.ComposeTestCheckFunc( 156 resource.TestCheckResourceAttr( 157 "aws_autoscaling_group.bar", "termination_policies.#", "1"), 158 resource.TestCheckResourceAttr( 159 "aws_autoscaling_group.bar", "termination_policies.0", "Default"), 160 ), 161 }, 162 163 resource.TestStep{ 164 Config: testAccAWSAutoScalingGroupConfig_terminationPoliciesEmpty, 165 Check: resource.ComposeTestCheckFunc( 166 resource.TestCheckResourceAttr( 167 "aws_autoscaling_group.bar", "termination_policies.#", "0"), 168 ), 169 }, 170 }, 171 }) 172 } 173 174 func TestAccAWSAutoScalingGroup_tags(t *testing.T) { 175 var group autoscaling.Group 176 177 randName := fmt.Sprintf("tfautotags-%s", acctest.RandString(5)) 178 179 resource.Test(t, resource.TestCase{ 180 PreCheck: func() { testAccPreCheck(t) }, 181 Providers: testAccProviders, 182 CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, 183 Steps: []resource.TestStep{ 184 resource.TestStep{ 185 Config: testAccAWSAutoScalingGroupConfig(randName), 186 Check: resource.ComposeTestCheckFunc( 187 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 188 testAccCheckAutoscalingTags(&group.Tags, "Foo", map[string]interface{}{ 189 "value": "foo-bar", 190 "propagate_at_launch": true, 191 }), 192 ), 193 }, 194 195 resource.TestStep{ 196 Config: testAccAWSAutoScalingGroupConfigUpdate(randName), 197 Check: resource.ComposeTestCheckFunc( 198 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 199 testAccCheckAutoscalingTagNotExists(&group.Tags, "Foo"), 200 testAccCheckAutoscalingTags(&group.Tags, "Bar", map[string]interface{}{ 201 "value": "bar-foo", 202 "propagate_at_launch": true, 203 }), 204 ), 205 }, 206 }, 207 }) 208 } 209 210 func TestAccAWSAutoScalingGroup_VpcUpdates(t *testing.T) { 211 var group autoscaling.Group 212 213 resource.Test(t, resource.TestCase{ 214 PreCheck: func() { testAccPreCheck(t) }, 215 Providers: testAccProviders, 216 CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, 217 Steps: []resource.TestStep{ 218 resource.TestStep{ 219 Config: testAccAWSAutoScalingGroupConfigWithAZ, 220 Check: resource.ComposeTestCheckFunc( 221 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 222 resource.TestCheckResourceAttr( 223 "aws_autoscaling_group.bar", "availability_zones.#", "1"), 224 resource.TestCheckResourceAttr( 225 "aws_autoscaling_group.bar", "availability_zones.2487133097", "us-west-2a"), 226 resource.TestCheckResourceAttr( 227 "aws_autoscaling_group.bar", "vpc_zone_identifier.#", "1"), 228 ), 229 }, 230 231 resource.TestStep{ 232 Config: testAccAWSAutoScalingGroupConfigWithVPCIdent, 233 Check: resource.ComposeTestCheckFunc( 234 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 235 testAccCheckAWSAutoScalingGroupAttributesVPCZoneIdentifer(&group), 236 resource.TestCheckResourceAttr( 237 "aws_autoscaling_group.bar", "availability_zones.#", "1"), 238 resource.TestCheckResourceAttr( 239 "aws_autoscaling_group.bar", "availability_zones.2487133097", "us-west-2a"), 240 resource.TestCheckResourceAttr( 241 "aws_autoscaling_group.bar", "vpc_zone_identifier.#", "1"), 242 ), 243 }, 244 }, 245 }) 246 } 247 248 func TestAccAWSAutoScalingGroup_WithLoadBalancer(t *testing.T) { 249 var group autoscaling.Group 250 251 resource.Test(t, resource.TestCase{ 252 PreCheck: func() { testAccPreCheck(t) }, 253 Providers: testAccProviders, 254 CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, 255 Steps: []resource.TestStep{ 256 resource.TestStep{ 257 Config: testAccAWSAutoScalingGroupConfigWithLoadBalancer, 258 Check: resource.ComposeTestCheckFunc( 259 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 260 testAccCheckAWSAutoScalingGroupAttributesLoadBalancer(&group), 261 ), 262 }, 263 }, 264 }) 265 } 266 267 func TestAccAWSAutoScalingGroup_withPlacementGroup(t *testing.T) { 268 var group autoscaling.Group 269 270 randName := fmt.Sprintf("tf_placement_test-%s", acctest.RandString(5)) 271 resource.Test(t, resource.TestCase{ 272 PreCheck: func() { testAccPreCheck(t) }, 273 Providers: testAccProviders, 274 CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, 275 Steps: []resource.TestStep{ 276 resource.TestStep{ 277 Config: testAccAWSAutoScalingGroupConfig_withPlacementGroup(randName), 278 Check: resource.ComposeTestCheckFunc( 279 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 280 resource.TestCheckResourceAttr( 281 "aws_autoscaling_group.bar", "placement_group", randName), 282 ), 283 }, 284 }, 285 }) 286 } 287 288 func TestAccAWSAutoScalingGroup_enablingMetrics(t *testing.T) { 289 var group autoscaling.Group 290 randName := fmt.Sprintf("terraform-test-%s", acctest.RandString(10)) 291 292 resource.Test(t, resource.TestCase{ 293 PreCheck: func() { testAccPreCheck(t) }, 294 Providers: testAccProviders, 295 CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, 296 Steps: []resource.TestStep{ 297 resource.TestStep{ 298 Config: testAccAWSAutoScalingGroupConfig(randName), 299 Check: resource.ComposeTestCheckFunc( 300 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 301 resource.TestCheckNoResourceAttr( 302 "aws_autoscaling_group.bar", "enabled_metrics"), 303 ), 304 }, 305 306 resource.TestStep{ 307 Config: testAccAWSAutoscalingMetricsCollectionConfig_updatingMetricsCollected, 308 Check: resource.ComposeTestCheckFunc( 309 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 310 resource.TestCheckResourceAttr( 311 "aws_autoscaling_group.bar", "enabled_metrics.#", "5"), 312 ), 313 }, 314 }, 315 }) 316 } 317 318 func TestAccAWSAutoScalingGroup_suspendingProcesses(t *testing.T) { 319 var group autoscaling.Group 320 randName := fmt.Sprintf("terraform-test-%s", acctest.RandString(10)) 321 322 resource.Test(t, resource.TestCase{ 323 PreCheck: func() { testAccPreCheck(t) }, 324 Providers: testAccProviders, 325 CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, 326 Steps: []resource.TestStep{ 327 { 328 Config: testAccAWSAutoScalingGroupConfig(randName), 329 Check: resource.ComposeTestCheckFunc( 330 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 331 resource.TestCheckResourceAttr( 332 "aws_autoscaling_group.bar", "suspended_processes.#", "0"), 333 ), 334 }, 335 { 336 Config: testAccAWSAutoScalingGroupConfigWithSuspendedProcesses(randName), 337 Check: resource.ComposeTestCheckFunc( 338 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 339 resource.TestCheckResourceAttr( 340 "aws_autoscaling_group.bar", "suspended_processes.#", "2"), 341 ), 342 }, 343 { 344 Config: testAccAWSAutoScalingGroupConfigWithSuspendedProcessesUpdated(randName), 345 Check: resource.ComposeTestCheckFunc( 346 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 347 resource.TestCheckResourceAttr( 348 "aws_autoscaling_group.bar", "suspended_processes.#", "2"), 349 ), 350 }, 351 }, 352 }) 353 } 354 355 func TestAccAWSAutoScalingGroup_withMetrics(t *testing.T) { 356 var group autoscaling.Group 357 358 resource.Test(t, resource.TestCase{ 359 PreCheck: func() { testAccPreCheck(t) }, 360 Providers: testAccProviders, 361 CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, 362 Steps: []resource.TestStep{ 363 resource.TestStep{ 364 Config: testAccAWSAutoscalingMetricsCollectionConfig_allMetricsCollected, 365 Check: resource.ComposeTestCheckFunc( 366 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 367 resource.TestCheckResourceAttr( 368 "aws_autoscaling_group.bar", "enabled_metrics.#", "7"), 369 ), 370 }, 371 372 resource.TestStep{ 373 Config: testAccAWSAutoscalingMetricsCollectionConfig_updatingMetricsCollected, 374 Check: resource.ComposeTestCheckFunc( 375 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 376 resource.TestCheckResourceAttr( 377 "aws_autoscaling_group.bar", "enabled_metrics.#", "5"), 378 ), 379 }, 380 }, 381 }) 382 } 383 384 func TestAccAWSAutoScalingGroup_ALB_TargetGroups(t *testing.T) { 385 var group autoscaling.Group 386 var tg elbv2.TargetGroup 387 var tg2 elbv2.TargetGroup 388 389 testCheck := func(targets []*elbv2.TargetGroup) resource.TestCheckFunc { 390 return func(*terraform.State) error { 391 var ts []string 392 var gs []string 393 for _, t := range targets { 394 ts = append(ts, *t.TargetGroupArn) 395 } 396 397 for _, s := range group.TargetGroupARNs { 398 gs = append(gs, *s) 399 } 400 401 sort.Strings(ts) 402 sort.Strings(gs) 403 404 if !reflect.DeepEqual(ts, gs) { 405 return fmt.Errorf("Error: target group match not found!\nASG Target groups: %#v\nTarget Group: %#v", ts, gs) 406 } 407 return nil 408 } 409 } 410 411 resource.Test(t, resource.TestCase{ 412 PreCheck: func() { testAccPreCheck(t) }, 413 Providers: testAccProviders, 414 CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, 415 Steps: []resource.TestStep{ 416 resource.TestStep{ 417 Config: testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_pre, 418 Check: resource.ComposeAggregateTestCheckFunc( 419 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 420 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &tg), 421 resource.TestCheckResourceAttr( 422 "aws_autoscaling_group.bar", "target_group_arns.#", "0"), 423 ), 424 }, 425 426 resource.TestStep{ 427 Config: testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_post_duo, 428 Check: resource.ComposeAggregateTestCheckFunc( 429 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 430 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &tg), 431 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test_more", &tg2), 432 testCheck([]*elbv2.TargetGroup{&tg, &tg2}), 433 resource.TestCheckResourceAttr( 434 "aws_autoscaling_group.bar", "target_group_arns.#", "2"), 435 ), 436 }, 437 438 resource.TestStep{ 439 Config: testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_post, 440 Check: resource.ComposeAggregateTestCheckFunc( 441 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 442 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &tg), 443 testCheck([]*elbv2.TargetGroup{&tg}), 444 resource.TestCheckResourceAttr( 445 "aws_autoscaling_group.bar", "target_group_arns.#", "1"), 446 ), 447 }, 448 449 resource.TestStep{ 450 Config: testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_pre, 451 Check: resource.ComposeAggregateTestCheckFunc( 452 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 453 resource.TestCheckResourceAttr( 454 "aws_autoscaling_group.bar", "target_group_arns.#", "0"), 455 ), 456 }, 457 }, 458 }) 459 } 460 461 func TestAccAWSAutoScalingGroup_initialLifecycleHook(t *testing.T) { 462 var group autoscaling.Group 463 464 randName := fmt.Sprintf("terraform-test-%s", acctest.RandString(10)) 465 466 resource.Test(t, resource.TestCase{ 467 PreCheck: func() { testAccPreCheck(t) }, 468 IDRefreshName: "aws_autoscaling_group.bar", 469 IDRefreshIgnore: []string{"force_delete", "metrics_granularity", "wait_for_capacity_timeout"}, 470 Providers: testAccProviders, 471 CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, 472 Steps: []resource.TestStep{ 473 resource.TestStep{ 474 Config: testAccAWSAutoScalingGroupWithHookConfig(randName), 475 Check: resource.ComposeTestCheckFunc( 476 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 477 testAccCheckAWSAutoScalingGroupHealthyCapacity(&group, 2), 478 resource.TestCheckResourceAttr( 479 "aws_autoscaling_group.bar", "initial_lifecycle_hook.#", "1"), 480 resource.TestCheckResourceAttr( 481 "aws_autoscaling_group.bar", "initial_lifecycle_hook.391359060.default_result", "CONTINUE"), 482 resource.TestCheckResourceAttr( 483 "aws_autoscaling_group.bar", "initial_lifecycle_hook.391359060.name", "launching"), 484 testAccCheckAWSAutoScalingGroupInitialLifecycleHookExists( 485 "aws_autoscaling_group.bar", "initial_lifecycle_hook.391359060.name"), 486 ), 487 }, 488 }, 489 }) 490 } 491 492 func TestAccAWSAutoScalingGroup_ALB_TargetGroups_ELBCapacity(t *testing.T) { 493 var group autoscaling.Group 494 var tg elbv2.TargetGroup 495 496 rInt := acctest.RandInt() 497 498 resource.Test(t, resource.TestCase{ 499 PreCheck: func() { testAccPreCheck(t) }, 500 Providers: testAccProviders, 501 CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, 502 Steps: []resource.TestStep{ 503 resource.TestStep{ 504 Config: testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_ELBCapacity(rInt), 505 Check: resource.ComposeAggregateTestCheckFunc( 506 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 507 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &tg), 508 testAccCheckAWSALBTargetGroupHealthy(&tg), 509 ), 510 }, 511 }, 512 }) 513 } 514 515 func testAccCheckAWSAutoScalingGroupDestroy(s *terraform.State) error { 516 conn := testAccProvider.Meta().(*AWSClient).autoscalingconn 517 518 for _, rs := range s.RootModule().Resources { 519 if rs.Type != "aws_autoscaling_group" { 520 continue 521 } 522 523 // Try to find the Group 524 describeGroups, err := conn.DescribeAutoScalingGroups( 525 &autoscaling.DescribeAutoScalingGroupsInput{ 526 AutoScalingGroupNames: []*string{aws.String(rs.Primary.ID)}, 527 }) 528 529 if err == nil { 530 if len(describeGroups.AutoScalingGroups) != 0 && 531 *describeGroups.AutoScalingGroups[0].AutoScalingGroupName == rs.Primary.ID { 532 return fmt.Errorf("AutoScaling Group still exists") 533 } 534 } 535 536 // Verify the error 537 ec2err, ok := err.(awserr.Error) 538 if !ok { 539 return err 540 } 541 if ec2err.Code() != "InvalidGroup.NotFound" { 542 return err 543 } 544 } 545 546 return nil 547 } 548 549 func testAccCheckAWSAutoScalingGroupAttributes(group *autoscaling.Group, name string) resource.TestCheckFunc { 550 return func(s *terraform.State) error { 551 if *group.AvailabilityZones[0] != "us-west-2a" { 552 return fmt.Errorf("Bad availability_zones: %#v", group.AvailabilityZones[0]) 553 } 554 555 if *group.AutoScalingGroupName != name { 556 return fmt.Errorf("Bad Autoscaling Group name, expected (%s), got (%s)", name, *group.AutoScalingGroupName) 557 } 558 559 if *group.MaxSize != 5 { 560 return fmt.Errorf("Bad max_size: %d", *group.MaxSize) 561 } 562 563 if *group.MinSize != 2 { 564 return fmt.Errorf("Bad max_size: %d", *group.MinSize) 565 } 566 567 if *group.HealthCheckType != "ELB" { 568 return fmt.Errorf("Bad health_check_type,\nexpected: %s\ngot: %s", "ELB", *group.HealthCheckType) 569 } 570 571 if *group.HealthCheckGracePeriod != 300 { 572 return fmt.Errorf("Bad health_check_grace_period: %d", *group.HealthCheckGracePeriod) 573 } 574 575 if *group.DesiredCapacity != 4 { 576 return fmt.Errorf("Bad desired_capacity: %d", *group.DesiredCapacity) 577 } 578 579 if *group.LaunchConfigurationName == "" { 580 return fmt.Errorf("Bad launch configuration name: %s", *group.LaunchConfigurationName) 581 } 582 583 t := &autoscaling.TagDescription{ 584 Key: aws.String("Foo"), 585 Value: aws.String("foo-bar"), 586 PropagateAtLaunch: aws.Bool(true), 587 ResourceType: aws.String("auto-scaling-group"), 588 ResourceId: group.AutoScalingGroupName, 589 } 590 591 if !reflect.DeepEqual(group.Tags[0], t) { 592 return fmt.Errorf( 593 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 594 group.Tags[0], 595 t) 596 } 597 598 return nil 599 } 600 } 601 602 func testAccCheckAWSAutoScalingGroupAttributesLoadBalancer(group *autoscaling.Group) resource.TestCheckFunc { 603 return func(s *terraform.State) error { 604 if len(group.LoadBalancerNames) != 1 { 605 return fmt.Errorf("Bad load_balancers: %v", group.LoadBalancerNames) 606 } 607 608 return nil 609 } 610 } 611 612 func testAccCheckAWSAutoScalingGroupExists(n string, group *autoscaling.Group) resource.TestCheckFunc { 613 return func(s *terraform.State) error { 614 rs, ok := s.RootModule().Resources[n] 615 if !ok { 616 return fmt.Errorf("Not found: %s", n) 617 } 618 619 if rs.Primary.ID == "" { 620 return fmt.Errorf("No AutoScaling Group ID is set") 621 } 622 623 conn := testAccProvider.Meta().(*AWSClient).autoscalingconn 624 625 describeGroups, err := conn.DescribeAutoScalingGroups( 626 &autoscaling.DescribeAutoScalingGroupsInput{ 627 AutoScalingGroupNames: []*string{aws.String(rs.Primary.ID)}, 628 }) 629 630 if err != nil { 631 return err 632 } 633 634 if len(describeGroups.AutoScalingGroups) != 1 || 635 *describeGroups.AutoScalingGroups[0].AutoScalingGroupName != rs.Primary.ID { 636 return fmt.Errorf("AutoScaling Group not found") 637 } 638 639 *group = *describeGroups.AutoScalingGroups[0] 640 641 return nil 642 } 643 } 644 645 func testAccCheckAWSAutoScalingGroupInitialLifecycleHookExists(asg, hookAttr string) resource.TestCheckFunc { 646 return func(s *terraform.State) error { 647 asgResource, ok := s.RootModule().Resources[asg] 648 if !ok { 649 return fmt.Errorf("Not found: %s", asg) 650 } 651 652 if asgResource.Primary.ID == "" { 653 return fmt.Errorf("No AutoScaling Group ID is set") 654 } 655 656 hookName := asgResource.Primary.Attributes[hookAttr] 657 if hookName == "" { 658 return fmt.Errorf("ASG %s has no hook name %s", asg, hookAttr) 659 } 660 661 return checkLifecycleHookExistsByName(asgResource.Primary.ID, hookName) 662 } 663 } 664 665 func testLaunchConfigurationName(n string, lc *autoscaling.LaunchConfiguration) resource.TestCheckFunc { 666 return func(s *terraform.State) error { 667 rs, ok := s.RootModule().Resources[n] 668 if !ok { 669 return fmt.Errorf("Not found: %s", n) 670 } 671 672 if *lc.LaunchConfigurationName != rs.Primary.Attributes["launch_configuration"] { 673 return fmt.Errorf("Launch configuration names do not match") 674 } 675 676 return nil 677 } 678 } 679 680 func testAccCheckAWSAutoScalingGroupHealthyCapacity( 681 g *autoscaling.Group, exp int) resource.TestCheckFunc { 682 return func(s *terraform.State) error { 683 healthy := 0 684 for _, i := range g.Instances { 685 if i.HealthStatus == nil { 686 continue 687 } 688 if strings.EqualFold(*i.HealthStatus, "Healthy") { 689 healthy++ 690 } 691 } 692 if healthy < exp { 693 return fmt.Errorf("Expected at least %d healthy, got %d.", exp, healthy) 694 } 695 return nil 696 } 697 } 698 699 func testAccCheckAWSAutoScalingGroupAttributesVPCZoneIdentifer(group *autoscaling.Group) resource.TestCheckFunc { 700 return func(s *terraform.State) error { 701 // Grab Subnet Ids 702 var subnets []string 703 for _, rs := range s.RootModule().Resources { 704 if rs.Type != "aws_subnet" { 705 continue 706 } 707 subnets = append(subnets, rs.Primary.Attributes["id"]) 708 } 709 710 if group.VPCZoneIdentifier == nil { 711 return fmt.Errorf("Bad VPC Zone Identifier\nexpected: %s\ngot nil", subnets) 712 } 713 714 zones := strings.Split(*group.VPCZoneIdentifier, ",") 715 716 remaining := len(zones) 717 for _, z := range zones { 718 for _, s := range subnets { 719 if z == s { 720 remaining-- 721 } 722 } 723 } 724 725 if remaining != 0 { 726 return fmt.Errorf("Bad VPC Zone Identifier match\nexpected: %s\ngot:%s", zones, subnets) 727 } 728 729 return nil 730 } 731 } 732 733 // testAccCheckAWSALBTargetGroupHealthy checks an *elbv2.TargetGroup to make 734 // sure that all instances in it are healthy. 735 func testAccCheckAWSALBTargetGroupHealthy(res *elbv2.TargetGroup) resource.TestCheckFunc { 736 return func(s *terraform.State) error { 737 conn := testAccProvider.Meta().(*AWSClient).elbv2conn 738 739 resp, err := conn.DescribeTargetHealth(&elbv2.DescribeTargetHealthInput{ 740 TargetGroupArn: res.TargetGroupArn, 741 }) 742 743 if err != nil { 744 return err 745 } 746 747 for _, target := range resp.TargetHealthDescriptions { 748 if target.TargetHealth == nil || target.TargetHealth.State == nil || *target.TargetHealth.State != "healthy" { 749 return errors.New("Not all instances in target group are healthy yet, but should be") 750 } 751 } 752 753 return nil 754 } 755 } 756 757 const testAccAWSAutoScalingGroupConfig_autoGeneratedName = ` 758 resource "aws_launch_configuration" "foobar" { 759 image_id = "ami-21f78e11" 760 instance_type = "t1.micro" 761 } 762 763 resource "aws_autoscaling_group" "bar" { 764 availability_zones = ["us-west-2a"] 765 desired_capacity = 0 766 max_size = 0 767 min_size = 0 768 launch_configuration = "${aws_launch_configuration.foobar.name}" 769 } 770 ` 771 772 const testAccAWSAutoScalingGroupConfig_namePrefix = ` 773 resource "aws_launch_configuration" "test" { 774 image_id = "ami-21f78e11" 775 instance_type = "t1.micro" 776 } 777 778 resource "aws_autoscaling_group" "test" { 779 availability_zones = ["us-west-2a"] 780 desired_capacity = 0 781 max_size = 0 782 min_size = 0 783 name_prefix = "test-" 784 launch_configuration = "${aws_launch_configuration.test.name}" 785 } 786 ` 787 788 const testAccAWSAutoScalingGroupConfig_terminationPoliciesEmpty = ` 789 resource "aws_launch_configuration" "foobar" { 790 image_id = "ami-21f78e11" 791 instance_type = "t1.micro" 792 } 793 794 resource "aws_autoscaling_group" "bar" { 795 availability_zones = ["us-west-2a"] 796 max_size = 0 797 min_size = 0 798 desired_capacity = 0 799 800 launch_configuration = "${aws_launch_configuration.foobar.name}" 801 } 802 ` 803 804 const testAccAWSAutoScalingGroupConfig_terminationPoliciesExplicitDefault = ` 805 resource "aws_launch_configuration" "foobar" { 806 image_id = "ami-21f78e11" 807 instance_type = "t1.micro" 808 } 809 810 resource "aws_autoscaling_group" "bar" { 811 availability_zones = ["us-west-2a"] 812 max_size = 0 813 min_size = 0 814 desired_capacity = 0 815 termination_policies = ["Default"] 816 817 launch_configuration = "${aws_launch_configuration.foobar.name}" 818 } 819 ` 820 821 const testAccAWSAutoScalingGroupConfig_terminationPoliciesUpdate = ` 822 resource "aws_launch_configuration" "foobar" { 823 image_id = "ami-21f78e11" 824 instance_type = "t1.micro" 825 } 826 827 resource "aws_autoscaling_group" "bar" { 828 availability_zones = ["us-west-2a"] 829 max_size = 0 830 min_size = 0 831 desired_capacity = 0 832 termination_policies = ["OldestInstance"] 833 834 launch_configuration = "${aws_launch_configuration.foobar.name}" 835 } 836 ` 837 838 func testAccAWSAutoScalingGroupConfig(name string) string { 839 return fmt.Sprintf(` 840 resource "aws_launch_configuration" "foobar" { 841 image_id = "ami-21f78e11" 842 instance_type = "t1.micro" 843 } 844 845 resource "aws_placement_group" "test" { 846 name = "asg_pg_%s" 847 strategy = "cluster" 848 } 849 850 resource "aws_autoscaling_group" "bar" { 851 availability_zones = ["us-west-2a"] 852 name = "%s" 853 max_size = 5 854 min_size = 2 855 health_check_type = "ELB" 856 desired_capacity = 4 857 force_delete = true 858 termination_policies = ["OldestInstance","ClosestToNextInstanceHour"] 859 860 launch_configuration = "${aws_launch_configuration.foobar.name}" 861 862 tag { 863 key = "Foo" 864 value = "foo-bar" 865 propagate_at_launch = true 866 } 867 } 868 `, name, name) 869 } 870 871 func testAccAWSAutoScalingGroupConfigUpdate(name string) string { 872 return fmt.Sprintf(` 873 resource "aws_launch_configuration" "foobar" { 874 image_id = "ami-21f78e11" 875 instance_type = "t1.micro" 876 } 877 878 resource "aws_launch_configuration" "new" { 879 image_id = "ami-21f78e11" 880 instance_type = "t1.micro" 881 } 882 883 resource "aws_autoscaling_group" "bar" { 884 availability_zones = ["us-west-2a"] 885 name = "%s" 886 max_size = 5 887 min_size = 2 888 health_check_grace_period = 300 889 health_check_type = "ELB" 890 desired_capacity = 5 891 force_delete = true 892 termination_policies = ["ClosestToNextInstanceHour"] 893 protect_from_scale_in = true 894 895 launch_configuration = "${aws_launch_configuration.new.name}" 896 897 tag { 898 key = "Bar" 899 value = "bar-foo" 900 propagate_at_launch = true 901 } 902 } 903 `, name) 904 } 905 906 const testAccAWSAutoScalingGroupConfigWithLoadBalancer = ` 907 resource "aws_vpc" "foo" { 908 cidr_block = "10.1.0.0/16" 909 tags { Name = "tf-asg-test" } 910 } 911 912 resource "aws_internet_gateway" "gw" { 913 vpc_id = "${aws_vpc.foo.id}" 914 } 915 916 resource "aws_subnet" "foo" { 917 cidr_block = "10.1.1.0/24" 918 vpc_id = "${aws_vpc.foo.id}" 919 } 920 921 resource "aws_security_group" "foo" { 922 vpc_id="${aws_vpc.foo.id}" 923 924 ingress { 925 protocol = "-1" 926 from_port = 0 927 to_port = 0 928 cidr_blocks = ["0.0.0.0/0"] 929 } 930 931 egress { 932 protocol = "-1" 933 from_port = 0 934 to_port = 0 935 cidr_blocks = ["0.0.0.0/0"] 936 } 937 } 938 939 resource "aws_elb" "bar" { 940 subnets = ["${aws_subnet.foo.id}"] 941 security_groups = ["${aws_security_group.foo.id}"] 942 943 listener { 944 instance_port = 80 945 instance_protocol = "http" 946 lb_port = 80 947 lb_protocol = "http" 948 } 949 950 health_check { 951 healthy_threshold = 2 952 unhealthy_threshold = 2 953 target = "HTTP:80/" 954 interval = 5 955 timeout = 2 956 } 957 958 depends_on = ["aws_internet_gateway.gw"] 959 } 960 961 resource "aws_launch_configuration" "foobar" { 962 // need an AMI that listens on :80 at boot, this is: 963 // bitnami-nginxstack-1.6.1-0-linux-ubuntu-14.04.1-x86_64-hvm-ebs-ami-99f5b1a9-3 964 image_id = "ami-b5b3fc85" 965 instance_type = "t2.micro" 966 security_groups = ["${aws_security_group.foo.id}"] 967 } 968 969 resource "aws_autoscaling_group" "bar" { 970 availability_zones = ["${aws_subnet.foo.availability_zone}"] 971 vpc_zone_identifier = ["${aws_subnet.foo.id}"] 972 max_size = 2 973 min_size = 2 974 health_check_grace_period = 300 975 health_check_type = "ELB" 976 wait_for_elb_capacity = 2 977 force_delete = true 978 979 launch_configuration = "${aws_launch_configuration.foobar.name}" 980 load_balancers = ["${aws_elb.bar.name}"] 981 } 982 ` 983 984 const testAccAWSAutoScalingGroupConfigWithAZ = ` 985 resource "aws_vpc" "default" { 986 cidr_block = "10.0.0.0/16" 987 tags { 988 Name = "terraform-test" 989 } 990 } 991 992 resource "aws_subnet" "main" { 993 vpc_id = "${aws_vpc.default.id}" 994 cidr_block = "10.0.1.0/24" 995 availability_zone = "us-west-2a" 996 tags { 997 Name = "terraform-test" 998 } 999 } 1000 1001 resource "aws_launch_configuration" "foobar" { 1002 image_id = "ami-b5b3fc85" 1003 instance_type = "t2.micro" 1004 } 1005 1006 resource "aws_autoscaling_group" "bar" { 1007 availability_zones = [ 1008 "us-west-2a" 1009 ] 1010 desired_capacity = 0 1011 max_size = 0 1012 min_size = 0 1013 launch_configuration = "${aws_launch_configuration.foobar.name}" 1014 } 1015 ` 1016 1017 const testAccAWSAutoScalingGroupConfigWithVPCIdent = ` 1018 resource "aws_vpc" "default" { 1019 cidr_block = "10.0.0.0/16" 1020 tags { 1021 Name = "terraform-test" 1022 } 1023 } 1024 1025 resource "aws_subnet" "main" { 1026 vpc_id = "${aws_vpc.default.id}" 1027 cidr_block = "10.0.1.0/24" 1028 availability_zone = "us-west-2a" 1029 tags { 1030 Name = "terraform-test" 1031 } 1032 } 1033 1034 resource "aws_launch_configuration" "foobar" { 1035 image_id = "ami-b5b3fc85" 1036 instance_type = "t2.micro" 1037 } 1038 1039 resource "aws_autoscaling_group" "bar" { 1040 vpc_zone_identifier = [ 1041 "${aws_subnet.main.id}", 1042 ] 1043 desired_capacity = 0 1044 max_size = 0 1045 min_size = 0 1046 launch_configuration = "${aws_launch_configuration.foobar.name}" 1047 } 1048 ` 1049 1050 func testAccAWSAutoScalingGroupConfig_withPlacementGroup(name string) string { 1051 return fmt.Sprintf(` 1052 resource "aws_launch_configuration" "foobar" { 1053 image_id = "ami-21f78e11" 1054 instance_type = "c3.large" 1055 } 1056 1057 resource "aws_placement_group" "test" { 1058 name = "%s" 1059 strategy = "cluster" 1060 } 1061 1062 resource "aws_autoscaling_group" "bar" { 1063 availability_zones = ["us-west-2a"] 1064 name = "%s" 1065 max_size = 1 1066 min_size = 1 1067 health_check_grace_period = 300 1068 health_check_type = "ELB" 1069 desired_capacity = 1 1070 force_delete = true 1071 termination_policies = ["OldestInstance","ClosestToNextInstanceHour"] 1072 placement_group = "${aws_placement_group.test.name}" 1073 1074 launch_configuration = "${aws_launch_configuration.foobar.name}" 1075 1076 tag { 1077 key = "Foo" 1078 value = "foo-bar" 1079 propagate_at_launch = true 1080 } 1081 } 1082 `, name, name) 1083 } 1084 1085 const testAccAWSAutoscalingMetricsCollectionConfig_allMetricsCollected = ` 1086 resource "aws_launch_configuration" "foobar" { 1087 image_id = "ami-21f78e11" 1088 instance_type = "t1.micro" 1089 } 1090 1091 resource "aws_autoscaling_group" "bar" { 1092 availability_zones = ["us-west-2a"] 1093 max_size = 1 1094 min_size = 0 1095 health_check_grace_period = 300 1096 health_check_type = "EC2" 1097 desired_capacity = 0 1098 force_delete = true 1099 termination_policies = ["OldestInstance","ClosestToNextInstanceHour"] 1100 launch_configuration = "${aws_launch_configuration.foobar.name}" 1101 enabled_metrics = ["GroupTotalInstances", 1102 "GroupPendingInstances", 1103 "GroupTerminatingInstances", 1104 "GroupDesiredCapacity", 1105 "GroupInServiceInstances", 1106 "GroupMinSize", 1107 "GroupMaxSize" 1108 ] 1109 metrics_granularity = "1Minute" 1110 } 1111 ` 1112 1113 const testAccAWSAutoscalingMetricsCollectionConfig_updatingMetricsCollected = ` 1114 resource "aws_launch_configuration" "foobar" { 1115 image_id = "ami-21f78e11" 1116 instance_type = "t1.micro" 1117 } 1118 1119 resource "aws_autoscaling_group" "bar" { 1120 availability_zones = ["us-west-2a"] 1121 max_size = 1 1122 min_size = 0 1123 health_check_grace_period = 300 1124 health_check_type = "EC2" 1125 desired_capacity = 0 1126 force_delete = true 1127 termination_policies = ["OldestInstance","ClosestToNextInstanceHour"] 1128 launch_configuration = "${aws_launch_configuration.foobar.name}" 1129 enabled_metrics = ["GroupTotalInstances", 1130 "GroupPendingInstances", 1131 "GroupTerminatingInstances", 1132 "GroupDesiredCapacity", 1133 "GroupMaxSize" 1134 ] 1135 metrics_granularity = "1Minute" 1136 } 1137 ` 1138 1139 const testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_pre = ` 1140 provider "aws" { 1141 region = "us-west-2" 1142 } 1143 1144 resource "aws_vpc" "default" { 1145 cidr_block = "10.0.0.0/16" 1146 1147 tags { 1148 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1149 } 1150 } 1151 1152 resource "aws_alb_target_group" "test" { 1153 name = "tf-example-alb-tg" 1154 port = 80 1155 protocol = "HTTP" 1156 vpc_id = "${aws_vpc.default.id}" 1157 } 1158 1159 resource "aws_subnet" "main" { 1160 vpc_id = "${aws_vpc.default.id}" 1161 cidr_block = "10.0.1.0/24" 1162 availability_zone = "us-west-2a" 1163 1164 tags { 1165 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1166 } 1167 } 1168 1169 resource "aws_subnet" "alt" { 1170 vpc_id = "${aws_vpc.default.id}" 1171 cidr_block = "10.0.2.0/24" 1172 availability_zone = "us-west-2b" 1173 1174 tags { 1175 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1176 } 1177 } 1178 1179 resource "aws_launch_configuration" "foobar" { 1180 # Golang-base from cts-hashi aws account, shared with tf testing account 1181 image_id = "ami-1817d178" 1182 instance_type = "t2.micro" 1183 enable_monitoring = false 1184 } 1185 1186 resource "aws_autoscaling_group" "bar" { 1187 vpc_zone_identifier = [ 1188 "${aws_subnet.main.id}", 1189 "${aws_subnet.alt.id}", 1190 ] 1191 1192 max_size = 2 1193 min_size = 0 1194 health_check_grace_period = 300 1195 health_check_type = "ELB" 1196 desired_capacity = 0 1197 force_delete = true 1198 termination_policies = ["OldestInstance"] 1199 launch_configuration = "${aws_launch_configuration.foobar.name}" 1200 1201 } 1202 1203 resource "aws_security_group" "tf_test_self" { 1204 name = "tf_test_alb_asg" 1205 description = "tf_test_alb_asg" 1206 vpc_id = "${aws_vpc.default.id}" 1207 1208 ingress { 1209 from_port = 80 1210 to_port = 80 1211 protocol = "tcp" 1212 cidr_blocks = ["0.0.0.0/0"] 1213 } 1214 1215 tags { 1216 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1217 } 1218 } 1219 ` 1220 1221 const testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_post = ` 1222 provider "aws" { 1223 region = "us-west-2" 1224 } 1225 1226 resource "aws_vpc" "default" { 1227 cidr_block = "10.0.0.0/16" 1228 1229 tags { 1230 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1231 } 1232 } 1233 1234 resource "aws_alb_target_group" "test" { 1235 name = "tf-example-alb-tg" 1236 port = 80 1237 protocol = "HTTP" 1238 vpc_id = "${aws_vpc.default.id}" 1239 } 1240 1241 resource "aws_subnet" "main" { 1242 vpc_id = "${aws_vpc.default.id}" 1243 cidr_block = "10.0.1.0/24" 1244 availability_zone = "us-west-2a" 1245 1246 tags { 1247 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1248 } 1249 } 1250 1251 resource "aws_subnet" "alt" { 1252 vpc_id = "${aws_vpc.default.id}" 1253 cidr_block = "10.0.2.0/24" 1254 availability_zone = "us-west-2b" 1255 1256 tags { 1257 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1258 } 1259 } 1260 1261 resource "aws_launch_configuration" "foobar" { 1262 # Golang-base from cts-hashi aws account, shared with tf testing account 1263 image_id = "ami-1817d178" 1264 instance_type = "t2.micro" 1265 enable_monitoring = false 1266 } 1267 1268 resource "aws_autoscaling_group" "bar" { 1269 vpc_zone_identifier = [ 1270 "${aws_subnet.main.id}", 1271 "${aws_subnet.alt.id}", 1272 ] 1273 1274 target_group_arns = ["${aws_alb_target_group.test.arn}"] 1275 1276 max_size = 2 1277 min_size = 0 1278 health_check_grace_period = 300 1279 health_check_type = "ELB" 1280 desired_capacity = 0 1281 force_delete = true 1282 termination_policies = ["OldestInstance"] 1283 launch_configuration = "${aws_launch_configuration.foobar.name}" 1284 1285 } 1286 1287 resource "aws_security_group" "tf_test_self" { 1288 name = "tf_test_alb_asg" 1289 description = "tf_test_alb_asg" 1290 vpc_id = "${aws_vpc.default.id}" 1291 1292 ingress { 1293 from_port = 80 1294 to_port = 80 1295 protocol = "tcp" 1296 cidr_blocks = ["0.0.0.0/0"] 1297 } 1298 1299 tags { 1300 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1301 } 1302 } 1303 ` 1304 1305 const testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_post_duo = ` 1306 provider "aws" { 1307 region = "us-west-2" 1308 } 1309 1310 resource "aws_vpc" "default" { 1311 cidr_block = "10.0.0.0/16" 1312 1313 tags { 1314 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1315 } 1316 } 1317 1318 resource "aws_alb_target_group" "test" { 1319 name = "tf-example-alb-tg" 1320 port = 80 1321 protocol = "HTTP" 1322 vpc_id = "${aws_vpc.default.id}" 1323 } 1324 1325 resource "aws_alb_target_group" "test_more" { 1326 name = "tf-example-alb-tg-more" 1327 port = 80 1328 protocol = "HTTP" 1329 vpc_id = "${aws_vpc.default.id}" 1330 } 1331 1332 resource "aws_subnet" "main" { 1333 vpc_id = "${aws_vpc.default.id}" 1334 cidr_block = "10.0.1.0/24" 1335 availability_zone = "us-west-2a" 1336 1337 tags { 1338 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1339 } 1340 } 1341 1342 resource "aws_subnet" "alt" { 1343 vpc_id = "${aws_vpc.default.id}" 1344 cidr_block = "10.0.2.0/24" 1345 availability_zone = "us-west-2b" 1346 1347 tags { 1348 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1349 } 1350 } 1351 1352 resource "aws_launch_configuration" "foobar" { 1353 # Golang-base from cts-hashi aws account, shared with tf testing account 1354 image_id = "ami-1817d178" 1355 instance_type = "t2.micro" 1356 enable_monitoring = false 1357 } 1358 1359 resource "aws_autoscaling_group" "bar" { 1360 vpc_zone_identifier = [ 1361 "${aws_subnet.main.id}", 1362 "${aws_subnet.alt.id}", 1363 ] 1364 1365 target_group_arns = [ 1366 "${aws_alb_target_group.test.arn}", 1367 "${aws_alb_target_group.test_more.arn}", 1368 ] 1369 1370 max_size = 2 1371 min_size = 0 1372 health_check_grace_period = 300 1373 health_check_type = "ELB" 1374 desired_capacity = 0 1375 force_delete = true 1376 termination_policies = ["OldestInstance"] 1377 launch_configuration = "${aws_launch_configuration.foobar.name}" 1378 1379 } 1380 1381 resource "aws_security_group" "tf_test_self" { 1382 name = "tf_test_alb_asg" 1383 description = "tf_test_alb_asg" 1384 vpc_id = "${aws_vpc.default.id}" 1385 1386 ingress { 1387 from_port = 80 1388 to_port = 80 1389 protocol = "tcp" 1390 cidr_blocks = ["0.0.0.0/0"] 1391 } 1392 1393 tags { 1394 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1395 } 1396 } 1397 ` 1398 1399 func testAccAWSAutoScalingGroupWithHookConfig(name string) string { 1400 return fmt.Sprintf(` 1401 resource "aws_launch_configuration" "foobar" { 1402 image_id = "ami-21f78e11" 1403 instance_type = "t1.micro" 1404 } 1405 1406 resource "aws_autoscaling_group" "bar" { 1407 availability_zones = ["us-west-2a"] 1408 name = "%s" 1409 max_size = 5 1410 min_size = 2 1411 health_check_type = "ELB" 1412 desired_capacity = 4 1413 force_delete = true 1414 termination_policies = ["OldestInstance","ClosestToNextInstanceHour"] 1415 1416 launch_configuration = "${aws_launch_configuration.foobar.name}" 1417 1418 initial_lifecycle_hook { 1419 name = "launching" 1420 default_result = "CONTINUE" 1421 heartbeat_timeout = 30 # minimum value 1422 lifecycle_transition = "autoscaling:EC2_INSTANCE_LAUNCHING" 1423 } 1424 } 1425 `, name) 1426 } 1427 1428 func testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_ELBCapacity(rInt int) string { 1429 return fmt.Sprintf(` 1430 provider "aws" { 1431 region = "us-west-2" 1432 } 1433 1434 resource "aws_vpc" "default" { 1435 cidr_block = "10.0.0.0/16" 1436 enable_dns_hostnames = "true" 1437 enable_dns_support = "true" 1438 1439 tags { 1440 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_ELBCapacity" 1441 } 1442 } 1443 1444 resource "aws_alb" "test_lb" { 1445 subnets = ["${aws_subnet.main.id}", "${aws_subnet.alt.id}"] 1446 1447 tags { 1448 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_ELBCapacity" 1449 } 1450 } 1451 1452 resource "aws_alb_listener" "test_listener" { 1453 load_balancer_arn = "${aws_alb.test_lb.arn}" 1454 port = "80" 1455 1456 default_action { 1457 target_group_arn = "${aws_alb_target_group.test.arn}" 1458 type = "forward" 1459 } 1460 } 1461 1462 resource "aws_alb_target_group" "test" { 1463 name = "tf-alb-test-%d" 1464 port = 80 1465 protocol = "HTTP" 1466 vpc_id = "${aws_vpc.default.id}" 1467 1468 health_check { 1469 path = "/" 1470 healthy_threshold = "2" 1471 timeout = "2" 1472 interval = "5" 1473 } 1474 1475 tags { 1476 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_ELBCapacity" 1477 } 1478 } 1479 1480 resource "aws_subnet" "main" { 1481 vpc_id = "${aws_vpc.default.id}" 1482 cidr_block = "10.0.1.0/24" 1483 availability_zone = "us-west-2a" 1484 1485 tags { 1486 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_ELBCapacity" 1487 } 1488 } 1489 1490 resource "aws_subnet" "alt" { 1491 vpc_id = "${aws_vpc.default.id}" 1492 cidr_block = "10.0.2.0/24" 1493 availability_zone = "us-west-2b" 1494 1495 tags { 1496 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_ELBCapacity" 1497 } 1498 } 1499 1500 resource "aws_internet_gateway" "internet_gateway" { 1501 vpc_id = "${aws_vpc.default.id}" 1502 } 1503 1504 resource "aws_route_table" "route_table" { 1505 vpc_id = "${aws_vpc.default.id}" 1506 } 1507 1508 resource "aws_route_table_association" "route_table_association_main" { 1509 subnet_id = "${aws_subnet.main.id}" 1510 route_table_id = "${aws_route_table.route_table.id}" 1511 } 1512 1513 resource "aws_route_table_association" "route_table_association_alt" { 1514 subnet_id = "${aws_subnet.alt.id}" 1515 route_table_id = "${aws_route_table.route_table.id}" 1516 } 1517 1518 resource "aws_route" "public_default_route" { 1519 route_table_id = "${aws_route_table.route_table.id}" 1520 destination_cidr_block = "0.0.0.0/0" 1521 gateway_id = "${aws_internet_gateway.internet_gateway.id}" 1522 } 1523 1524 data "aws_ami" "test_ami" { 1525 most_recent = true 1526 1527 filter { 1528 name = "owner-alias" 1529 values = ["amazon"] 1530 } 1531 1532 filter { 1533 name = "name" 1534 values = ["amzn-ami-hvm-*-x86_64-gp2"] 1535 } 1536 } 1537 1538 resource "aws_launch_configuration" "foobar" { 1539 image_id = "${data.aws_ami.test_ami.id}" 1540 instance_type = "t2.micro" 1541 associate_public_ip_address = "true" 1542 1543 user_data = <<EOS 1544 #!/bin/bash 1545 yum -y install httpd 1546 echo "hello world" > /var/www/html/index.html 1547 chkconfig httpd on 1548 service httpd start 1549 EOS 1550 } 1551 1552 resource "aws_autoscaling_group" "bar" { 1553 vpc_zone_identifier = [ 1554 "${aws_subnet.main.id}", 1555 "${aws_subnet.alt.id}", 1556 ] 1557 1558 target_group_arns = ["${aws_alb_target_group.test.arn}"] 1559 1560 max_size = 2 1561 min_size = 2 1562 health_check_grace_period = 300 1563 health_check_type = "ELB" 1564 desired_capacity = 2 1565 wait_for_elb_capacity = 2 1566 force_delete = true 1567 termination_policies = ["OldestInstance"] 1568 launch_configuration = "${aws_launch_configuration.foobar.name}" 1569 }`, rInt) 1570 } 1571 1572 func testAccAWSAutoScalingGroupConfigWithSuspendedProcesses(name string) string { 1573 return fmt.Sprintf(` 1574 resource "aws_launch_configuration" "foobar" { 1575 image_id = "ami-21f78e11" 1576 instance_type = "t1.micro" 1577 } 1578 1579 resource "aws_placement_group" "test" { 1580 name = "asg_pg_%s" 1581 strategy = "cluster" 1582 } 1583 1584 resource "aws_autoscaling_group" "bar" { 1585 availability_zones = ["us-west-2a"] 1586 name = "%s" 1587 max_size = 5 1588 min_size = 2 1589 health_check_type = "ELB" 1590 desired_capacity = 4 1591 force_delete = true 1592 termination_policies = ["OldestInstance","ClosestToNextInstanceHour"] 1593 1594 launch_configuration = "${aws_launch_configuration.foobar.name}" 1595 1596 suspended_processes = ["AlarmNotification","ScheduledActions"] 1597 1598 tag { 1599 key = "Foo" 1600 value = "foo-bar" 1601 propagate_at_launch = true 1602 } 1603 } 1604 `, name, name) 1605 } 1606 1607 func testAccAWSAutoScalingGroupConfigWithSuspendedProcessesUpdated(name string) string { 1608 return fmt.Sprintf(` 1609 resource "aws_launch_configuration" "foobar" { 1610 image_id = "ami-21f78e11" 1611 instance_type = "t1.micro" 1612 } 1613 1614 resource "aws_placement_group" "test" { 1615 name = "asg_pg_%s" 1616 strategy = "cluster" 1617 } 1618 1619 resource "aws_autoscaling_group" "bar" { 1620 availability_zones = ["us-west-2a"] 1621 name = "%s" 1622 max_size = 5 1623 min_size = 2 1624 health_check_type = "ELB" 1625 desired_capacity = 4 1626 force_delete = true 1627 termination_policies = ["OldestInstance","ClosestToNextInstanceHour"] 1628 1629 launch_configuration = "${aws_launch_configuration.foobar.name}" 1630 1631 suspended_processes = ["AZRebalance","ScheduledActions"] 1632 1633 tag { 1634 key = "Foo" 1635 value = "foo-bar" 1636 propagate_at_launch = true 1637 } 1638 } 1639 `, name, name) 1640 }