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