Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/aws_autoscaling_group: Add support for launch template #4305

Merged
merged 1 commit into from
Apr 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 80 additions & 5 deletions aws/resource_aws_autoscaling_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"time"

"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/customdiff"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
Expand Down Expand Up @@ -48,8 +50,39 @@ func resourceAwsAutoscalingGroup() *schema.Resource {
},

"launch_configuration": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"launch_template"},
},

"launch_template": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
ConflictsWith: []string{"launch_configuration"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"launch_template.0.name"},
ValidateFunc: validateLaunchTemplateId,
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"launch_template.0.id"},
ValidateFunc: validateLaunchTemplateName,
},
"version": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(1, 255),
},
},
},
},

"desired_capacity": {
Expand Down Expand Up @@ -249,6 +282,15 @@ func resourceAwsAutoscalingGroup() *schema.Resource {
Computed: true,
},
},

CustomizeDiff: customdiff.Sequence(
customdiff.ComputedIf("launch_template.0.id", func(diff *schema.ResourceDiff, meta interface{}) bool {
return diff.HasChange("launch_template.0.name")
}),
customdiff.ComputedIf("launch_template.0.name", func(diff *schema.ResourceDiff, meta interface{}) bool {
return diff.HasChange("launch_template.0.id")
}),
),
}
}

Expand Down Expand Up @@ -310,7 +352,6 @@ func resourceAwsAutoscalingGroupCreate(d *schema.ResourceData, meta interface{})

createOpts := autoscaling.CreateAutoScalingGroupInput{
AutoScalingGroupName: aws.String(asgName),
LaunchConfigurationName: aws.String(d.Get("launch_configuration").(string)),
NewInstancesProtectedFromScaleIn: aws.Bool(d.Get("protect_from_scale_in").(bool)),
}
updateOpts := autoscaling.UpdateAutoScalingGroupInput{
Expand Down Expand Up @@ -342,6 +383,25 @@ func resourceAwsAutoscalingGroupCreate(d *schema.ResourceData, meta interface{})
}
}

launchConfigurationValue, launchConfigurationOk := d.GetOk("launch_configuration")
launchTemplateValue, launchTemplateOk := d.GetOk("launch_template")

if !launchConfigurationOk && !launchTemplateOk {
return fmt.Errorf("One of `launch_configuration` or `launch_template` must be set for an autoscaling group")
}

if launchConfigurationOk {
createOpts.LaunchConfigurationName = aws.String(launchConfigurationValue.(string))
}

if launchTemplateOk {
var err error
createOpts.LaunchTemplate, err = expandLaunchTemplateSpecification(launchTemplateValue.([]interface{}))
if err != nil {
return err
}
}

// Availability Zones are optional if VPC Zone Identifer(s) are specified
if v, ok := d.GetOk("availability_zones"); ok && v.(*schema.Set).Len() > 0 {
createOpts.AvailabilityZones = expandStringList(v.(*schema.Set).List())
Expand Down Expand Up @@ -465,9 +525,16 @@ func resourceAwsAutoscalingGroupRead(d *schema.ResourceData, meta interface{}) e
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)
d.Set("load_balancers", flattenStringList(g.LoadBalancerNames))

d.Set("launch_configuration", g.LaunchConfigurationName)

if g.LaunchTemplate != nil {
d.Set("launch_template", flattenLaunchTemplateSpecification(g.LaunchTemplate))
} else {
d.Set("launch_template", nil)
}

if err := d.Set("suspended_processes", flattenAsgSuspendedProcesses(g.SuspendedProcesses)); err != nil {
log.Printf("[WARN] Error setting suspended_processes for %q: %s", d.Id(), err)
}
Expand Down Expand Up @@ -572,7 +639,15 @@ func resourceAwsAutoscalingGroupUpdate(d *schema.ResourceData, meta interface{})
}

if d.HasChange("launch_configuration") {
opts.LaunchConfigurationName = aws.String(d.Get("launch_configuration").(string))
if v, ok := d.GetOk("launch_configuration"); ok {
opts.LaunchConfigurationName = aws.String(v.(string))
}
}

if d.HasChange("launch_template") {
if v, ok := d.GetOk("launch_template"); ok && len(v.([]interface{})) > 0 {
opts.LaunchTemplate, _ = expandLaunchTemplateSpecification(v.([]interface{}))
}
}

if d.HasChange("min_size") {
Expand Down
Loading