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