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