github.com/tomaszheflik/terraform@v0.7.3-0.20160827060421-32f990b41594/builtin/providers/aws/resource_aws_autoscaling_group_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "reflect" 6 "regexp" 7 "sort" 8 "strings" 9 "testing" 10 11 "github.com/aws/aws-sdk-go/aws" 12 "github.com/aws/aws-sdk-go/aws/awserr" 13 "github.com/aws/aws-sdk-go/service/autoscaling" 14 "github.com/aws/aws-sdk-go/service/elbv2" 15 "github.com/hashicorp/terraform/helper/acctest" 16 "github.com/hashicorp/terraform/helper/resource" 17 "github.com/hashicorp/terraform/terraform" 18 ) 19 20 func TestAccAWSAutoScalingGroup_basic(t *testing.T) { 21 var group autoscaling.Group 22 var lc autoscaling.LaunchConfiguration 23 24 randName := fmt.Sprintf("terraform-test-%s", acctest.RandString(10)) 25 26 resource.Test(t, resource.TestCase{ 27 PreCheck: func() { testAccPreCheck(t) }, 28 IDRefreshName: "aws_autoscaling_group.bar", 29 IDRefreshIgnore: []string{"force_delete", "metrics_granularity", "wait_for_capacity_timeout"}, 30 Providers: testAccProviders, 31 CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, 32 Steps: []resource.TestStep{ 33 resource.TestStep{ 34 Config: testAccAWSAutoScalingGroupConfig(randName), 35 Check: resource.ComposeTestCheckFunc( 36 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 37 testAccCheckAWSAutoScalingGroupHealthyCapacity(&group, 2), 38 testAccCheckAWSAutoScalingGroupAttributes(&group, randName), 39 resource.TestCheckResourceAttr( 40 "aws_autoscaling_group.bar", "availability_zones.2487133097", "us-west-2a"), 41 resource.TestCheckResourceAttr( 42 "aws_autoscaling_group.bar", "name", randName), 43 resource.TestCheckResourceAttr( 44 "aws_autoscaling_group.bar", "max_size", "5"), 45 resource.TestCheckResourceAttr( 46 "aws_autoscaling_group.bar", "min_size", "2"), 47 resource.TestCheckResourceAttr( 48 "aws_autoscaling_group.bar", "health_check_grace_period", "300"), 49 resource.TestCheckResourceAttr( 50 "aws_autoscaling_group.bar", "health_check_type", "ELB"), 51 resource.TestCheckResourceAttr( 52 "aws_autoscaling_group.bar", "desired_capacity", "4"), 53 resource.TestCheckResourceAttr( 54 "aws_autoscaling_group.bar", "force_delete", "true"), 55 resource.TestCheckResourceAttr( 56 "aws_autoscaling_group.bar", "termination_policies.0", "OldestInstance"), 57 resource.TestCheckResourceAttr( 58 "aws_autoscaling_group.bar", "termination_policies.1", "ClosestToNextInstanceHour"), 59 resource.TestCheckResourceAttr( 60 "aws_autoscaling_group.bar", "protect_from_scale_in", "false"), 61 ), 62 }, 63 64 resource.TestStep{ 65 Config: testAccAWSAutoScalingGroupConfigUpdate(randName), 66 Check: resource.ComposeTestCheckFunc( 67 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 68 testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.new", &lc), 69 resource.TestCheckResourceAttr( 70 "aws_autoscaling_group.bar", "desired_capacity", "5"), 71 resource.TestCheckResourceAttr( 72 "aws_autoscaling_group.bar", "termination_policies.0", "ClosestToNextInstanceHour"), 73 resource.TestCheckResourceAttr( 74 "aws_autoscaling_group.bar", "protect_from_scale_in", "true"), 75 testLaunchConfigurationName("aws_autoscaling_group.bar", &lc), 76 testAccCheckAutoscalingTags(&group.Tags, "Bar", map[string]interface{}{ 77 "value": "bar-foo", 78 "propagate_at_launch": true, 79 }), 80 ), 81 }, 82 }, 83 }) 84 } 85 86 func TestAccAWSAutoScalingGroup_autoGeneratedName(t *testing.T) { 87 asgNameRegexp := regexp.MustCompile("^tf-asg-") 88 89 resource.Test(t, resource.TestCase{ 90 PreCheck: func() { testAccPreCheck(t) }, 91 Providers: testAccProviders, 92 CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, 93 Steps: []resource.TestStep{ 94 resource.TestStep{ 95 Config: testAccAWSAutoScalingGroupConfig_autoGeneratedName, 96 Check: resource.ComposeTestCheckFunc( 97 resource.TestMatchResourceAttr( 98 "aws_autoscaling_group.bar", "name", asgNameRegexp), 99 ), 100 }, 101 }, 102 }) 103 } 104 105 func TestAccAWSAutoScalingGroup_terminationPolicies(t *testing.T) { 106 resource.Test(t, resource.TestCase{ 107 PreCheck: func() { testAccPreCheck(t) }, 108 Providers: testAccProviders, 109 CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, 110 Steps: []resource.TestStep{ 111 resource.TestStep{ 112 Config: testAccAWSAutoScalingGroupConfig_terminationPoliciesEmpty, 113 Check: resource.ComposeTestCheckFunc( 114 resource.TestCheckResourceAttr( 115 "aws_autoscaling_group.bar", "termination_policies.#", "0"), 116 ), 117 }, 118 119 resource.TestStep{ 120 Config: testAccAWSAutoScalingGroupConfig_terminationPoliciesUpdate, 121 Check: resource.ComposeTestCheckFunc( 122 resource.TestCheckResourceAttr( 123 "aws_autoscaling_group.bar", "termination_policies.#", "1"), 124 resource.TestCheckResourceAttr( 125 "aws_autoscaling_group.bar", "termination_policies.0", "OldestInstance"), 126 ), 127 }, 128 129 resource.TestStep{ 130 Config: testAccAWSAutoScalingGroupConfig_terminationPoliciesExplicitDefault, 131 Check: resource.ComposeTestCheckFunc( 132 resource.TestCheckResourceAttr( 133 "aws_autoscaling_group.bar", "termination_policies.#", "1"), 134 resource.TestCheckResourceAttr( 135 "aws_autoscaling_group.bar", "termination_policies.0", "Default"), 136 ), 137 }, 138 139 resource.TestStep{ 140 Config: testAccAWSAutoScalingGroupConfig_terminationPoliciesEmpty, 141 Check: resource.ComposeTestCheckFunc( 142 resource.TestCheckResourceAttr( 143 "aws_autoscaling_group.bar", "termination_policies.#", "0"), 144 ), 145 }, 146 }, 147 }) 148 } 149 150 func TestAccAWSAutoScalingGroup_tags(t *testing.T) { 151 var group autoscaling.Group 152 153 randName := fmt.Sprintf("tfautotags-%s", acctest.RandString(5)) 154 155 resource.Test(t, resource.TestCase{ 156 PreCheck: func() { testAccPreCheck(t) }, 157 Providers: testAccProviders, 158 CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, 159 Steps: []resource.TestStep{ 160 resource.TestStep{ 161 Config: testAccAWSAutoScalingGroupConfig(randName), 162 Check: resource.ComposeTestCheckFunc( 163 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 164 testAccCheckAutoscalingTags(&group.Tags, "Foo", map[string]interface{}{ 165 "value": "foo-bar", 166 "propagate_at_launch": true, 167 }), 168 ), 169 }, 170 171 resource.TestStep{ 172 Config: testAccAWSAutoScalingGroupConfigUpdate(randName), 173 Check: resource.ComposeTestCheckFunc( 174 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 175 testAccCheckAutoscalingTagNotExists(&group.Tags, "Foo"), 176 testAccCheckAutoscalingTags(&group.Tags, "Bar", map[string]interface{}{ 177 "value": "bar-foo", 178 "propagate_at_launch": true, 179 }), 180 ), 181 }, 182 }, 183 }) 184 } 185 186 func TestAccAWSAutoScalingGroup_VpcUpdates(t *testing.T) { 187 var group autoscaling.Group 188 189 resource.Test(t, resource.TestCase{ 190 PreCheck: func() { testAccPreCheck(t) }, 191 Providers: testAccProviders, 192 CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, 193 Steps: []resource.TestStep{ 194 resource.TestStep{ 195 Config: testAccAWSAutoScalingGroupConfigWithAZ, 196 Check: resource.ComposeTestCheckFunc( 197 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 198 resource.TestCheckResourceAttr( 199 "aws_autoscaling_group.bar", "availability_zones.#", "1"), 200 resource.TestCheckResourceAttr( 201 "aws_autoscaling_group.bar", "availability_zones.2487133097", "us-west-2a"), 202 resource.TestCheckResourceAttr( 203 "aws_autoscaling_group.bar", "vpc_zone_identifier.#", "1"), 204 ), 205 }, 206 207 resource.TestStep{ 208 Config: testAccAWSAutoScalingGroupConfigWithVPCIdent, 209 Check: resource.ComposeTestCheckFunc( 210 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 211 testAccCheckAWSAutoScalingGroupAttributesVPCZoneIdentifer(&group), 212 resource.TestCheckResourceAttr( 213 "aws_autoscaling_group.bar", "availability_zones.#", "1"), 214 resource.TestCheckResourceAttr( 215 "aws_autoscaling_group.bar", "availability_zones.2487133097", "us-west-2a"), 216 resource.TestCheckResourceAttr( 217 "aws_autoscaling_group.bar", "vpc_zone_identifier.#", "1"), 218 ), 219 }, 220 }, 221 }) 222 } 223 224 func TestAccAWSAutoScalingGroup_WithLoadBalancer(t *testing.T) { 225 var group autoscaling.Group 226 227 resource.Test(t, resource.TestCase{ 228 PreCheck: func() { testAccPreCheck(t) }, 229 Providers: testAccProviders, 230 CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, 231 Steps: []resource.TestStep{ 232 resource.TestStep{ 233 Config: testAccAWSAutoScalingGroupConfigWithLoadBalancer, 234 Check: resource.ComposeTestCheckFunc( 235 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 236 testAccCheckAWSAutoScalingGroupAttributesLoadBalancer(&group), 237 ), 238 }, 239 }, 240 }) 241 } 242 243 func TestAccAWSAutoScalingGroup_withPlacementGroup(t *testing.T) { 244 var group autoscaling.Group 245 246 randName := fmt.Sprintf("tf_placement_test-%s", acctest.RandString(5)) 247 resource.Test(t, resource.TestCase{ 248 PreCheck: func() { testAccPreCheck(t) }, 249 Providers: testAccProviders, 250 CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, 251 Steps: []resource.TestStep{ 252 resource.TestStep{ 253 Config: testAccAWSAutoScalingGroupConfig_withPlacementGroup(randName), 254 Check: resource.ComposeTestCheckFunc( 255 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 256 resource.TestCheckResourceAttr( 257 "aws_autoscaling_group.bar", "placement_group", randName), 258 ), 259 }, 260 }, 261 }) 262 } 263 264 func TestAccAWSAutoScalingGroup_enablingMetrics(t *testing.T) { 265 var group autoscaling.Group 266 randName := fmt.Sprintf("terraform-test-%s", acctest.RandString(10)) 267 268 resource.Test(t, resource.TestCase{ 269 PreCheck: func() { testAccPreCheck(t) }, 270 Providers: testAccProviders, 271 CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, 272 Steps: []resource.TestStep{ 273 resource.TestStep{ 274 Config: testAccAWSAutoScalingGroupConfig(randName), 275 Check: resource.ComposeTestCheckFunc( 276 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 277 resource.TestCheckResourceAttr( 278 "aws_autoscaling_group.bar", "enabled_metrics.#", ""), 279 ), 280 }, 281 282 resource.TestStep{ 283 Config: testAccAWSAutoscalingMetricsCollectionConfig_updatingMetricsCollected, 284 Check: resource.ComposeTestCheckFunc( 285 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 286 resource.TestCheckResourceAttr( 287 "aws_autoscaling_group.bar", "enabled_metrics.#", "5"), 288 ), 289 }, 290 }, 291 }) 292 } 293 294 func TestAccAWSAutoScalingGroup_withMetrics(t *testing.T) { 295 var group autoscaling.Group 296 297 resource.Test(t, resource.TestCase{ 298 PreCheck: func() { testAccPreCheck(t) }, 299 Providers: testAccProviders, 300 CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, 301 Steps: []resource.TestStep{ 302 resource.TestStep{ 303 Config: testAccAWSAutoscalingMetricsCollectionConfig_allMetricsCollected, 304 Check: resource.ComposeTestCheckFunc( 305 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 306 resource.TestCheckResourceAttr( 307 "aws_autoscaling_group.bar", "enabled_metrics.#", "7"), 308 ), 309 }, 310 311 resource.TestStep{ 312 Config: testAccAWSAutoscalingMetricsCollectionConfig_updatingMetricsCollected, 313 Check: resource.ComposeTestCheckFunc( 314 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 315 resource.TestCheckResourceAttr( 316 "aws_autoscaling_group.bar", "enabled_metrics.#", "5"), 317 ), 318 }, 319 }, 320 }) 321 } 322 323 func TestAccAWSAutoScalingGroup_ALB_TargetGroups(t *testing.T) { 324 var group autoscaling.Group 325 var tg elbv2.TargetGroup 326 var tg2 elbv2.TargetGroup 327 328 testCheck := func(targets []*elbv2.TargetGroup) resource.TestCheckFunc { 329 return func(*terraform.State) error { 330 var ts []string 331 var gs []string 332 for _, t := range targets { 333 ts = append(ts, *t.TargetGroupArn) 334 } 335 336 for _, s := range group.TargetGroupARNs { 337 gs = append(gs, *s) 338 } 339 340 sort.Strings(ts) 341 sort.Strings(gs) 342 343 if !reflect.DeepEqual(ts, gs) { 344 return fmt.Errorf("Error: target group match not found!\nASG Target groups: %#v\nTarget Group: %#v", ts, gs) 345 } 346 return nil 347 } 348 } 349 350 resource.Test(t, resource.TestCase{ 351 PreCheck: func() { testAccPreCheck(t) }, 352 Providers: testAccProviders, 353 CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, 354 Steps: []resource.TestStep{ 355 resource.TestStep{ 356 Config: testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_pre, 357 Check: resource.ComposeAggregateTestCheckFunc( 358 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 359 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &tg), 360 resource.TestCheckResourceAttr( 361 "aws_autoscaling_group.bar", "target_group_arns.#", "0"), 362 ), 363 }, 364 365 resource.TestStep{ 366 Config: testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_post_duo, 367 Check: resource.ComposeAggregateTestCheckFunc( 368 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 369 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &tg), 370 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test_more", &tg2), 371 testCheck([]*elbv2.TargetGroup{&tg, &tg2}), 372 resource.TestCheckResourceAttr( 373 "aws_autoscaling_group.bar", "target_group_arns.#", "2"), 374 ), 375 }, 376 377 resource.TestStep{ 378 Config: testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_post, 379 Check: resource.ComposeAggregateTestCheckFunc( 380 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 381 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &tg), 382 testCheck([]*elbv2.TargetGroup{&tg}), 383 resource.TestCheckResourceAttr( 384 "aws_autoscaling_group.bar", "target_group_arns.#", "1"), 385 ), 386 }, 387 388 resource.TestStep{ 389 Config: testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_pre, 390 Check: resource.ComposeAggregateTestCheckFunc( 391 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 392 resource.TestCheckResourceAttr( 393 "aws_autoscaling_group.bar", "target_group_arns.#", "0"), 394 ), 395 }, 396 }, 397 }) 398 } 399 400 func testAccCheckAWSAutoScalingGroupDestroy(s *terraform.State) error { 401 conn := testAccProvider.Meta().(*AWSClient).autoscalingconn 402 403 for _, rs := range s.RootModule().Resources { 404 if rs.Type != "aws_autoscaling_group" { 405 continue 406 } 407 408 // Try to find the Group 409 describeGroups, err := conn.DescribeAutoScalingGroups( 410 &autoscaling.DescribeAutoScalingGroupsInput{ 411 AutoScalingGroupNames: []*string{aws.String(rs.Primary.ID)}, 412 }) 413 414 if err == nil { 415 if len(describeGroups.AutoScalingGroups) != 0 && 416 *describeGroups.AutoScalingGroups[0].AutoScalingGroupName == rs.Primary.ID { 417 return fmt.Errorf("AutoScaling Group still exists") 418 } 419 } 420 421 // Verify the error 422 ec2err, ok := err.(awserr.Error) 423 if !ok { 424 return err 425 } 426 if ec2err.Code() != "InvalidGroup.NotFound" { 427 return err 428 } 429 } 430 431 return nil 432 } 433 434 func testAccCheckAWSAutoScalingGroupAttributes(group *autoscaling.Group, name string) resource.TestCheckFunc { 435 return func(s *terraform.State) error { 436 if *group.AvailabilityZones[0] != "us-west-2a" { 437 return fmt.Errorf("Bad availability_zones: %#v", group.AvailabilityZones[0]) 438 } 439 440 if *group.AutoScalingGroupName != name { 441 return fmt.Errorf("Bad Autoscaling Group name, expected (%s), got (%s)", name, *group.AutoScalingGroupName) 442 } 443 444 if *group.MaxSize != 5 { 445 return fmt.Errorf("Bad max_size: %d", *group.MaxSize) 446 } 447 448 if *group.MinSize != 2 { 449 return fmt.Errorf("Bad max_size: %d", *group.MinSize) 450 } 451 452 if *group.HealthCheckType != "ELB" { 453 return fmt.Errorf("Bad health_check_type,\nexpected: %s\ngot: %s", "ELB", *group.HealthCheckType) 454 } 455 456 if *group.HealthCheckGracePeriod != 300 { 457 return fmt.Errorf("Bad health_check_grace_period: %d", *group.HealthCheckGracePeriod) 458 } 459 460 if *group.DesiredCapacity != 4 { 461 return fmt.Errorf("Bad desired_capacity: %d", *group.DesiredCapacity) 462 } 463 464 if *group.LaunchConfigurationName == "" { 465 return fmt.Errorf("Bad launch configuration name: %s", *group.LaunchConfigurationName) 466 } 467 468 t := &autoscaling.TagDescription{ 469 Key: aws.String("Foo"), 470 Value: aws.String("foo-bar"), 471 PropagateAtLaunch: aws.Bool(true), 472 ResourceType: aws.String("auto-scaling-group"), 473 ResourceId: group.AutoScalingGroupName, 474 } 475 476 if !reflect.DeepEqual(group.Tags[0], t) { 477 return fmt.Errorf( 478 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 479 group.Tags[0], 480 t) 481 } 482 483 return nil 484 } 485 } 486 487 func testAccCheckAWSAutoScalingGroupAttributesLoadBalancer(group *autoscaling.Group) resource.TestCheckFunc { 488 return func(s *terraform.State) error { 489 if len(group.LoadBalancerNames) != 1 { 490 return fmt.Errorf("Bad load_balancers: %v", group.LoadBalancerNames) 491 } 492 493 return nil 494 } 495 } 496 497 func testAccCheckAWSAutoScalingGroupExists(n string, group *autoscaling.Group) resource.TestCheckFunc { 498 return func(s *terraform.State) error { 499 rs, ok := s.RootModule().Resources[n] 500 if !ok { 501 return fmt.Errorf("Not found: %s", n) 502 } 503 504 if rs.Primary.ID == "" { 505 return fmt.Errorf("No AutoScaling Group ID is set") 506 } 507 508 conn := testAccProvider.Meta().(*AWSClient).autoscalingconn 509 510 describeGroups, err := conn.DescribeAutoScalingGroups( 511 &autoscaling.DescribeAutoScalingGroupsInput{ 512 AutoScalingGroupNames: []*string{aws.String(rs.Primary.ID)}, 513 }) 514 515 if err != nil { 516 return err 517 } 518 519 if len(describeGroups.AutoScalingGroups) != 1 || 520 *describeGroups.AutoScalingGroups[0].AutoScalingGroupName != rs.Primary.ID { 521 return fmt.Errorf("AutoScaling Group not found") 522 } 523 524 *group = *describeGroups.AutoScalingGroups[0] 525 526 return nil 527 } 528 } 529 530 func testLaunchConfigurationName(n string, lc *autoscaling.LaunchConfiguration) resource.TestCheckFunc { 531 return func(s *terraform.State) error { 532 rs, ok := s.RootModule().Resources[n] 533 if !ok { 534 return fmt.Errorf("Not found: %s", n) 535 } 536 537 if *lc.LaunchConfigurationName != rs.Primary.Attributes["launch_configuration"] { 538 return fmt.Errorf("Launch configuration names do not match") 539 } 540 541 return nil 542 } 543 } 544 545 func testAccCheckAWSAutoScalingGroupHealthyCapacity( 546 g *autoscaling.Group, exp int) resource.TestCheckFunc { 547 return func(s *terraform.State) error { 548 healthy := 0 549 for _, i := range g.Instances { 550 if i.HealthStatus == nil { 551 continue 552 } 553 if strings.EqualFold(*i.HealthStatus, "Healthy") { 554 healthy++ 555 } 556 } 557 if healthy < exp { 558 return fmt.Errorf("Expected at least %d healthy, got %d.", exp, healthy) 559 } 560 return nil 561 } 562 } 563 564 func testAccCheckAWSAutoScalingGroupAttributesVPCZoneIdentifer(group *autoscaling.Group) resource.TestCheckFunc { 565 return func(s *terraform.State) error { 566 // Grab Subnet Ids 567 var subnets []string 568 for _, rs := range s.RootModule().Resources { 569 if rs.Type != "aws_subnet" { 570 continue 571 } 572 subnets = append(subnets, rs.Primary.Attributes["id"]) 573 } 574 575 if group.VPCZoneIdentifier == nil { 576 return fmt.Errorf("Bad VPC Zone Identifier\nexpected: %s\ngot nil", subnets) 577 } 578 579 zones := strings.Split(*group.VPCZoneIdentifier, ",") 580 581 remaining := len(zones) 582 for _, z := range zones { 583 for _, s := range subnets { 584 if z == s { 585 remaining-- 586 } 587 } 588 } 589 590 if remaining != 0 { 591 return fmt.Errorf("Bad VPC Zone Identifier match\nexpected: %s\ngot:%s", zones, subnets) 592 } 593 594 return nil 595 } 596 } 597 598 const testAccAWSAutoScalingGroupConfig_autoGeneratedName = ` 599 resource "aws_launch_configuration" "foobar" { 600 image_id = "ami-21f78e11" 601 instance_type = "t1.micro" 602 } 603 604 resource "aws_autoscaling_group" "bar" { 605 availability_zones = ["us-west-2a"] 606 desired_capacity = 0 607 max_size = 0 608 min_size = 0 609 launch_configuration = "${aws_launch_configuration.foobar.name}" 610 } 611 ` 612 613 const testAccAWSAutoScalingGroupConfig_terminationPoliciesEmpty = ` 614 resource "aws_launch_configuration" "foobar" { 615 image_id = "ami-21f78e11" 616 instance_type = "t1.micro" 617 } 618 619 resource "aws_autoscaling_group" "bar" { 620 availability_zones = ["us-west-2a"] 621 max_size = 0 622 min_size = 0 623 desired_capacity = 0 624 625 launch_configuration = "${aws_launch_configuration.foobar.name}" 626 } 627 ` 628 629 const testAccAWSAutoScalingGroupConfig_terminationPoliciesExplicitDefault = ` 630 resource "aws_launch_configuration" "foobar" { 631 image_id = "ami-21f78e11" 632 instance_type = "t1.micro" 633 } 634 635 resource "aws_autoscaling_group" "bar" { 636 availability_zones = ["us-west-2a"] 637 max_size = 0 638 min_size = 0 639 desired_capacity = 0 640 termination_policies = ["Default"] 641 642 launch_configuration = "${aws_launch_configuration.foobar.name}" 643 } 644 ` 645 646 const testAccAWSAutoScalingGroupConfig_terminationPoliciesUpdate = ` 647 resource "aws_launch_configuration" "foobar" { 648 image_id = "ami-21f78e11" 649 instance_type = "t1.micro" 650 } 651 652 resource "aws_autoscaling_group" "bar" { 653 availability_zones = ["us-west-2a"] 654 max_size = 0 655 min_size = 0 656 desired_capacity = 0 657 termination_policies = ["OldestInstance"] 658 659 launch_configuration = "${aws_launch_configuration.foobar.name}" 660 } 661 ` 662 663 func testAccAWSAutoScalingGroupConfig(name string) string { 664 return fmt.Sprintf(` 665 resource "aws_launch_configuration" "foobar" { 666 image_id = "ami-21f78e11" 667 instance_type = "t1.micro" 668 } 669 670 resource "aws_placement_group" "test" { 671 name = "asg_pg_%s" 672 strategy = "cluster" 673 } 674 675 resource "aws_autoscaling_group" "bar" { 676 availability_zones = ["us-west-2a"] 677 name = "%s" 678 max_size = 5 679 min_size = 2 680 health_check_type = "ELB" 681 desired_capacity = 4 682 force_delete = true 683 termination_policies = ["OldestInstance","ClosestToNextInstanceHour"] 684 685 launch_configuration = "${aws_launch_configuration.foobar.name}" 686 687 tag { 688 key = "Foo" 689 value = "foo-bar" 690 propagate_at_launch = true 691 } 692 } 693 `, name, name) 694 } 695 696 func testAccAWSAutoScalingGroupConfigUpdate(name string) string { 697 return fmt.Sprintf(` 698 resource "aws_launch_configuration" "foobar" { 699 image_id = "ami-21f78e11" 700 instance_type = "t1.micro" 701 } 702 703 resource "aws_launch_configuration" "new" { 704 image_id = "ami-21f78e11" 705 instance_type = "t1.micro" 706 } 707 708 resource "aws_autoscaling_group" "bar" { 709 availability_zones = ["us-west-2a"] 710 name = "%s" 711 max_size = 5 712 min_size = 2 713 health_check_grace_period = 300 714 health_check_type = "ELB" 715 desired_capacity = 5 716 force_delete = true 717 termination_policies = ["ClosestToNextInstanceHour"] 718 protect_from_scale_in = true 719 720 launch_configuration = "${aws_launch_configuration.new.name}" 721 722 tag { 723 key = "Bar" 724 value = "bar-foo" 725 propagate_at_launch = true 726 } 727 } 728 `, name) 729 } 730 731 const testAccAWSAutoScalingGroupConfigWithLoadBalancer = ` 732 resource "aws_vpc" "foo" { 733 cidr_block = "10.1.0.0/16" 734 tags { Name = "tf-asg-test" } 735 } 736 737 resource "aws_internet_gateway" "gw" { 738 vpc_id = "${aws_vpc.foo.id}" 739 } 740 741 resource "aws_subnet" "foo" { 742 cidr_block = "10.1.1.0/24" 743 vpc_id = "${aws_vpc.foo.id}" 744 } 745 746 resource "aws_security_group" "foo" { 747 vpc_id="${aws_vpc.foo.id}" 748 749 ingress { 750 protocol = "-1" 751 from_port = 0 752 to_port = 0 753 cidr_blocks = ["0.0.0.0/0"] 754 } 755 756 egress { 757 protocol = "-1" 758 from_port = 0 759 to_port = 0 760 cidr_blocks = ["0.0.0.0/0"] 761 } 762 } 763 764 resource "aws_elb" "bar" { 765 subnets = ["${aws_subnet.foo.id}"] 766 security_groups = ["${aws_security_group.foo.id}"] 767 768 listener { 769 instance_port = 80 770 instance_protocol = "http" 771 lb_port = 80 772 lb_protocol = "http" 773 } 774 775 health_check { 776 healthy_threshold = 2 777 unhealthy_threshold = 2 778 target = "HTTP:80/" 779 interval = 5 780 timeout = 2 781 } 782 783 depends_on = ["aws_internet_gateway.gw"] 784 } 785 786 resource "aws_launch_configuration" "foobar" { 787 // need an AMI that listens on :80 at boot, this is: 788 // bitnami-nginxstack-1.6.1-0-linux-ubuntu-14.04.1-x86_64-hvm-ebs-ami-99f5b1a9-3 789 image_id = "ami-b5b3fc85" 790 instance_type = "t2.micro" 791 security_groups = ["${aws_security_group.foo.id}"] 792 } 793 794 resource "aws_autoscaling_group" "bar" { 795 availability_zones = ["${aws_subnet.foo.availability_zone}"] 796 vpc_zone_identifier = ["${aws_subnet.foo.id}"] 797 max_size = 2 798 min_size = 2 799 health_check_grace_period = 300 800 health_check_type = "ELB" 801 wait_for_elb_capacity = 2 802 force_delete = true 803 804 launch_configuration = "${aws_launch_configuration.foobar.name}" 805 load_balancers = ["${aws_elb.bar.name}"] 806 } 807 ` 808 809 const testAccAWSAutoScalingGroupConfigWithAZ = ` 810 resource "aws_vpc" "default" { 811 cidr_block = "10.0.0.0/16" 812 tags { 813 Name = "terraform-test" 814 } 815 } 816 817 resource "aws_subnet" "main" { 818 vpc_id = "${aws_vpc.default.id}" 819 cidr_block = "10.0.1.0/24" 820 availability_zone = "us-west-2a" 821 tags { 822 Name = "terraform-test" 823 } 824 } 825 826 resource "aws_launch_configuration" "foobar" { 827 image_id = "ami-b5b3fc85" 828 instance_type = "t2.micro" 829 } 830 831 resource "aws_autoscaling_group" "bar" { 832 availability_zones = [ 833 "us-west-2a" 834 ] 835 desired_capacity = 0 836 max_size = 0 837 min_size = 0 838 launch_configuration = "${aws_launch_configuration.foobar.name}" 839 } 840 ` 841 842 const testAccAWSAutoScalingGroupConfigWithVPCIdent = ` 843 resource "aws_vpc" "default" { 844 cidr_block = "10.0.0.0/16" 845 tags { 846 Name = "terraform-test" 847 } 848 } 849 850 resource "aws_subnet" "main" { 851 vpc_id = "${aws_vpc.default.id}" 852 cidr_block = "10.0.1.0/24" 853 availability_zone = "us-west-2a" 854 tags { 855 Name = "terraform-test" 856 } 857 } 858 859 resource "aws_launch_configuration" "foobar" { 860 image_id = "ami-b5b3fc85" 861 instance_type = "t2.micro" 862 } 863 864 resource "aws_autoscaling_group" "bar" { 865 vpc_zone_identifier = [ 866 "${aws_subnet.main.id}", 867 ] 868 desired_capacity = 0 869 max_size = 0 870 min_size = 0 871 launch_configuration = "${aws_launch_configuration.foobar.name}" 872 } 873 ` 874 875 func testAccAWSAutoScalingGroupConfig_withPlacementGroup(name string) string { 876 return fmt.Sprintf(` 877 resource "aws_launch_configuration" "foobar" { 878 image_id = "ami-21f78e11" 879 instance_type = "c3.large" 880 } 881 882 resource "aws_placement_group" "test" { 883 name = "%s" 884 strategy = "cluster" 885 } 886 887 resource "aws_autoscaling_group" "bar" { 888 availability_zones = ["us-west-2a"] 889 name = "%s" 890 max_size = 1 891 min_size = 1 892 health_check_grace_period = 300 893 health_check_type = "ELB" 894 desired_capacity = 1 895 force_delete = true 896 termination_policies = ["OldestInstance","ClosestToNextInstanceHour"] 897 placement_group = "${aws_placement_group.test.name}" 898 899 launch_configuration = "${aws_launch_configuration.foobar.name}" 900 901 tag { 902 key = "Foo" 903 value = "foo-bar" 904 propagate_at_launch = true 905 } 906 } 907 `, name, name) 908 } 909 910 const testAccAWSAutoscalingMetricsCollectionConfig_allMetricsCollected = ` 911 resource "aws_launch_configuration" "foobar" { 912 image_id = "ami-21f78e11" 913 instance_type = "t1.micro" 914 } 915 916 resource "aws_autoscaling_group" "bar" { 917 availability_zones = ["us-west-2a"] 918 max_size = 1 919 min_size = 0 920 health_check_grace_period = 300 921 health_check_type = "EC2" 922 desired_capacity = 0 923 force_delete = true 924 termination_policies = ["OldestInstance","ClosestToNextInstanceHour"] 925 launch_configuration = "${aws_launch_configuration.foobar.name}" 926 enabled_metrics = ["GroupTotalInstances", 927 "GroupPendingInstances", 928 "GroupTerminatingInstances", 929 "GroupDesiredCapacity", 930 "GroupInServiceInstances", 931 "GroupMinSize", 932 "GroupMaxSize" 933 ] 934 metrics_granularity = "1Minute" 935 } 936 ` 937 938 const testAccAWSAutoscalingMetricsCollectionConfig_updatingMetricsCollected = ` 939 resource "aws_launch_configuration" "foobar" { 940 image_id = "ami-21f78e11" 941 instance_type = "t1.micro" 942 } 943 944 resource "aws_autoscaling_group" "bar" { 945 availability_zones = ["us-west-2a"] 946 max_size = 1 947 min_size = 0 948 health_check_grace_period = 300 949 health_check_type = "EC2" 950 desired_capacity = 0 951 force_delete = true 952 termination_policies = ["OldestInstance","ClosestToNextInstanceHour"] 953 launch_configuration = "${aws_launch_configuration.foobar.name}" 954 enabled_metrics = ["GroupTotalInstances", 955 "GroupPendingInstances", 956 "GroupTerminatingInstances", 957 "GroupDesiredCapacity", 958 "GroupMaxSize" 959 ] 960 metrics_granularity = "1Minute" 961 } 962 ` 963 964 const testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_pre = ` 965 provider "aws" { 966 region = "us-west-2" 967 } 968 969 resource "aws_vpc" "default" { 970 cidr_block = "10.0.0.0/16" 971 972 tags { 973 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 974 } 975 } 976 977 resource "aws_alb_target_group" "test" { 978 name = "tf-example-alb-tg" 979 port = 80 980 protocol = "HTTP" 981 vpc_id = "${aws_vpc.default.id}" 982 } 983 984 resource "aws_subnet" "main" { 985 vpc_id = "${aws_vpc.default.id}" 986 cidr_block = "10.0.1.0/24" 987 availability_zone = "us-west-2a" 988 989 tags { 990 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 991 } 992 } 993 994 resource "aws_subnet" "alt" { 995 vpc_id = "${aws_vpc.default.id}" 996 cidr_block = "10.0.2.0/24" 997 availability_zone = "us-west-2b" 998 999 tags { 1000 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1001 } 1002 } 1003 1004 resource "aws_launch_configuration" "foobar" { 1005 # Golang-base from cts-hashi aws account, shared with tf testing account 1006 image_id = "ami-1817d178" 1007 instance_type = "t2.micro" 1008 enable_monitoring = false 1009 } 1010 1011 resource "aws_autoscaling_group" "bar" { 1012 vpc_zone_identifier = [ 1013 "${aws_subnet.main.id}", 1014 "${aws_subnet.alt.id}", 1015 ] 1016 1017 max_size = 2 1018 min_size = 0 1019 health_check_grace_period = 300 1020 health_check_type = "ELB" 1021 desired_capacity = 0 1022 force_delete = true 1023 termination_policies = ["OldestInstance"] 1024 launch_configuration = "${aws_launch_configuration.foobar.name}" 1025 1026 } 1027 1028 resource "aws_security_group" "tf_test_self" { 1029 name = "tf_test_alb_asg" 1030 description = "tf_test_alb_asg" 1031 vpc_id = "${aws_vpc.default.id}" 1032 1033 ingress { 1034 from_port = 80 1035 to_port = 80 1036 protocol = "tcp" 1037 cidr_blocks = ["0.0.0.0/0"] 1038 } 1039 1040 tags { 1041 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1042 } 1043 } 1044 ` 1045 1046 const testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_post = ` 1047 provider "aws" { 1048 region = "us-west-2" 1049 } 1050 1051 resource "aws_vpc" "default" { 1052 cidr_block = "10.0.0.0/16" 1053 1054 tags { 1055 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1056 } 1057 } 1058 1059 resource "aws_alb_target_group" "test" { 1060 name = "tf-example-alb-tg" 1061 port = 80 1062 protocol = "HTTP" 1063 vpc_id = "${aws_vpc.default.id}" 1064 } 1065 1066 resource "aws_subnet" "main" { 1067 vpc_id = "${aws_vpc.default.id}" 1068 cidr_block = "10.0.1.0/24" 1069 availability_zone = "us-west-2a" 1070 1071 tags { 1072 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1073 } 1074 } 1075 1076 resource "aws_subnet" "alt" { 1077 vpc_id = "${aws_vpc.default.id}" 1078 cidr_block = "10.0.2.0/24" 1079 availability_zone = "us-west-2b" 1080 1081 tags { 1082 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1083 } 1084 } 1085 1086 resource "aws_launch_configuration" "foobar" { 1087 # Golang-base from cts-hashi aws account, shared with tf testing account 1088 image_id = "ami-1817d178" 1089 instance_type = "t2.micro" 1090 enable_monitoring = false 1091 } 1092 1093 resource "aws_autoscaling_group" "bar" { 1094 vpc_zone_identifier = [ 1095 "${aws_subnet.main.id}", 1096 "${aws_subnet.alt.id}", 1097 ] 1098 1099 target_group_arns = ["${aws_alb_target_group.test.arn}"] 1100 1101 max_size = 2 1102 min_size = 0 1103 health_check_grace_period = 300 1104 health_check_type = "ELB" 1105 desired_capacity = 0 1106 force_delete = true 1107 termination_policies = ["OldestInstance"] 1108 launch_configuration = "${aws_launch_configuration.foobar.name}" 1109 1110 } 1111 1112 resource "aws_security_group" "tf_test_self" { 1113 name = "tf_test_alb_asg" 1114 description = "tf_test_alb_asg" 1115 vpc_id = "${aws_vpc.default.id}" 1116 1117 ingress { 1118 from_port = 80 1119 to_port = 80 1120 protocol = "tcp" 1121 cidr_blocks = ["0.0.0.0/0"] 1122 } 1123 1124 tags { 1125 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1126 } 1127 } 1128 ` 1129 1130 const testAccAWSAutoScalingGroupConfig_ALB_TargetGroup_post_duo = ` 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_alb_target_group" "test_more" { 1151 name = "tf-example-alb-tg-more" 1152 port = 80 1153 protocol = "HTTP" 1154 vpc_id = "${aws_vpc.default.id}" 1155 } 1156 1157 resource "aws_subnet" "main" { 1158 vpc_id = "${aws_vpc.default.id}" 1159 cidr_block = "10.0.1.0/24" 1160 availability_zone = "us-west-2a" 1161 1162 tags { 1163 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1164 } 1165 } 1166 1167 resource "aws_subnet" "alt" { 1168 vpc_id = "${aws_vpc.default.id}" 1169 cidr_block = "10.0.2.0/24" 1170 availability_zone = "us-west-2b" 1171 1172 tags { 1173 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1174 } 1175 } 1176 1177 resource "aws_launch_configuration" "foobar" { 1178 # Golang-base from cts-hashi aws account, shared with tf testing account 1179 image_id = "ami-1817d178" 1180 instance_type = "t2.micro" 1181 enable_monitoring = false 1182 } 1183 1184 resource "aws_autoscaling_group" "bar" { 1185 vpc_zone_identifier = [ 1186 "${aws_subnet.main.id}", 1187 "${aws_subnet.alt.id}", 1188 ] 1189 1190 target_group_arns = [ 1191 "${aws_alb_target_group.test.arn}", 1192 "${aws_alb_target_group.test_more.arn}", 1193 ] 1194 1195 max_size = 2 1196 min_size = 0 1197 health_check_grace_period = 300 1198 health_check_type = "ELB" 1199 desired_capacity = 0 1200 force_delete = true 1201 termination_policies = ["OldestInstance"] 1202 launch_configuration = "${aws_launch_configuration.foobar.name}" 1203 1204 } 1205 1206 resource "aws_security_group" "tf_test_self" { 1207 name = "tf_test_alb_asg" 1208 description = "tf_test_alb_asg" 1209 vpc_id = "${aws_vpc.default.id}" 1210 1211 ingress { 1212 from_port = 80 1213 to_port = 80 1214 protocol = "tcp" 1215 cidr_blocks = ["0.0.0.0/0"] 1216 } 1217 1218 tags { 1219 Name = "testAccAWSAutoScalingGroupConfig_ALB_TargetGroup" 1220 } 1221 } 1222 `