func getAwsAutoscalingGroup( asgName string, conn *autoscaling.AutoScaling) (*autoscaling.Group, error) { describeOpts := autoscaling.DescribeAutoScalingGroupsInput{ AutoScalingGroupNames: []*string{aws.String(asgName)}, } log.Printf("[DEBUG] AutoScaling Group describe configuration: %#v", describeOpts) describeGroups, err := conn.DescribeAutoScalingGroups(&describeOpts) if err != nil { autoscalingerr, ok := err.(awserr.Error) if ok && autoscalingerr.Code() == "InvalidGroup.NotFound" { return nil, nil } return nil, fmt.Errorf("Error retrieving AutoScaling groups: %s", err) } // Search for the autoscaling group for idx, asc := range describeGroups.AutoScalingGroups { if *asc.AutoScalingGroupName == asgName { return describeGroups.AutoScalingGroups[idx], nil } } return nil, nil }
// setTags is a helper to set the tags for a resource. It expects the // tags field to be named "tag" func setAutoscalingTags(conn *autoscaling.AutoScaling, d *schema.ResourceData) error { if d.HasChange("tag") { oraw, nraw := d.GetChange("tag") o := setToMapByKey(oraw.(*schema.Set), "key") n := setToMapByKey(nraw.(*schema.Set), "key") resourceID := d.Get("name").(string) c, r := diffAutoscalingTags( autoscalingTagsFromMap(o, resourceID), autoscalingTagsFromMap(n, resourceID), resourceID) create := autoscaling.CreateOrUpdateTagsInput{ Tags: c, } remove := autoscaling.DeleteTagsInput{ Tags: r, } // Set tags if len(r) > 0 { log.Printf("[DEBUG] Removing autoscaling tags: %#v", r) if _, err := conn.DeleteTags(&remove); err != nil { return err } } if len(c) > 0 { log.Printf("[DEBUG] Creating autoscaling tags: %#v", c) if _, err := conn.CreateOrUpdateTags(&create); err != nil { return err } } } return nil }
func enableASGSuspendedProcesses(d *schema.ResourceData, conn *autoscaling.AutoScaling) error { props := &autoscaling.ScalingProcessQuery{ AutoScalingGroupName: aws.String(d.Id()), ScalingProcesses: expandStringList(d.Get("suspended_processes").(*schema.Set).List()), } _, err := conn.SuspendProcesses(props) if err != nil { return err } return nil }
func removeNotificationConfigToGroupsWithTopic(conn *autoscaling.AutoScaling, groups []*string, topic string) error { for _, r := range groups { opts := &autoscaling.DeleteNotificationConfigurationInput{ AutoScalingGroupName: r, TopicARN: aws.String(topic), } _, err := conn.DeleteNotificationConfiguration(opts) if err != nil { return fmt.Errorf("[WARN] Error deleting notification configuration for ASG \"%s\", Topic ARN \"%s\"", *r, topic) } } return nil }
func enableASGMetricsCollection(d *schema.ResourceData, conn *autoscaling.AutoScaling) error { props := &autoscaling.EnableMetricsCollectionInput{ AutoScalingGroupName: aws.String(d.Id()), Granularity: aws.String(d.Get("metrics_granularity").(string)), Metrics: expandStringList(d.Get("enabled_metrics").(*schema.Set).List()), } log.Printf("[INFO] Enabling metrics collection for the ASG: %s", d.Id()) _, metricsErr := conn.EnableMetricsCollection(props) if metricsErr != nil { return metricsErr } return nil }
func resourceAwsAutoscalingLifecycleHookPutOp(conn *autoscaling.AutoScaling, params *autoscaling.PutLifecycleHookInput) error { log.Printf("[DEBUG] AutoScaling PutLifecyleHook: %s", params) return resource.Retry(5*time.Minute, func() *resource.RetryError { _, err := conn.PutLifecycleHook(params) if err != nil { if awsErr, ok := err.(awserr.Error); ok { if strings.Contains(awsErr.Message(), "Unable to publish test message to notification target") { return resource.RetryableError(fmt.Errorf("[DEBUG] Retrying AWS AutoScaling Lifecycle Hook: %s", params)) } } return resource.NonRetryableError(fmt.Errorf("Error putting lifecycle hook: %s", err)) } return nil }) }
func terminateASG(api *autoscaling.AutoScaling, instance *autoscaling.Group, wg *sync.WaitGroup) error { defer wg.Done() // 1. set the number of instances to 0 _, err := api.UpdateAutoScalingGroup(&autoscaling.UpdateAutoScalingGroupInput{ AutoScalingGroupName: instance.AutoScalingGroupName, DesiredCapacity: aws.Int64(0), MaxSize: aws.Int64(0), MinSize: aws.Int64(0), }) if err != nil { return err } // 2. wait until the instance count is 0 for { instances, err := api.DescribeAutoScalingGroups(&autoscaling.DescribeAutoScalingGroupsInput{ AutoScalingGroupNames: []*string{instance.AutoScalingGroupName}, }) if err != nil { return err } // instance already deleted? we're done if len(instances.AutoScalingGroups) == 0 { return nil } // no instances remaining in asg? break if v := instances.AutoScalingGroups[0]; len(v.Instances) == 0 { break } time.Sleep(15 * time.Second) } // 3. terminate the asg log.Printf("deleting autoscaling group, %s\n", *instance.AutoScalingGroupName) _, err = api.DeleteAutoScalingGroup(&autoscaling.DeleteAutoScalingGroupInput{ AutoScalingGroupName: instance.AutoScalingGroupName, ForceDelete: aws.Bool(true), }) if err != nil { return err } log.Printf("deleting launch configuration, %s\n", *instance.LaunchConfigurationName) _, err = api.DeleteLaunchConfiguration(&autoscaling.DeleteLaunchConfigurationInput{ LaunchConfigurationName: instance.LaunchConfigurationName, }) if err != nil { return err } return nil }
func addNotificationConfigToGroupsWithTopic(conn *autoscaling.AutoScaling, groups []*string, nl []*string, topic string) error { for _, a := range groups { opts := &autoscaling.PutNotificationConfigurationInput{ AutoScalingGroupName: a, NotificationTypes: nl, TopicARN: aws.String(topic), } _, err := conn.PutNotificationConfiguration(opts) if err != nil { if awsErr, ok := err.(awserr.Error); ok { return fmt.Errorf("[WARN] Error creating Autoscaling Group Notification for Group %s, error: \"%s\", code: \"%s\"", *a, awsErr.Message(), awsErr.Code()) } return err } } return nil }
func updateASGMetricsCollection(d *schema.ResourceData, conn *autoscaling.AutoScaling) error { o, n := d.GetChange("enabled_metrics") if o == nil { o = new(schema.Set) } if n == nil { n = new(schema.Set) } os := o.(*schema.Set) ns := n.(*schema.Set) disableMetrics := os.Difference(ns) if disableMetrics.Len() != 0 { props := &autoscaling.DisableMetricsCollectionInput{ AutoScalingGroupName: aws.String(d.Id()), Metrics: expandStringList(disableMetrics.List()), } _, err := conn.DisableMetricsCollection(props) if err != nil { return fmt.Errorf("Failure to Disable metrics collection types for ASG %s: %s", d.Id(), err) } } enabledMetrics := ns.Difference(os) if enabledMetrics.Len() != 0 { props := &autoscaling.EnableMetricsCollectionInput{ AutoScalingGroupName: aws.String(d.Id()), Metrics: expandStringList(enabledMetrics.List()), Granularity: aws.String(d.Get("metrics_granularity").(string)), } _, err := conn.EnableMetricsCollection(props) if err != nil { return fmt.Errorf("Failure to Enable metrics collection types for ASG %s: %s", d.Id(), err) } } return nil }
func updateASGSuspendedProcesses(d *schema.ResourceData, conn *autoscaling.AutoScaling) error { o, n := d.GetChange("suspended_processes") if o == nil { o = new(schema.Set) } if n == nil { n = new(schema.Set) } os := o.(*schema.Set) ns := n.(*schema.Set) resumeProcesses := os.Difference(ns) if resumeProcesses.Len() != 0 { props := &autoscaling.ScalingProcessQuery{ AutoScalingGroupName: aws.String(d.Id()), ScalingProcesses: expandStringList(resumeProcesses.List()), } _, err := conn.ResumeProcesses(props) if err != nil { return fmt.Errorf("Error Resuming Processes for ASG %q: %s", d.Id(), err) } } suspendedProcesses := ns.Difference(os) if suspendedProcesses.Len() != 0 { props := &autoscaling.ScalingProcessQuery{ AutoScalingGroupName: aws.String(d.Id()), ScalingProcesses: expandStringList(suspendedProcesses.List()), } _, err := conn.SuspendProcesses(props) if err != nil { return fmt.Errorf("Error Suspending Processes for ASG %q: %s", d.Id(), err) } } return nil }
func findASGInstancesToTerminate(api *autoscaling.AutoScaling, opts Options) ([]*autoscaling.Group, error) { groups, err := api.DescribeAutoScalingGroups(&autoscaling.DescribeAutoScalingGroupsInput{}) if err != nil { return nil, err } instances := []*autoscaling.Group{} for _, asg := range groups.AutoScalingGroups { if !hasASGTags(asg, "app", opts.App) { continue } if !hasASGTags(asg, "env", opts.Env) { continue } if hasASGTags(asg, "version", opts.Exclude) { continue } instances = append(instances, asg) } return instances, nil }