github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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 }) 450 } 451 452 func TestAccAWSAutoScalingGroup_initialLifecycleHook(t *testing.T) { 453 var group autoscaling.Group 454 455 randName := fmt.Sprintf("terraform-test-%s", acctest.RandString(10)) 456 457 resource.Test(t, resource.TestCase{ 458 PreCheck: func() { testAccPreCheck(t) }, 459 IDRefreshName: "aws_autoscaling_group.bar", 460 IDRefreshIgnore: []string{"force_delete", "metrics_granularity", "wait_for_capacity_timeout"}, 461 Providers: testAccProviders, 462 CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, 463 Steps: []resource.TestStep{ 464 resource.TestStep{ 465 Config: testAccAWSAutoScalingGroupWithHookConfig(randName), 466 Check: resource.ComposeTestCheckFunc( 467 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 468 testAccCheckAWSAutoScalingGroupHealthyCapacity(&group, 2), 469 resource.TestCheckResourceAttr( 470 "aws_autoscaling_group.bar", "initial_lifecycle_hook.#", "1"), 471 resource.TestCheckResourceAttr( 472 "aws_autoscaling_group.bar", "initial_lifecycle_hook.391359060.default_result", "CONTINUE"), 473 resource.TestCheckResourceAttr( 474 "aws_autoscaling_group.bar", "initial_lifecycle_hook.391359060.name", "launching"), 475 testAccCheckAWSAutoScalingGroupInitialLifecycleHookExists( 476 "aws_autoscaling_group.bar", "initial_lifecycle_hook.391359060.name"), 477 ), 478 }, 479 }, 480 }) 481 } 482 483 func TestAccAWSAutoScalingGroup_ALB_TargetGroups_ELBCapacity(t *testing.T) { 484 var group autoscaling.Group 485 var tg elbv2.TargetGroup 486 487 rInt := acctest.RandInt() 488 489 resource.Test(t, resource.TestCase{ 490 PreCheck: func() { testAccPreCheck(t) }, 491 Providers: testAccProviders, 492 CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, 493 Steps: []resource.TestStep{ 494 resource.TestStep{ 495 Config: testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_ELBCapacity(rInt), 496 Check: resource.ComposeAggregateTestCheckFunc( 497 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 498 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &tg), 499 testAccCheckAWSALBTargetGroupHealthy(&tg), 500 ), 501 }, 502 }, 503 }) 504 } 505 506 func testAccCheckAWSAutoScalingGroupDestroy(s *terraform.State) error { 507 conn := testAccProvider.Meta().(*AWSClient).autoscalingconn 508 509 for _, rs := range s.RootModule().Resources { 510 if rs.Type != "aws_autoscaling_group" { 511 continue 512 } 513 514 // Try to find the Group 515 describeGroups, err := conn.DescribeAutoScalingGroups( 516 &autoscaling.DescribeAutoScalingGroupsInput{ 517 AutoScalingGroupNames: []*string{aws.String(rs.Primary.ID)}, 518 }) 519 520 if err == nil { 521 if len(describeGroups.AutoScalingGroups) != 0 && 522 *describeGroups.AutoScalingGroups[0].AutoScalingGroupName == rs.Primary.ID { 523 return fmt.Errorf("AutoScaling Group still exists") 524 } 525 } 526 527 // Verify the error 528 ec2err, ok := err.(awserr.Error) 529 if !ok { 530 return err 531 } 532 if ec2err.Code() != "InvalidGroup.NotFound" { 533 return err 534 } 535 } 536 537 return nil 538 } 539 540 func testAccCheckAWSAutoScalingGroupAttributes(group *autoscaling.Group, name string) resource.TestCheckFunc { 541 return func(s *terraform.State) error { 542 if *group.AvailabilityZones[0] != "us-west-2a" { 543 return fmt.Errorf("Bad availability_zones: %#v", group.AvailabilityZones[0]) 544 } 545 546 if *group.AutoScalingGroupName != name { 547 return fmt.Errorf("Bad Autoscaling Group name, expected (%s), got (%s)", name, *group.AutoScalingGroupName) 548 } 549 550 if *group.MaxSize != 5 { 551 return fmt.Errorf("Bad max_size: %d", *group.MaxSize) 552 } 553 554 if *group.MinSize != 2 { 555 return fmt.Errorf("Bad max_size: %d", *group.MinSize) 556 } 557 558 if *group.HealthCheckType != "ELB" { 559 return fmt.Errorf("Bad health_check_type,\nexpected: %s\ngot: %s", "ELB", *group.HealthCheckType) 560 } 561 562 if *group.HealthCheckGracePeriod != 300 { 563 return fmt.Errorf("Bad health_check_grace_period: %d", *group.HealthCheckGracePeriod) 564 } 565 566 if *group.DesiredCapacity != 4 { 567 return fmt.Errorf("Bad desired_capacity: %d", *group.DesiredCapacity) 568 } 569 570 if *group.LaunchConfigurationName == "" { 571 return fmt.Errorf("Bad launch configuration name: %s", *group.LaunchConfigurationName) 572 } 573 574 t := &autoscaling.TagDescription{ 575 Key: aws.String("Foo"), 576 Value: aws.String("foo-bar"), 577 PropagateAtLaunch: aws.Bool(true), 578 ResourceType: aws.String("auto-scaling-group"), 579 ResourceId: group.AutoScalingGroupName, 580 } 581 582 if !reflect.DeepEqual(group.Tags[0], t) { 583 return fmt.Errorf( 584 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 585 group.Tags[0], 586 t) 587 } 588 589 return nil 590 } 591 } 592 593 func testAccCheckAWSAutoScalingGroupAttributesLoadBalancer(group *autoscaling.Group) resource.TestCheckFunc { 594 return func(s *terraform.State) error { 595 if len(group.LoadBalancerNames) != 1 { 596 return fmt.Errorf("Bad load_balancers: %v", group.LoadBalancerNames) 597 } 598 599 return nil 600 } 601 } 602 603 func testAccCheckAWSAutoScalingGroupExists(n string, group *autoscaling.Group) resource.TestCheckFunc { 604 return func(s *terraform.State) error { 605 rs, ok := s.RootModule().Resources[n] 606 if !ok { 607 return fmt.Errorf("Not found: %s", n) 608 } 609 610 if rs.Primary.ID == "" { 611 return fmt.Errorf("No AutoScaling Group ID is set") 612 } 613 614 conn := testAccProvider.Meta().(*AWSClient).autoscalingconn 615 616 describeGroups, err := conn.DescribeAutoScalingGroups( 617 &autoscaling.DescribeAutoScalingGroupsInput{ 618 AutoScalingGroupNames: []*string{aws.String(rs.Primary.ID)}, 619 }) 620 621 if err != nil { 622 return err 623 } 624 625 if len(describeGroups.AutoScalingGroups) != 1 || 626 *describeGroups.AutoScalingGroups[0].AutoScalingGroupName != rs.Primary.ID { 627 return fmt.Errorf("AutoScaling Group not found") 628 } 629 630 *group = *describeGroups.AutoScalingGroups[0] 631 632 return nil 633 } 634 } 635 636 func testAccCheckAWSAutoScalingGroupInitialLifecycleHookExists(asg, hookAttr string) resource.TestCheckFunc { 637 return func(s *terraform.State) error { 638 asgResource, ok := s.RootModule().Resources[asg] 639 if !ok { 640 return fmt.Errorf("Not found: %s", asg) 641 } 642 643 if asgResource.Primary.ID == "" { 644 return fmt.Errorf("No AutoScaling Group ID is set") 645 } 646 647 hookName := asgResource.Primary.Attributes[hookAttr] 648 if hookName == "" { 649 return fmt.Errorf("ASG %s has no hook name %s", asg, hookAttr) 650 } 651 652 return checkLifecycleHookExistsByName(asgResource.Primary.ID, hookName) 653 } 654 } 655 656 func testLaunchConfigurationName(n string, lc *autoscaling.LaunchConfiguration) resource.TestCheckFunc { 657 return func(s *terraform.State) error { 658 rs, ok := s.RootModule().Resources[n] 659 if !ok { 660 return fmt.Errorf("Not found: %s", n) 661 } 662 663 if *lc.LaunchConfigurationName != rs.Primary.Attributes["launch_configuration"] { 664 return fmt.Errorf("Launch configuration names do not match") 665 } 666 667 return nil 668 } 669 } 670 671 func testAccCheckAWSAutoScalingGroupHealthyCapacity( 672 g *autoscaling.Group, exp int) resource.TestCheckFunc { 673 return func(s *terraform.State) error { 674 healthy := 0 675 for _, i := range g.Instances { 676 if i.HealthStatus == nil { 677 continue 678 } 679 if strings.EqualFold(*i.HealthStatus, "Healthy") { 680 healthy++ 681 } 682 } 683 if healthy < exp { 684 return fmt.Errorf("Expected at least %d healthy, got %d.", exp, healthy) 685 } 686 return nil 687 } 688 } 689 690 func testAccCheckAWSAutoScalingGroupAttributesVPCZoneIdentifer(group *autoscaling.Group) resource.TestCheckFunc { 691 return func(s *terraform.State) error { 692 // Grab Subnet Ids 693 var subnets []string 694 for _, rs := range s.RootModule().Resources { 695 if rs.Type != "aws_subnet" { 696 continue 697 } 698 subnets = append(subnets, rs.Primary.Attributes["id"]) 699 } 700 701 if group.VPCZoneIdentifier == nil { 702 return fmt.Errorf("Bad VPC Zone Identifier\nexpected: %s\ngot nil", subnets) 703 } 704 705 zones := strings.Split(*group.VPCZoneIdentifier, ",") 706 707 remaining := len(zones) 708 for _, z := range zones { 709 for _, s := range subnets { 710 if z == s { 711 remaining-- 712 } 713 } 714 } 715 716 if remaining != 0 { 717 return fmt.Errorf("Bad VPC Zone Identifier match\nexpected: %s\ngot:%s", zones, subnets) 718 } 719 720 return nil 721 } 722 } 723 724 // testAccCheckAWSALBTargetGroupHealthy checks an *elbv2.TargetGroup to make 725 // sure that all instances in it are healthy. 726 func testAccCheckAWSALBTargetGroupHealthy(res *elbv2.TargetGroup) resource.TestCheckFunc { 727 return func(s *terraform.State) error { 728 conn := testAccProvider.Meta().(*AWSClient).elbv2conn 729 730 resp, err := conn.DescribeTargetHealth(&elbv2.DescribeTargetHealthInput{ 731 TargetGroupArn: res.TargetGroupArn, 732 }) 733 734 if err != nil { 735 return err 736 } 737 738 for _, target := range resp.TargetHealthDescriptions { 739 if target.TargetHealth == nil || target.TargetHealth.State == nil || *target.TargetHealth.State != "healthy" { 740 return errors.New("Not all instances in target group are healthy yet, but should be") 741 } 742 } 743 744 return nil 745 } 746 } 747 748 const testAccAWSAutoScalingGroupConfig_autoGeneratedName = ` 749 resource "aws_launch_configuration" "foobar" { 750 image_id = "ami-21f78e11" 751 instance_type = "t1.micro" 752 } 753 754 resource "aws_autoscaling_group" "bar" { 755 availability_zones = ["us-west-2a"] 756 desired_capacity = 0 757 max_size = 0 758 min_size = 0 759 launch_configuration = "${aws_launch_configuration.foobar.name}" 760 } 761 ` 762 763 const testAccAWSAutoScalingGroupConfig_namePrefix = ` 764 resource "aws_launch_configuration" "test" { 765 image_id = "ami-21f78e11" 766 instance_type = "t1.micro" 767 } 768 769 resource "aws_autoscaling_group" "test" { 770 availability_zones = ["us-west-2a"] 771 desired_capacity = 0 772 max_size = 0 773 min_size = 0 774 name_prefix = "test-" 775 launch_configuration = "${aws_launch_configuration.test.name}" 776 } 777 ` 778 779 const testAccAWSAutoScalingGroupConfig_terminationPoliciesEmpty = ` 780 resource "aws_launch_configuration" "foobar" { 781 image_id = "ami-21f78e11" 782 instance_type = "t1.micro" 783 } 784 785 resource "aws_autoscaling_group" "bar" { 786 availability_zones = ["us-west-2a"] 787 max_size = 0 788 min_size = 0 789 desired_capacity = 0 790 791 launch_configuration = "${aws_launch_configuration.foobar.name}" 792 } 793 ` 794 795 const testAccAWSAutoScalingGroupConfig_terminationPoliciesExplicitDefault = ` 796 resource "aws_launch_configuration" "foobar" { 797 image_id = "ami-21f78e11" 798 instance_type = "t1.micro" 799 } 800 801 resource "aws_autoscaling_group" "bar" { 802 availability_zones = ["us-west-2a"] 803 max_size = 0 804 min_size = 0 805 desired_capacity = 0 806 termination_policies = ["Default"] 807 808 launch_configuration = "${aws_launch_configuration.foobar.name}" 809 } 810 ` 811 812 const testAccAWSAutoScalingGroupConfig_terminationPoliciesUpdate = ` 813 resource "aws_launch_configuration" "foobar" { 814 image_id = "ami-21f78e11" 815 instance_type = "t1.micro" 816 } 817 818 resource "aws_autoscaling_group" "bar" { 819 availability_zones = ["us-west-2a"] 820 max_size = 0 821 min_size = 0 822 desired_capacity = 0 823 termination_policies = ["OldestInstance"] 824 825 launch_configuration = "${aws_launch_configuration.foobar.name}" 826 } 827 ` 828 829 func testAccAWSAutoScalingGroupConfig(name string) string { 830 return fmt.Sprintf(` 831 resource "aws_launch_configuration" "foobar" { 832 image_id = "ami-21f78e11" 833 instance_type = "t1.micro" 834 } 835 836 resource "aws_placement_group" "test" { 837 name = "asg_pg_%s" 838 strategy = "cluster" 839 } 840 841 resource "aws_autoscaling_group" "bar" { 842 availability_zones = ["us-west-2a"] 843 name = "%s" 844 max_size = 5 845 min_size = 2 846 health_check_type = "ELB" 847 desired_capacity = 4 848 force_delete = true 849 termination_policies = ["OldestInstance","ClosestToNextInstanceHour"] 850 851 launch_configuration = "${aws_launch_configuration.foobar.name}" 852 853 tag { 854 key = "Foo" 855 value = "foo-bar" 856 propagate_at_launch = true 857 } 858 } 859 `, name, name) 860 } 861 862 func testAccAWSAutoScalingGroupConfigUpdate(name string) string { 863 return fmt.Sprintf(` 864 resource "aws_launch_configuration" "foobar" { 865 image_id = "ami-21f78e11" 866 instance_type = "t1.micro" 867 } 868 869 resource "aws_launch_configuration" "new" { 870 image_id = "ami-21f78e11" 871 instance_type = "t1.micro" 872 } 873 874 resource "aws_autoscaling_group" "bar" { 875 availability_zones = ["us-west-2a"] 876 name = "%s" 877 max_size = 5 878 min_size = 2 879 health_check_grace_period = 300 880 health_check_type = "ELB" 881 desired_capacity = 5 882 force_delete = true 883 termination_policies = ["ClosestToNextInstanceHour"] 884 protect_from_scale_in = true 885 886 launch_configuration = "${aws_launch_configuration.new.name}" 887 888 tag { 889 key = "Bar" 890 value = "bar-foo" 891 propagate_at_launch = true 892 } 893 } 894 `, name) 895 } 896 897 const testAccAWSAutoScalingGroupConfigWithLoadBalancer = ` 898 resource "aws_vpc" "foo" { 899 cidr_block = "10.1.0.0/16" 900 tags { Name = "tf-asg-test" } 901 } 902 903 resource "aws_internet_gateway" "gw" { 904 vpc_id = "${aws_vpc.foo.id}" 905 } 906 907 resource "aws_subnet" "foo" { 908 cidr_block = "10.1.1.0/24" 909 vpc_id = "${aws_vpc.foo.id}" 910 } 911 912 resource "aws_security_group" "foo" { 913 vpc_id="${aws_vpc.foo.id}" 914 915 ingress { 916 protocol = "-1" 917 from_port = 0 918 to_port = 0 919 cidr_blocks = ["0.0.0.0/0"] 920 } 921 922 egress { 923 protocol = "-1" 924 from_port = 0 925 to_port = 0 926 cidr_blocks = ["0.0.0.0/0"] 927 } 928 } 929 930 resource "aws_elb" "bar" { 931 subnets = ["${aws_subnet.foo.id}"] 932 security_groups = ["${aws_security_group.foo.id}"] 933 934 listener { 935 instance_port = 80 936 instance_protocol = "http" 937 lb_port = 80 938 lb_protocol = "http" 939 } 940 941 health_check { 942 healthy_threshold = 2 943 unhealthy_threshold = 2 944 target = "HTTP:80/" 945 interval = 5 946 timeout = 2 947 } 948 949 depends_on = ["aws_internet_gateway.gw"] 950 } 951 952 resource "aws_launch_configuration" "foobar" { 953 // need an AMI that listens on :80 at boot, this is: 954 // bitnami-nginxstack-1.6.1-0-linux-ubuntu-14.04.1-x86_64-hvm-ebs-ami-99f5b1a9-3 955 image_id = "ami-b5b3fc85" 956 instance_type = "t2.micro" 957 security_groups = ["${aws_security_group.foo.id}"] 958 } 959 960 resource "aws_autoscaling_group" "bar" { 961 availability_zones = ["${aws_subnet.foo.availability_zone}"] 962 vpc_zone_identifier = ["${aws_subnet.foo.id}"] 963 max_size = 2 964 min_size = 2 965 health_check_grace_period = 300 966 health_check_type = "ELB" 967 wait_for_elb_capacity = 2 968 force_delete = true 969 970 launch_configuration = "${aws_launch_configuration.foobar.name}" 971 load_balancers = ["${aws_elb.bar.name}"] 972 } 973 ` 974 975 const testAccAWSAutoScalingGroupConfigWithAZ = ` 976 resource "aws_vpc" "default" { 977 cidr_block = "10.0.0.0/16" 978 tags { 979 Name = "terraform-test" 980 } 981 } 982 983 resource "aws_subnet" "main" { 984 vpc_id = "${aws_vpc.default.id}" 985 cidr_block = "10.0.1.0/24" 986 availability_zone = "us-west-2a" 987 tags { 988 Name = "terraform-test" 989 } 990 } 991 992 resource "aws_launch_configuration" "foobar" { 993 image_id = "ami-b5b3fc85" 994 instance_type = "t2.micro" 995 } 996 997 resource "aws_autoscaling_group" "bar" { 998 availability_zones = [ 999 "us-west-2a" 1000 ] 1001 desired_capacity = 0 1002 max_size = 0 1003 min_size = 0 1004 launch_configuration = "${aws_launch_configuration.foobar.name}" 1005 } 1006 ` 1007 1008 const testAccAWSAutoScalingGroupConfigWithVPCIdent = ` 1009 resource "aws_vpc" "default" { 1010 cidr_block = "10.0.0.0/16" 1011 tags { 1012 Name = "terraform-test" 1013 } 1014 } 1015 1016 resource "aws_subnet" "main" { 1017 vpc_id = "${aws_vpc.default.id}" 1018 cidr_block = "10.0.1.0/24" 1019 availability_zone = "us-west-2a" 1020 tags { 1021 Name = "terraform-test" 1022 } 1023 } 1024 1025 resource "aws_launch_configuration" "foobar" { 1026 image_id = "ami-b5b3fc85" 1027 instance_type = "t2.micro" 1028 } 1029 1030 resource "aws_autoscaling_group" "bar" { 1031 vpc_zone_identifier = [ 1032 "${aws_subnet.main.id}", 1033 ] 1034 desired_capacity = 0 1035 max_size = 0 1036 min_size = 0 1037 launch_configuration = "${aws_launch_configuration.foobar.name}" 1038 } 1039 ` 1040 1041 func testAccAWSAutoScalingGroupConfig_withPlacementGroup(name string) string { 1042 return fmt.Sprintf(` 1043 resource "aws_launch_configuration" "foobar" { 1044 image_id = "ami-21f78e11" 1045 instance_type = "c3.large" 1046 } 1047 1048 resource "aws_placement_group" "test" { 1049 name = "%s" 1050 strategy = "cluster" 1051 } 1052 1053 resource "aws_autoscaling_group" "bar" { 1054 availability_zones = ["us-west-2a"] 1055 name = "%s" 1056 max_size = 1 1057 min_size = 1 1058 health_check_grace_period = 300 1059 health_check_type = "ELB" 1060 desired_capacity = 1 1061 force_delete = true 1062 termination_policies = ["OldestInstance","ClosestToNextInstanceHour"] 1063 placement_group = "${aws_placement_group.test.name}" 1064 1065 launch_configuration = "${aws_launch_configuration.foobar.name}" 1066 1067 tag { 1068 key = "Foo" 1069 value = "foo-bar" 1070 propagate_at_launch = true 1071 } 1072 } 1073 `, name, name) 1074 } 1075 1076 const testAccAWSAutoscalingMetricsCollectionConfig_allMetricsCollected = ` 1077 resource "aws_launch_configuration" "foobar" { 1078 image_id = "ami-21f78e11" 1079 instance_type = "t1.micro" 1080 } 1081 1082 resource "aws_autoscaling_group" "bar" { 1083 availability_zones = ["us-west-2a"] 1084 max_size = 1 1085 min_size = 0 1086 health_check_grace_period = 300 1087 health_check_type = "EC2" 1088 desired_capacity = 0 1089 force_delete = true 1090 termination_policies = ["OldestInstance","ClosestToNextInstanceHour"] 1091 launch_configuration = "${aws_launch_configuration.foobar.name}" 1092 enabled_metrics = ["GroupTotalInstances", 1093 "GroupPendingInstances", 1094 "GroupTerminatingInstances", 1095 "GroupDesiredCapacity", 1096 "GroupInServiceInstances", 1097 "GroupMinSize", 1098 "GroupMaxSize" 1099 ] 1100 metrics_granularity = "1Minute" 1101 } 1102 ` 1103 1104 const testAccAWSAutoscalingMetricsCollectionConfig_updatingMetricsCollected = ` 1105 resource "aws_launch_configuration" "foobar" { 1106 image_id = "ami-21f78e11" 1107 instance_type = "t1.micro" 1108 } 1109 1110 resource "aws_autoscaling_group" "bar" { 1111 availability_zones = ["us-west-2a"] 1112 max_size = 1 1113 min_size = 0 1114 health_check_grace_period = 300 1115 health_check_type = "EC2" 1116 desired_capacity = 0 1117 force_delete = true 1118 termination_policies = ["OldestInstance","ClosestToNextInstanceHour"] 1119 launch_configuration = "${aws_launch_configuration.foobar.name}" 1120 enabled_metrics = ["GroupTotalInstances", 1121 "GroupPendingInstances", 1122 "GroupTerminatingInstances", 1123 "GroupDesiredCapacity", 1124 "GroupMaxSize" 1125 ] 1126 metrics_granularity = "1Minute" 1127 } 1128 ` 1129 1130 const testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_pre = ` 1131 provider "aws" { 1132 region = "us-west-2" 1133 } 1134 1135 resource "aws_vpc" "default" { 1136 cidr_block = "10.0.0.0/16" 1137 1138 tags { 1139 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1140 } 1141 } 1142 1143 resource "aws_alb_target_group" "test" { 1144 name = "tf-example-alb-tg" 1145 port = 80 1146 protocol = "HTTP" 1147 vpc_id = "${aws_vpc.default.id}" 1148 } 1149 1150 resource "aws_subnet" "main" { 1151 vpc_id = "${aws_vpc.default.id}" 1152 cidr_block = "10.0.1.0/24" 1153 availability_zone = "us-west-2a" 1154 1155 tags { 1156 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1157 } 1158 } 1159 1160 resource "aws_subnet" "alt" { 1161 vpc_id = "${aws_vpc.default.id}" 1162 cidr_block = "10.0.2.0/24" 1163 availability_zone = "us-west-2b" 1164 1165 tags { 1166 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1167 } 1168 } 1169 1170 resource "aws_launch_configuration" "foobar" { 1171 # Golang-base from cts-hashi aws account, shared with tf testing account 1172 image_id = "ami-1817d178" 1173 instance_type = "t2.micro" 1174 enable_monitoring = false 1175 } 1176 1177 resource "aws_autoscaling_group" "bar" { 1178 vpc_zone_identifier = [ 1179 "${aws_subnet.main.id}", 1180 "${aws_subnet.alt.id}", 1181 ] 1182 1183 max_size = 2 1184 min_size = 0 1185 health_check_grace_period = 300 1186 health_check_type = "ELB" 1187 desired_capacity = 0 1188 force_delete = true 1189 termination_policies = ["OldestInstance"] 1190 launch_configuration = "${aws_launch_configuration.foobar.name}" 1191 1192 } 1193 1194 resource "aws_security_group" "tf_test_self" { 1195 name = "tf_test_alb_asg" 1196 description = "tf_test_alb_asg" 1197 vpc_id = "${aws_vpc.default.id}" 1198 1199 ingress { 1200 from_port = 80 1201 to_port = 80 1202 protocol = "tcp" 1203 cidr_blocks = ["0.0.0.0/0"] 1204 } 1205 1206 tags { 1207 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1208 } 1209 } 1210 ` 1211 1212 const testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_post = ` 1213 provider "aws" { 1214 region = "us-west-2" 1215 } 1216 1217 resource "aws_vpc" "default" { 1218 cidr_block = "10.0.0.0/16" 1219 1220 tags { 1221 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1222 } 1223 } 1224 1225 resource "aws_alb_target_group" "test" { 1226 name = "tf-example-alb-tg" 1227 port = 80 1228 protocol = "HTTP" 1229 vpc_id = "${aws_vpc.default.id}" 1230 } 1231 1232 resource "aws_subnet" "main" { 1233 vpc_id = "${aws_vpc.default.id}" 1234 cidr_block = "10.0.1.0/24" 1235 availability_zone = "us-west-2a" 1236 1237 tags { 1238 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1239 } 1240 } 1241 1242 resource "aws_subnet" "alt" { 1243 vpc_id = "${aws_vpc.default.id}" 1244 cidr_block = "10.0.2.0/24" 1245 availability_zone = "us-west-2b" 1246 1247 tags { 1248 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1249 } 1250 } 1251 1252 resource "aws_launch_configuration" "foobar" { 1253 # Golang-base from cts-hashi aws account, shared with tf testing account 1254 image_id = "ami-1817d178" 1255 instance_type = "t2.micro" 1256 enable_monitoring = false 1257 } 1258 1259 resource "aws_autoscaling_group" "bar" { 1260 vpc_zone_identifier = [ 1261 "${aws_subnet.main.id}", 1262 "${aws_subnet.alt.id}", 1263 ] 1264 1265 target_group_arns = ["${aws_alb_target_group.test.arn}"] 1266 1267 max_size = 2 1268 min_size = 0 1269 health_check_grace_period = 300 1270 health_check_type = "ELB" 1271 desired_capacity = 0 1272 force_delete = true 1273 termination_policies = ["OldestInstance"] 1274 launch_configuration = "${aws_launch_configuration.foobar.name}" 1275 1276 } 1277 1278 resource "aws_security_group" "tf_test_self" { 1279 name = "tf_test_alb_asg" 1280 description = "tf_test_alb_asg" 1281 vpc_id = "${aws_vpc.default.id}" 1282 1283 ingress { 1284 from_port = 80 1285 to_port = 80 1286 protocol = "tcp" 1287 cidr_blocks = ["0.0.0.0/0"] 1288 } 1289 1290 tags { 1291 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1292 } 1293 } 1294 ` 1295 1296 const testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_post_duo = ` 1297 provider "aws" { 1298 region = "us-west-2" 1299 } 1300 1301 resource "aws_vpc" "default" { 1302 cidr_block = "10.0.0.0/16" 1303 1304 tags { 1305 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1306 } 1307 } 1308 1309 resource "aws_alb_target_group" "test" { 1310 name = "tf-example-alb-tg" 1311 port = 80 1312 protocol = "HTTP" 1313 vpc_id = "${aws_vpc.default.id}" 1314 } 1315 1316 resource "aws_alb_target_group" "test_more" { 1317 name = "tf-example-alb-tg-more" 1318 port = 80 1319 protocol = "HTTP" 1320 vpc_id = "${aws_vpc.default.id}" 1321 } 1322 1323 resource "aws_subnet" "main" { 1324 vpc_id = "${aws_vpc.default.id}" 1325 cidr_block = "10.0.1.0/24" 1326 availability_zone = "us-west-2a" 1327 1328 tags { 1329 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1330 } 1331 } 1332 1333 resource "aws_subnet" "alt" { 1334 vpc_id = "${aws_vpc.default.id}" 1335 cidr_block = "10.0.2.0/24" 1336 availability_zone = "us-west-2b" 1337 1338 tags { 1339 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1340 } 1341 } 1342 1343 resource "aws_launch_configuration" "foobar" { 1344 # Golang-base from cts-hashi aws account, shared with tf testing account 1345 image_id = "ami-1817d178" 1346 instance_type = "t2.micro" 1347 enable_monitoring = false 1348 } 1349 1350 resource "aws_autoscaling_group" "bar" { 1351 vpc_zone_identifier = [ 1352 "${aws_subnet.main.id}", 1353 "${aws_subnet.alt.id}", 1354 ] 1355 1356 target_group_arns = [ 1357 "${aws_alb_target_group.test.arn}", 1358 "${aws_alb_target_group.test_more.arn}", 1359 ] 1360 1361 max_size = 2 1362 min_size = 0 1363 health_check_grace_period = 300 1364 health_check_type = "ELB" 1365 desired_capacity = 0 1366 force_delete = true 1367 termination_policies = ["OldestInstance"] 1368 launch_configuration = "${aws_launch_configuration.foobar.name}" 1369 1370 } 1371 1372 resource "aws_security_group" "tf_test_self" { 1373 name = "tf_test_alb_asg" 1374 description = "tf_test_alb_asg" 1375 vpc_id = "${aws_vpc.default.id}" 1376 1377 ingress { 1378 from_port = 80 1379 to_port = 80 1380 protocol = "tcp" 1381 cidr_blocks = ["0.0.0.0/0"] 1382 } 1383 1384 tags { 1385 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1386 } 1387 } 1388 ` 1389 1390 func testAccAWSAutoScalingGroupWithHookConfig(name string) string { 1391 return fmt.Sprintf(` 1392 resource "aws_launch_configuration" "foobar" { 1393 image_id = "ami-21f78e11" 1394 instance_type = "t1.micro" 1395 } 1396 1397 resource "aws_autoscaling_group" "bar" { 1398 availability_zones = ["us-west-2a"] 1399 name = "%s" 1400 max_size = 5 1401 min_size = 2 1402 health_check_type = "ELB" 1403 desired_capacity = 4 1404 force_delete = true 1405 termination_policies = ["OldestInstance","ClosestToNextInstanceHour"] 1406 1407 launch_configuration = "${aws_launch_configuration.foobar.name}" 1408 1409 initial_lifecycle_hook { 1410 name = "launching" 1411 default_result = "CONTINUE" 1412 heartbeat_timeout = 30 # minimum value 1413 lifecycle_transition = "autoscaling:EC2_INSTANCE_LAUNCHING" 1414 } 1415 } 1416 `, name) 1417 } 1418 1419 func testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_ELBCapacity(rInt int) string { 1420 return fmt.Sprintf(` 1421 provider "aws" { 1422 region = "us-west-2" 1423 } 1424 1425 resource "aws_vpc" "default" { 1426 cidr_block = "10.0.0.0/16" 1427 enable_dns_hostnames = "true" 1428 enable_dns_support = "true" 1429 1430 tags { 1431 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_ELBCapacity" 1432 } 1433 } 1434 1435 resource "aws_alb" "test_lb" { 1436 subnets = ["${aws_subnet.main.id}", "${aws_subnet.alt.id}"] 1437 1438 tags { 1439 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_ELBCapacity" 1440 } 1441 } 1442 1443 resource "aws_alb_listener" "test_listener" { 1444 load_balancer_arn = "${aws_alb.test_lb.arn}" 1445 port = "80" 1446 1447 default_action { 1448 target_group_arn = "${aws_alb_target_group.test.arn}" 1449 type = "forward" 1450 } 1451 } 1452 1453 resource "aws_alb_target_group" "test" { 1454 name = "tf-alb-test-%d" 1455 port = 80 1456 protocol = "HTTP" 1457 vpc_id = "${aws_vpc.default.id}" 1458 1459 health_check { 1460 path = "/" 1461 healthy_threshold = "2" 1462 timeout = "2" 1463 interval = "5" 1464 } 1465 1466 tags { 1467 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_ELBCapacity" 1468 } 1469 } 1470 1471 resource "aws_subnet" "main" { 1472 vpc_id = "${aws_vpc.default.id}" 1473 cidr_block = "10.0.1.0/24" 1474 availability_zone = "us-west-2a" 1475 1476 tags { 1477 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_ELBCapacity" 1478 } 1479 } 1480 1481 resource "aws_subnet" "alt" { 1482 vpc_id = "${aws_vpc.default.id}" 1483 cidr_block = "10.0.2.0/24" 1484 availability_zone = "us-west-2b" 1485 1486 tags { 1487 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_ELBCapacity" 1488 } 1489 } 1490 1491 resource "aws_internet_gateway" "internet_gateway" { 1492 vpc_id = "${aws_vpc.default.id}" 1493 } 1494 1495 resource "aws_route_table" "route_table" { 1496 vpc_id = "${aws_vpc.default.id}" 1497 } 1498 1499 resource "aws_route_table_association" "route_table_association_main" { 1500 subnet_id = "${aws_subnet.main.id}" 1501 route_table_id = "${aws_route_table.route_table.id}" 1502 } 1503 1504 resource "aws_route_table_association" "route_table_association_alt" { 1505 subnet_id = "${aws_subnet.alt.id}" 1506 route_table_id = "${aws_route_table.route_table.id}" 1507 } 1508 1509 resource "aws_route" "public_default_route" { 1510 route_table_id = "${aws_route_table.route_table.id}" 1511 destination_cidr_block = "0.0.0.0/0" 1512 gateway_id = "${aws_internet_gateway.internet_gateway.id}" 1513 } 1514 1515 data "aws_ami" "test_ami" { 1516 most_recent = true 1517 1518 filter { 1519 name = "owner-alias" 1520 values = ["amazon"] 1521 } 1522 1523 filter { 1524 name = "name" 1525 values = ["amzn-ami-hvm-*-x86_64-gp2"] 1526 } 1527 } 1528 1529 resource "aws_launch_configuration" "foobar" { 1530 image_id = "${data.aws_ami.test_ami.id}" 1531 instance_type = "t2.micro" 1532 associate_public_ip_address = "true" 1533 1534 user_data = <<EOS 1535 #!/bin/bash 1536 yum -y install httpd 1537 echo "hello world" > /var/www/html/index.html 1538 chkconfig httpd on 1539 service httpd start 1540 EOS 1541 } 1542 1543 resource "aws_autoscaling_group" "bar" { 1544 vpc_zone_identifier = [ 1545 "${aws_subnet.main.id}", 1546 "${aws_subnet.alt.id}", 1547 ] 1548 1549 target_group_arns = ["${aws_alb_target_group.test.arn}"] 1550 1551 max_size = 2 1552 min_size = 2 1553 health_check_grace_period = 300 1554 health_check_type = "ELB" 1555 desired_capacity = 2 1556 wait_for_elb_capacity = 2 1557 force_delete = true 1558 termination_policies = ["OldestInstance"] 1559 launch_configuration = "${aws_launch_configuration.foobar.name}" 1560 }`, rInt) 1561 } 1562 1563 func testAccAWSAutoScalingGroupConfigWithSuspendedProcesses(name string) string { 1564 return fmt.Sprintf(` 1565 resource "aws_launch_configuration" "foobar" { 1566 image_id = "ami-21f78e11" 1567 instance_type = "t1.micro" 1568 } 1569 1570 resource "aws_placement_group" "test" { 1571 name = "asg_pg_%s" 1572 strategy = "cluster" 1573 } 1574 1575 resource "aws_autoscaling_group" "bar" { 1576 availability_zones = ["us-west-2a"] 1577 name = "%s" 1578 max_size = 5 1579 min_size = 2 1580 health_check_type = "ELB" 1581 desired_capacity = 4 1582 force_delete = true 1583 termination_policies = ["OldestInstance","ClosestToNextInstanceHour"] 1584 1585 launch_configuration = "${aws_launch_configuration.foobar.name}" 1586 1587 suspended_processes = ["AlarmNotification","ScheduledActions"] 1588 1589 tag { 1590 key = "Foo" 1591 value = "foo-bar" 1592 propagate_at_launch = true 1593 } 1594 } 1595 `, name, name) 1596 } 1597 1598 func testAccAWSAutoScalingGroupConfigWithSuspendedProcessesUpdated(name string) string { 1599 return fmt.Sprintf(` 1600 resource "aws_launch_configuration" "foobar" { 1601 image_id = "ami-21f78e11" 1602 instance_type = "t1.micro" 1603 } 1604 1605 resource "aws_placement_group" "test" { 1606 name = "asg_pg_%s" 1607 strategy = "cluster" 1608 } 1609 1610 resource "aws_autoscaling_group" "bar" { 1611 availability_zones = ["us-west-2a"] 1612 name = "%s" 1613 max_size = 5 1614 min_size = 2 1615 health_check_type = "ELB" 1616 desired_capacity = 4 1617 force_delete = true 1618 termination_policies = ["OldestInstance","ClosestToNextInstanceHour"] 1619 1620 launch_configuration = "${aws_launch_configuration.foobar.name}" 1621 1622 suspended_processes = ["AZRebalance","ScheduledActions"] 1623 1624 tag { 1625 key = "Foo" 1626 value = "foo-bar" 1627 propagate_at_launch = true 1628 } 1629 } 1630 `, name, name) 1631 }