diff --git a/builtin/providers/aws/resource_aws_autoscaling_group.go b/builtin/providers/aws/resource_aws_autoscaling_group.go index 06036b248c37..183fc6618507 100644 --- a/builtin/providers/aws/resource_aws_autoscaling_group.go +++ b/builtin/providers/aws/resource_aws_autoscaling_group.go @@ -163,16 +163,93 @@ func resourceAwsAutoscalingGroup() *schema.Resource { Default: "1Minute", }, + "initial_lifecycle_hook": &schema.Schema{ + Type: schema.TypeSet, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + }, + "default_result": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "heartbeat_timeout": { + Type: schema.TypeInt, + Optional: true, + }, + "lifecycle_transition": { + Type: schema.TypeString, + Required: true, + }, + "notification_metadata": { + Type: schema.TypeString, + Optional: true, + }, + "notification_target_arn": { + Type: schema.TypeString, + Optional: true, + }, + "role_arn": { + Type: schema.TypeString, + Optional: true, + }, + }, + }, + }, + "tag": autoscalingTagsSchema(), }, } } +func generatePutLifecycleHookInputs(asgName string, cfgs []interface{}) []autoscaling.PutLifecycleHookInput { + res := make([]autoscaling.PutLifecycleHookInput, 0, len(cfgs)) + + for _, raw := range cfgs { + cfg := raw.(map[string]interface{}) + + input := autoscaling.PutLifecycleHookInput{ + AutoScalingGroupName: &asgName, + LifecycleHookName: aws.String(cfg["name"].(string)), + } + + if v, ok := cfg["default_result"]; ok && v.(string) != "" { + input.DefaultResult = aws.String(v.(string)) + } + + if v, ok := cfg["heartbeat_timeout"]; ok && v.(int) > 0 { + input.HeartbeatTimeout = aws.Int64(int64(v.(int))) + } + + if v, ok := cfg["lifecycle_transition"]; ok && v.(string) != "" { + input.LifecycleTransition = aws.String(v.(string)) + } + + if v, ok := cfg["notification_metadata"]; ok && v.(string) != "" { + input.NotificationMetadata = aws.String(v.(string)) + } + + if v, ok := cfg["notification_target_arn"]; ok && v.(string) != "" { + input.NotificationTargetARN = aws.String(v.(string)) + } + + if v, ok := cfg["role_arn"]; ok && v.(string) != "" { + input.RoleARN = aws.String(v.(string)) + } + + res = append(res, input) + } + + return res +} + func resourceAwsAutoscalingGroupCreate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).autoscalingconn - var autoScalingGroupOpts autoscaling.CreateAutoScalingGroupInput - var asgName string if v, ok := d.GetOk("name"); ok { asgName = v.(string) @@ -181,64 +258,101 @@ func resourceAwsAutoscalingGroupCreate(d *schema.ResourceData, meta interface{}) d.Set("name", asgName) } - autoScalingGroupOpts.AutoScalingGroupName = aws.String(asgName) - autoScalingGroupOpts.LaunchConfigurationName = aws.String(d.Get("launch_configuration").(string)) - autoScalingGroupOpts.MinSize = aws.Int64(int64(d.Get("min_size").(int))) - autoScalingGroupOpts.MaxSize = aws.Int64(int64(d.Get("max_size").(int))) + createOpts := autoscaling.CreateAutoScalingGroupInput{ + AutoScalingGroupName: aws.String(asgName), + LaunchConfigurationName: aws.String(d.Get("launch_configuration").(string)), + } + updateOpts := autoscaling.UpdateAutoScalingGroupInput{ + AutoScalingGroupName: aws.String(asgName), + } + + initialLifecycleHooks := d.Get("initial_lifecycle_hook").(*schema.Set).List() + twoPhases := len(initialLifecycleHooks) > 0 + + minSize := aws.Int64(int64(d.Get("min_size").(int))) + maxSize := aws.Int64(int64(d.Get("max_size").(int))) + + if twoPhases { + createOpts.MinSize = aws.Int64(int64(0)) + createOpts.MaxSize = aws.Int64(int64(0)) + + updateOpts.MinSize = minSize + updateOpts.MaxSize = maxSize + + if v, ok := d.GetOk("desired_capacity"); ok { + updateOpts.DesiredCapacity = aws.Int64(int64(v.(int))) + } + } else { + createOpts.MinSize = minSize + createOpts.MaxSize = maxSize + + if v, ok := d.GetOk("desired_capacity"); ok { + createOpts.DesiredCapacity = aws.Int64(int64(v.(int))) + } + } // Availability Zones are optional if VPC Zone Identifer(s) are specified if v, ok := d.GetOk("availability_zones"); ok && v.(*schema.Set).Len() > 0 { - autoScalingGroupOpts.AvailabilityZones = expandStringList(v.(*schema.Set).List()) + createOpts.AvailabilityZones = expandStringList(v.(*schema.Set).List()) } if v, ok := d.GetOk("tag"); ok { - autoScalingGroupOpts.Tags = autoscalingTagsFromMap( + createOpts.Tags = autoscalingTagsFromMap( setToMapByKey(v.(*schema.Set), "key"), d.Get("name").(string)) } if v, ok := d.GetOk("default_cooldown"); ok { - autoScalingGroupOpts.DefaultCooldown = aws.Int64(int64(v.(int))) + createOpts.DefaultCooldown = aws.Int64(int64(v.(int))) } if v, ok := d.GetOk("health_check_type"); ok && v.(string) != "" { - autoScalingGroupOpts.HealthCheckType = aws.String(v.(string)) - } - - if v, ok := d.GetOk("desired_capacity"); ok { - autoScalingGroupOpts.DesiredCapacity = aws.Int64(int64(v.(int))) + createOpts.HealthCheckType = aws.String(v.(string)) } if v, ok := d.GetOk("health_check_grace_period"); ok { - autoScalingGroupOpts.HealthCheckGracePeriod = aws.Int64(int64(v.(int))) + createOpts.HealthCheckGracePeriod = aws.Int64(int64(v.(int))) } if v, ok := d.GetOk("placement_group"); ok { - autoScalingGroupOpts.PlacementGroup = aws.String(v.(string)) + createOpts.PlacementGroup = aws.String(v.(string)) } if v, ok := d.GetOk("load_balancers"); ok && v.(*schema.Set).Len() > 0 { - autoScalingGroupOpts.LoadBalancerNames = expandStringList( + createOpts.LoadBalancerNames = expandStringList( v.(*schema.Set).List()) } if v, ok := d.GetOk("vpc_zone_identifier"); ok && v.(*schema.Set).Len() > 0 { - autoScalingGroupOpts.VPCZoneIdentifier = expandVpcZoneIdentifiers(v.(*schema.Set).List()) + createOpts.VPCZoneIdentifier = expandVpcZoneIdentifiers(v.(*schema.Set).List()) } if v, ok := d.GetOk("termination_policies"); ok && len(v.([]interface{})) > 0 { - autoScalingGroupOpts.TerminationPolicies = expandStringList(v.([]interface{})) + createOpts.TerminationPolicies = expandStringList(v.([]interface{})) } - log.Printf("[DEBUG] AutoScaling Group create configuration: %#v", autoScalingGroupOpts) - _, err := conn.CreateAutoScalingGroup(&autoScalingGroupOpts) + log.Printf("[DEBUG] AutoScaling Group create configuration: %#v", createOpts) + _, err := conn.CreateAutoScalingGroup(&createOpts) if err != nil { - return fmt.Errorf("Error creating Autoscaling Group: %s", err) + return fmt.Errorf("Error creating AutoScaling Group: %s", err) } d.SetId(d.Get("name").(string)) log.Printf("[INFO] AutoScaling Group ID: %s", d.Id()) - if err := waitForASGCapacity(d, meta, capacitySatifiedCreate); err != nil { + if twoPhases { + for _, hook := range generatePutLifecycleHookInputs(asgName, initialLifecycleHooks) { + if err = resourceAwsAutoscalingLifecycleHookPutOp(conn, &hook); err != nil { + return fmt.Errorf("Error creating initial lifecycle hooks: %s", err) + } + } + + _, err = conn.UpdateAutoScalingGroup(&updateOpts) + if err != nil { + return fmt.Errorf("Error setting AutoScaling Group initial capacity: %s", err) + } + } + + if err := waitForASGCapacity(d, meta, capacitySatisfiedCreate); err != nil { return err } @@ -413,7 +527,7 @@ func resourceAwsAutoscalingGroupUpdate(d *schema.ResourceData, meta interface{}) } if shouldWaitForCapacity { - waitForASGCapacity(d, meta, capacitySatifiedUpdate) + waitForASGCapacity(d, meta, capacitySatisfiedUpdate) } if d.HasChange("enabled_metrics") { diff --git a/builtin/providers/aws/resource_aws_autoscaling_group_waiting.go b/builtin/providers/aws/resource_aws_autoscaling_group_waiting.go index bfafa9b3364f..80f126983bd5 100644 --- a/builtin/providers/aws/resource_aws_autoscaling_group_waiting.go +++ b/builtin/providers/aws/resource_aws_autoscaling_group_waiting.go @@ -93,8 +93,8 @@ func waitForASGCapacity( type capacitySatisfiedFunc func(*schema.ResourceData, int, int) (bool, string) -// capacitySatifiedCreate treats all targets as minimums -func capacitySatifiedCreate(d *schema.ResourceData, haveASG, haveELB int) (bool, string) { +// capacitySatisfiedCreate treats all targets as minimums +func capacitySatisfiedCreate(d *schema.ResourceData, haveASG, haveELB int) (bool, string) { minASG := d.Get("min_size").(int) if wantASG := d.Get("desired_capacity").(int); wantASG > 0 { minASG = wantASG @@ -114,8 +114,8 @@ func capacitySatifiedCreate(d *schema.ResourceData, haveASG, haveELB int) (bool, return true, "" } -// capacitySatifiedUpdate only cares about specific targets -func capacitySatifiedUpdate(d *schema.ResourceData, haveASG, haveELB int) (bool, string) { +// capacitySatisfiedUpdate only cares about specific targets +func capacitySatisfiedUpdate(d *schema.ResourceData, haveASG, haveELB int) (bool, string) { if wantASG := d.Get("desired_capacity").(int); wantASG > 0 { if haveASG != wantASG { return false, fmt.Sprintf( diff --git a/builtin/providers/aws/resource_aws_autoscaling_group_waiting_test.go b/builtin/providers/aws/resource_aws_autoscaling_group_waiting_test.go index d7c3895c0e17..fa27102e8080 100644 --- a/builtin/providers/aws/resource_aws_autoscaling_group_waiting_test.go +++ b/builtin/providers/aws/resource_aws_autoscaling_group_waiting_test.go @@ -127,7 +127,7 @@ func TestCapacitySatisfiedCreate(t *testing.T) { t.Fatalf("err: %s", err) } } - gotSatisfied, gotReason := capacitySatifiedCreate(d, tc.HaveASG, tc.HaveELB) + gotSatisfied, gotReason := capacitySatisfiedCreate(d, tc.HaveASG, tc.HaveELB) if gotSatisfied != tc.ExpectSatisfied { t.Fatalf("%s: expected satisfied: %t, got: %t (reason: %s)", @@ -209,7 +209,7 @@ func TestCapacitySatisfiedUpdate(t *testing.T) { t.Fatalf("err: %s", err) } } - gotSatisfied, gotReason := capacitySatifiedUpdate(d, tc.HaveASG, tc.HaveELB) + gotSatisfied, gotReason := capacitySatisfiedUpdate(d, tc.HaveASG, tc.HaveELB) if gotSatisfied != tc.ExpectSatisfied { t.Fatalf("%s: expected satisfied: %t, got: %t (reason: %s)", diff --git a/builtin/providers/aws/resource_aws_autoscaling_lifecycle_hook.go b/builtin/providers/aws/resource_aws_autoscaling_lifecycle_hook.go index 8b3a3159d66d..7743c1ad3616 100644 --- a/builtin/providers/aws/resource_aws_autoscaling_lifecycle_hook.go +++ b/builtin/providers/aws/resource_aws_autoscaling_lifecycle_hook.go @@ -21,37 +21,37 @@ func resourceAwsAutoscalingLifecycleHook() *schema.Resource { Delete: resourceAwsAutoscalingLifecycleHookDelete, Schema: map[string]*schema.Schema{ - "name": &schema.Schema{ + "name": { Type: schema.TypeString, Required: true, ForceNew: true, }, - "autoscaling_group_name": &schema.Schema{ + "autoscaling_group_name": { Type: schema.TypeString, Required: true, }, - "default_result": &schema.Schema{ + "default_result": { Type: schema.TypeString, Optional: true, Computed: true, }, - "heartbeat_timeout": &schema.Schema{ + "heartbeat_timeout": { Type: schema.TypeInt, Optional: true, }, - "lifecycle_transition": &schema.Schema{ + "lifecycle_transition": { Type: schema.TypeString, Required: true, }, - "notification_metadata": &schema.Schema{ + "notification_metadata": { Type: schema.TypeString, Optional: true, }, - "notification_target_arn": &schema.Schema{ + "notification_target_arn": { Type: schema.TypeString, Optional: true, }, - "role_arn": &schema.Schema{ + "role_arn": { Type: schema.TypeString, Optional: true, }, @@ -59,13 +59,10 @@ func resourceAwsAutoscalingLifecycleHook() *schema.Resource { } } -func resourceAwsAutoscalingLifecycleHookPut(d *schema.ResourceData, meta interface{}) error { - conn := meta.(*AWSClient).autoscalingconn - params := getAwsAutoscalingPutLifecycleHookInput(d) - +func resourceAwsAutoscalingLifecycleHookPutOp(conn *autoscaling.AutoScaling, params *autoscaling.PutLifecycleHookInput) error { log.Printf("[DEBUG] AutoScaling PutLifecyleHook: %s", params) - err := resource.Retry(5*time.Minute, func() *resource.RetryError { - _, err := conn.PutLifecycleHook(¶ms) + return resource.Retry(5*time.Minute, func() *resource.RetryError { + _, err := conn.PutLifecycleHook(params) if err != nil { if awsErr, ok := err.(awserr.Error); ok { @@ -77,8 +74,13 @@ func resourceAwsAutoscalingLifecycleHookPut(d *schema.ResourceData, meta interfa } return nil }) +} - if err != nil { +func resourceAwsAutoscalingLifecycleHookPut(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).autoscalingconn + params := getAwsAutoscalingPutLifecycleHookInput(d) + + if err := resourceAwsAutoscalingLifecycleHookPutOp(conn, ¶ms); err != nil { return err }