2014-07-10 01:00:11 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2014-12-12 23:21:20 +01:00
|
|
|
"strings"
|
2014-10-18 05:10:52 +02:00
|
|
|
"time"
|
2014-07-10 01:00:11 +02:00
|
|
|
|
2014-10-10 23:34:40 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/hashcode"
|
2014-10-18 05:10:52 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2014-10-10 23:34:40 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2015-02-20 15:55:54 +01:00
|
|
|
|
|
|
|
"github.com/awslabs/aws-sdk-go/aws"
|
|
|
|
"github.com/awslabs/aws-sdk-go/gen/autoscaling"
|
2014-07-10 01:00:11 +02:00
|
|
|
)
|
|
|
|
|
2014-10-10 23:34:40 +02:00
|
|
|
func resourceAwsAutoscalingGroup() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsAutoscalingGroupCreate,
|
|
|
|
Read: resourceAwsAutoscalingGroupRead,
|
|
|
|
Update: resourceAwsAutoscalingGroupUpdate,
|
|
|
|
Delete: resourceAwsAutoscalingGroupDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"launch_configuration": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"desired_capacity": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"min_size": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"max_size": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"default_cooldown": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"force_delete": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"health_check_grace_period": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"health_check_type": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"availability_zones": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Set: func(v interface{}) int {
|
|
|
|
return hashcode.String(v.(string))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"load_balancers": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Set: func(v interface{}) int {
|
|
|
|
return hashcode.String(v.(string))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"vpc_zone_identifier": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Set: func(v interface{}) int {
|
|
|
|
return hashcode.String(v.(string))
|
|
|
|
},
|
|
|
|
},
|
2014-10-23 23:58:54 +02:00
|
|
|
|
|
|
|
"termination_policies": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Set: func(v interface{}) int {
|
|
|
|
return hashcode.String(v.(string))
|
|
|
|
},
|
|
|
|
},
|
2014-10-10 23:34:40 +02:00
|
|
|
},
|
2014-07-10 01:00:11 +02:00
|
|
|
}
|
2014-10-10 23:34:40 +02:00
|
|
|
}
|
2014-07-10 01:00:11 +02:00
|
|
|
|
2014-10-10 23:34:40 +02:00
|
|
|
func resourceAwsAutoscalingGroupCreate(d *schema.ResourceData, meta interface{}) error {
|
2015-02-20 18:28:20 +01:00
|
|
|
autoscalingconn := meta.(*AWSClient).awsAutoScalingconn
|
2014-07-10 01:00:11 +02:00
|
|
|
|
2015-02-20 15:55:54 +01:00
|
|
|
var autoScalingGroupOpts autoscaling.CreateAutoScalingGroupType
|
|
|
|
autoScalingGroupOpts.AutoScalingGroupName = aws.String(d.Get("name").(string))
|
|
|
|
autoScalingGroupOpts.HealthCheckType = aws.String(d.Get("health_check_type").(string))
|
|
|
|
autoScalingGroupOpts.LaunchConfigurationName = aws.String(d.Get("launch_configuration").(string))
|
|
|
|
autoScalingGroupOpts.MinSize = aws.Integer(d.Get("min_size").(int))
|
|
|
|
autoScalingGroupOpts.MaxSize = aws.Integer(d.Get("max_size").(int))
|
|
|
|
autoScalingGroupOpts.AvailabilityZones = expandStringList(
|
2014-10-10 23:34:40 +02:00
|
|
|
d.Get("availability_zones").(*schema.Set).List())
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("default_cooldown"); ok {
|
2015-02-20 15:55:54 +01:00
|
|
|
autoScalingGroupOpts.DefaultCooldown = aws.Integer(v.(int))
|
2014-07-10 01:00:11 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 23:34:40 +02:00
|
|
|
if v, ok := d.GetOk("desired_capacity"); ok {
|
2015-02-20 15:55:54 +01:00
|
|
|
autoScalingGroupOpts.DesiredCapacity = aws.Integer(v.(int))
|
2014-07-10 01:00:11 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 23:34:40 +02:00
|
|
|
if v, ok := d.GetOk("health_check_grace_period"); ok {
|
2015-02-20 15:55:54 +01:00
|
|
|
autoScalingGroupOpts.HealthCheckGracePeriod = aws.Integer(v.(int))
|
2014-07-10 01:00:11 +02:00
|
|
|
}
|
|
|
|
|
2015-02-18 01:12:02 +01:00
|
|
|
if v, ok := d.GetOk("load_balancers"); ok && v.(*schema.Set).Len() > 0 {
|
2014-10-10 23:34:40 +02:00
|
|
|
autoScalingGroupOpts.LoadBalancerNames = expandStringList(
|
|
|
|
v.(*schema.Set).List())
|
2014-07-10 01:00:11 +02:00
|
|
|
}
|
|
|
|
|
2015-02-18 01:12:02 +01:00
|
|
|
if v, ok := d.GetOk("vpc_zone_identifier"); ok && v.(*schema.Set).Len() > 0 {
|
2015-02-20 15:55:54 +01:00
|
|
|
exp := expandStringList(v.(*schema.Set).List())
|
2015-02-20 21:27:48 +01:00
|
|
|
autoScalingGroupOpts.VPCZoneIdentifier = aws.String(strings.Join(exp, ","))
|
2014-07-10 01:00:11 +02:00
|
|
|
}
|
2014-12-10 02:11:50 +01:00
|
|
|
|
2015-02-18 01:12:02 +01:00
|
|
|
if v, ok := d.GetOk("termination_policies"); ok && v.(*schema.Set).Len() > 0 {
|
2014-10-23 23:58:54 +02:00
|
|
|
autoScalingGroupOpts.TerminationPolicies = expandStringList(
|
|
|
|
v.(*schema.Set).List())
|
|
|
|
}
|
2014-07-10 01:00:11 +02:00
|
|
|
|
2014-07-14 17:36:25 +02:00
|
|
|
log.Printf("[DEBUG] AutoScaling Group create configuration: %#v", autoScalingGroupOpts)
|
2015-02-20 15:55:54 +01:00
|
|
|
err := autoscalingconn.CreateAutoScalingGroup(&autoScalingGroupOpts)
|
2014-07-10 01:00:11 +02:00
|
|
|
if err != nil {
|
2014-10-10 23:34:40 +02:00
|
|
|
return fmt.Errorf("Error creating Autoscaling Group: %s", err)
|
2014-07-10 01:00:11 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 23:34:40 +02:00
|
|
|
d.SetId(d.Get("name").(string))
|
|
|
|
log.Printf("[INFO] AutoScaling Group ID: %s", d.Id())
|
2014-07-10 01:00:11 +02:00
|
|
|
|
2014-10-10 23:34:40 +02:00
|
|
|
return resourceAwsAutoscalingGroupRead(d, meta)
|
2014-07-10 01:00:11 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
func resourceAwsAutoscalingGroupRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
g, err := getAwsAutoscalingGroup(d, meta)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if g == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("availability_zones", g.AvailabilityZones)
|
2015-02-20 21:27:48 +01:00
|
|
|
d.Set("default_cooldown", *g.DefaultCooldown)
|
|
|
|
d.Set("desired_capacity", *g.DesiredCapacity)
|
|
|
|
d.Set("health_check_grace_period", *g.HealthCheckGracePeriod)
|
|
|
|
d.Set("health_check_type", *g.HealthCheckType)
|
|
|
|
d.Set("launch_configuration", *g.LaunchConfigurationName)
|
2014-11-21 17:58:34 +01:00
|
|
|
d.Set("load_balancers", g.LoadBalancerNames)
|
2015-02-20 21:27:48 +01:00
|
|
|
d.Set("min_size", *g.MinSize)
|
|
|
|
d.Set("max_size", *g.MaxSize)
|
|
|
|
d.Set("name", *g.AutoScalingGroupName)
|
2015-02-20 15:55:54 +01:00
|
|
|
d.Set("vpc_zone_identifier", strings.Split(*g.VPCZoneIdentifier, ","))
|
2015-02-04 17:00:03 +01:00
|
|
|
d.Set("termination_policies", g.TerminationPolicies)
|
2014-11-21 17:58:34 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-10 23:34:40 +02:00
|
|
|
func resourceAwsAutoscalingGroupUpdate(d *schema.ResourceData, meta interface{}) error {
|
2015-02-20 18:28:20 +01:00
|
|
|
autoscalingconn := meta.(*AWSClient).awsAutoScalingconn
|
2014-07-10 01:00:11 +02:00
|
|
|
|
2015-02-20 15:55:54 +01:00
|
|
|
opts := autoscaling.UpdateAutoScalingGroupType{
|
|
|
|
AutoScalingGroupName: aws.String(d.Id()),
|
2014-07-15 23:20:54 +02:00
|
|
|
}
|
|
|
|
|
2014-10-11 01:25:23 +02:00
|
|
|
if d.HasChange("desired_capacity") {
|
2015-02-20 15:55:54 +01:00
|
|
|
opts.DesiredCapacity = aws.Integer(d.Get("desired_capacity").(int))
|
2014-10-11 01:25:23 +02:00
|
|
|
}
|
|
|
|
|
2015-02-18 00:48:15 +01:00
|
|
|
if d.HasChange("launch_configuration") {
|
2015-02-20 15:55:54 +01:00
|
|
|
opts.LaunchConfigurationName = aws.String(d.Get("launch_configuration").(string))
|
2015-02-18 00:48:15 +01:00
|
|
|
}
|
|
|
|
|
2014-10-10 23:34:40 +02:00
|
|
|
if d.HasChange("min_size") {
|
2015-02-20 15:55:54 +01:00
|
|
|
opts.MinSize = aws.Integer(d.Get("min_size").(int))
|
2014-07-15 23:20:54 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 23:34:40 +02:00
|
|
|
if d.HasChange("max_size") {
|
2015-02-20 15:55:54 +01:00
|
|
|
opts.MaxSize = aws.Integer(d.Get("max_size").(int))
|
2014-07-15 23:20:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] AutoScaling Group update configuration: %#v", opts)
|
2015-02-20 15:55:54 +01:00
|
|
|
err := autoscalingconn.UpdateAutoScalingGroup(&opts)
|
2014-07-15 23:20:54 +02:00
|
|
|
if err != nil {
|
2014-10-10 23:34:40 +02:00
|
|
|
d.Partial(true)
|
|
|
|
return fmt.Errorf("Error updating Autoscaling group: %s", err)
|
2014-07-15 23:20:54 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 23:34:40 +02:00
|
|
|
return resourceAwsAutoscalingGroupRead(d, meta)
|
2014-07-10 01:00:11 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 23:34:40 +02:00
|
|
|
func resourceAwsAutoscalingGroupDelete(d *schema.ResourceData, meta interface{}) error {
|
2015-02-20 18:28:20 +01:00
|
|
|
autoscalingconn := meta.(*AWSClient).awsAutoScalingconn
|
2014-07-10 01:00:11 +02:00
|
|
|
|
2014-10-18 05:10:52 +02:00
|
|
|
// Read the autoscaling group first. If it doesn't exist, we're done.
|
|
|
|
// We need the group in order to check if there are instances attached.
|
|
|
|
// If so, we need to remove those first.
|
|
|
|
g, err := getAwsAutoscalingGroup(d, meta)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if g == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2015-02-20 15:55:54 +01:00
|
|
|
if len(g.Instances) > 0 || *g.DesiredCapacity > 0 {
|
2014-10-18 05:10:52 +02:00
|
|
|
if err := resourceAwsAutoscalingGroupDrain(d, meta); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-10 23:34:40 +02:00
|
|
|
log.Printf("[DEBUG] AutoScaling Group destroy: %v", d.Id())
|
2015-02-20 15:55:54 +01:00
|
|
|
deleteopts := autoscaling.DeleteAutoScalingGroupType{AutoScalingGroupName: aws.String(d.Id())}
|
2014-07-14 17:36:25 +02:00
|
|
|
|
|
|
|
// You can force an autoscaling group to delete
|
|
|
|
// even if it's in the process of scaling a resource.
|
|
|
|
// Normally, you would set the min-size and max-size to 0,0
|
|
|
|
// and then delete the group. This bypasses that and leaves
|
|
|
|
// resources potentially dangling.
|
2014-10-10 23:34:40 +02:00
|
|
|
if d.Get("force_delete").(bool) {
|
2015-02-20 15:55:54 +01:00
|
|
|
deleteopts.ForceDelete = aws.Boolean(true)
|
2014-07-14 17:36:25 +02:00
|
|
|
}
|
|
|
|
|
2015-02-20 15:55:54 +01:00
|
|
|
if err := autoscalingconn.DeleteAutoScalingGroup(&deleteopts); err != nil {
|
|
|
|
autoscalingerr, ok := err.(aws.APIError)
|
2014-07-10 21:41:06 +02:00
|
|
|
if ok && autoscalingerr.Code == "InvalidGroup.NotFound" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2014-07-10 01:00:11 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-18 05:10:52 +02:00
|
|
|
func getAwsAutoscalingGroup(
|
|
|
|
d *schema.ResourceData,
|
|
|
|
meta interface{}) (*autoscaling.AutoScalingGroup, error) {
|
2015-02-20 18:28:20 +01:00
|
|
|
autoscalingconn := meta.(*AWSClient).awsAutoScalingconn
|
2014-07-10 01:00:11 +02:00
|
|
|
|
2015-02-20 15:55:54 +01:00
|
|
|
describeOpts := autoscaling.AutoScalingGroupNamesType{
|
|
|
|
AutoScalingGroupNames: []string{d.Id()},
|
2014-07-10 01:00:11 +02:00
|
|
|
}
|
|
|
|
|
2014-07-14 17:36:25 +02:00
|
|
|
log.Printf("[DEBUG] AutoScaling Group describe configuration: %#v", describeOpts)
|
2014-07-10 01:00:11 +02:00
|
|
|
describeGroups, err := autoscalingconn.DescribeAutoScalingGroups(&describeOpts)
|
|
|
|
if err != nil {
|
2015-02-20 15:55:54 +01:00
|
|
|
autoscalingerr, ok := err.(aws.APIError)
|
2014-10-10 23:34:40 +02:00
|
|
|
if ok && autoscalingerr.Code == "InvalidGroup.NotFound" {
|
|
|
|
d.SetId("")
|
2014-10-18 05:10:52 +02:00
|
|
|
return nil, nil
|
2014-10-10 23:34:40 +02:00
|
|
|
}
|
|
|
|
|
2014-10-18 05:10:52 +02:00
|
|
|
return nil, fmt.Errorf("Error retrieving AutoScaling groups: %s", err)
|
2014-07-10 01:00:11 +02:00
|
|
|
}
|
|
|
|
|
2014-12-10 02:11:50 +01:00
|
|
|
// Search for the autoscaling group
|
|
|
|
for idx, asc := range describeGroups.AutoScalingGroups {
|
2015-02-20 18:28:20 +01:00
|
|
|
if *asc.AutoScalingGroupName == d.Id() {
|
2014-12-10 02:11:50 +01:00
|
|
|
return &describeGroups.AutoScalingGroups[idx], nil
|
2014-07-10 01:00:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-10 02:11:50 +01:00
|
|
|
// ASG not found
|
|
|
|
d.SetId("")
|
|
|
|
return nil, nil
|
2014-10-18 05:10:52 +02:00
|
|
|
}
|
2014-07-10 01:00:11 +02:00
|
|
|
|
2014-10-18 05:10:52 +02:00
|
|
|
func resourceAwsAutoscalingGroupDrain(d *schema.ResourceData, meta interface{}) error {
|
2015-02-20 18:28:20 +01:00
|
|
|
autoscalingconn := meta.(*AWSClient).awsAutoScalingconn
|
2014-07-15 18:31:49 +02:00
|
|
|
|
2014-10-18 05:10:52 +02:00
|
|
|
// First, set the capacity to zero so the group will drain
|
|
|
|
log.Printf("[DEBUG] Reducing autoscaling group capacity to zero")
|
2015-02-20 18:28:20 +01:00
|
|
|
opts := autoscaling.UpdateAutoScalingGroupType{
|
|
|
|
AutoScalingGroupName: aws.String(d.Id()),
|
|
|
|
DesiredCapacity: aws.Integer(0),
|
|
|
|
MinSize: aws.Integer(0),
|
|
|
|
MaxSize: aws.Integer(0),
|
2014-10-18 05:10:52 +02:00
|
|
|
}
|
2015-02-20 18:28:20 +01:00
|
|
|
if err := autoscalingconn.UpdateAutoScalingGroup(&opts); err != nil {
|
2014-10-18 05:10:52 +02:00
|
|
|
return fmt.Errorf("Error setting capacity to zero to drain: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, wait for the autoscale group to drain
|
|
|
|
log.Printf("[DEBUG] Waiting for group to have zero instances")
|
|
|
|
return resource.Retry(10*time.Minute, func() error {
|
|
|
|
g, err := getAwsAutoscalingGroup(d, meta)
|
|
|
|
if err != nil {
|
2015-02-18 19:26:59 +01:00
|
|
|
return resource.RetryError{Err: err}
|
2014-10-18 05:10:52 +02:00
|
|
|
}
|
|
|
|
if g == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(g.Instances) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("group still has %d instances", len(g.Instances))
|
|
|
|
})
|
2014-07-15 18:31:49 +02:00
|
|
|
}
|