2014-07-10 20:15:58 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2015-04-02 17:47:37 +02:00
|
|
|
"bytes"
|
2014-10-10 23:50:23 +02:00
|
|
|
"crypto/sha1"
|
2015-02-20 23:26:43 +01:00
|
|
|
"encoding/base64"
|
2014-10-10 23:50:23 +02:00
|
|
|
"encoding/hex"
|
2014-07-10 20:15:58 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2014-10-11 21:27:23 +02:00
|
|
|
"time"
|
2014-07-10 20:15:58 +02:00
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/aws-sdk-go/service/autoscaling"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2014-10-10 23:50:23 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/hashcode"
|
2014-10-11 21:27:23 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2014-10-10 23:50:23 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2014-07-10 20:15:58 +02:00
|
|
|
)
|
|
|
|
|
2014-10-10 23:50:23 +02:00
|
|
|
func resourceAwsLaunchConfiguration() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsLaunchConfigurationCreate,
|
|
|
|
Read: resourceAwsLaunchConfigurationRead,
|
|
|
|
Delete: resourceAwsLaunchConfigurationDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
2015-02-27 19:15:12 +01:00
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
2014-10-10 23:50:23 +02:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"image_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"instance_type": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"iam_instance_profile": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"key_name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"user_data": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
StateFunc: func(v interface{}) string {
|
|
|
|
switch v.(type) {
|
|
|
|
case string:
|
|
|
|
hash := sha1.Sum([]byte(v.(string)))
|
|
|
|
return hex.EncodeToString(hash[:])
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"security_groups": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
2015-04-09 15:38:16 +02:00
|
|
|
Set: schema.HashString,
|
2014-10-10 23:50:23 +02:00
|
|
|
},
|
2014-11-02 00:21:55 +01:00
|
|
|
|
|
|
|
"associate_public_ip_address": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
2015-04-03 16:57:30 +02:00
|
|
|
ForceNew: true,
|
2014-11-02 00:21:55 +01:00
|
|
|
Default: false,
|
|
|
|
},
|
2015-01-22 05:48:04 +01:00
|
|
|
|
|
|
|
"spot_price": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2015-04-02 17:47:37 +02:00
|
|
|
|
|
|
|
"ebs_optimized": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
2015-04-03 16:57:30 +02:00
|
|
|
ForceNew: true,
|
2015-04-03 02:51:17 +02:00
|
|
|
Computed: true,
|
2015-04-02 17:47:37 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"placement_tenancy": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2015-06-21 00:24:28 +02:00
|
|
|
"enable_monitoring": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2015-04-02 17:47:37 +02:00
|
|
|
"ebs_block_device": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"delete_on_termination": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"device_name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"iops": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"snapshot_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"volume_size": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"volume_type": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Set: func(v interface{}) int {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
m := v.(map[string]interface{})
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["device_name"].(string)))
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["snapshot_id"].(string)))
|
|
|
|
return hashcode.String(buf.String())
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"ephemeral_block_device": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"device_name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"virtual_name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Set: func(v interface{}) int {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
m := v.(map[string]interface{})
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["device_name"].(string)))
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["virtual_name"].(string)))
|
|
|
|
return hashcode.String(buf.String())
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"root_block_device": &schema.Schema{
|
|
|
|
// TODO: This is a set because we don't support singleton
|
|
|
|
// sub-resources today. We'll enforce that the set only ever has
|
|
|
|
// length zero or one below. When TF gains support for
|
|
|
|
// sub-resources this can be converted.
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
// "You can only modify the volume size, volume type, and Delete on
|
|
|
|
// Termination flag on the block device mapping entry for the root
|
|
|
|
// device volume." - bit.ly/ec2bdmap
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"delete_on_termination": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"iops": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"volume_size": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"volume_type": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Set: func(v interface{}) int {
|
2015-04-08 00:05:00 +02:00
|
|
|
// there can be only one root device; no need to hash anything
|
|
|
|
return 0
|
2015-04-02 17:47:37 +02:00
|
|
|
},
|
|
|
|
},
|
2014-10-10 23:50:23 +02:00
|
|
|
},
|
2014-07-10 20:15:58 +02:00
|
|
|
}
|
2014-10-10 23:50:23 +02:00
|
|
|
}
|
2014-07-10 20:15:58 +02:00
|
|
|
|
2014-10-10 23:50:23 +02:00
|
|
|
func resourceAwsLaunchConfigurationCreate(d *schema.ResourceData, meta interface{}) error {
|
2014-11-21 17:58:34 +01:00
|
|
|
autoscalingconn := meta.(*AWSClient).autoscalingconn
|
2015-04-16 22:05:55 +02:00
|
|
|
ec2conn := meta.(*AWSClient).ec2conn
|
2014-07-10 20:15:58 +02:00
|
|
|
|
2015-04-16 14:10:17 +02:00
|
|
|
createLaunchConfigurationOpts := autoscaling.CreateLaunchConfigurationInput{
|
2015-04-02 17:47:37 +02:00
|
|
|
LaunchConfigurationName: aws.String(d.Get("name").(string)),
|
|
|
|
ImageID: aws.String(d.Get("image_id").(string)),
|
|
|
|
InstanceType: aws.String(d.Get("instance_type").(string)),
|
|
|
|
EBSOptimized: aws.Boolean(d.Get("ebs_optimized").(bool)),
|
|
|
|
}
|
2015-02-20 23:26:43 +01:00
|
|
|
|
|
|
|
if v, ok := d.GetOk("user_data"); ok {
|
2015-04-02 17:47:37 +02:00
|
|
|
userData := base64.StdEncoding.EncodeToString([]byte(v.(string)))
|
|
|
|
createLaunchConfigurationOpts.UserData = aws.String(userData)
|
2015-02-20 23:26:43 +01:00
|
|
|
}
|
2015-04-02 17:47:37 +02:00
|
|
|
|
2015-06-21 00:24:28 +02:00
|
|
|
if v, ok := d.GetOk("enable_monitoring"); ok {
|
|
|
|
createLaunchConfigurationOpts.InstanceMonitoring = &autoscaling.InstanceMonitoring{
|
|
|
|
Enabled: aws.Boolean(v.(bool)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 23:26:43 +01:00
|
|
|
if v, ok := d.GetOk("iam_instance_profile"); ok {
|
|
|
|
createLaunchConfigurationOpts.IAMInstanceProfile = aws.String(v.(string))
|
|
|
|
}
|
2015-04-02 17:47:37 +02:00
|
|
|
|
|
|
|
if v, ok := d.GetOk("placement_tenancy"); ok {
|
|
|
|
createLaunchConfigurationOpts.PlacementTenancy = aws.String(v.(string))
|
|
|
|
}
|
|
|
|
|
2015-04-07 20:08:09 +02:00
|
|
|
if v, ok := d.GetOk("associate_public_ip_address"); ok {
|
2015-04-02 17:47:37 +02:00
|
|
|
createLaunchConfigurationOpts.AssociatePublicIPAddress = aws.Boolean(v.(bool))
|
|
|
|
}
|
|
|
|
|
2015-02-20 23:26:43 +01:00
|
|
|
if v, ok := d.GetOk("key_name"); ok {
|
|
|
|
createLaunchConfigurationOpts.KeyName = aws.String(v.(string))
|
|
|
|
}
|
|
|
|
if v, ok := d.GetOk("spot_price"); ok {
|
|
|
|
createLaunchConfigurationOpts.SpotPrice = aws.String(v.(string))
|
|
|
|
}
|
2014-07-10 20:15:58 +02:00
|
|
|
|
2014-10-10 23:50:23 +02:00
|
|
|
if v, ok := d.GetOk("security_groups"); ok {
|
2015-04-16 22:28:18 +02:00
|
|
|
createLaunchConfigurationOpts.SecurityGroups = expandStringList(
|
2015-04-02 17:47:37 +02:00
|
|
|
v.(*schema.Set).List(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2015-04-16 14:10:17 +02:00
|
|
|
var blockDevices []*autoscaling.BlockDeviceMapping
|
2015-04-02 17:47:37 +02:00
|
|
|
|
|
|
|
if v, ok := d.GetOk("ebs_block_device"); ok {
|
|
|
|
vL := v.(*schema.Set).List()
|
|
|
|
for _, v := range vL {
|
|
|
|
bd := v.(map[string]interface{})
|
|
|
|
ebs := &autoscaling.EBS{
|
|
|
|
DeleteOnTermination: aws.Boolean(bd["delete_on_termination"].(bool)),
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := bd["snapshot_id"].(string); ok && v != "" {
|
|
|
|
ebs.SnapshotID = aws.String(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := bd["volume_size"].(int); ok && v != 0 {
|
2015-04-16 14:10:17 +02:00
|
|
|
ebs.VolumeSize = aws.Long(int64(v))
|
2015-04-02 17:47:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := bd["volume_type"].(string); ok && v != "" {
|
|
|
|
ebs.VolumeType = aws.String(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := bd["iops"].(int); ok && v > 0 {
|
2015-04-16 14:10:17 +02:00
|
|
|
ebs.IOPS = aws.Long(int64(v))
|
2015-04-02 17:47:37 +02:00
|
|
|
}
|
|
|
|
|
2015-04-16 14:10:17 +02:00
|
|
|
blockDevices = append(blockDevices, &autoscaling.BlockDeviceMapping{
|
2015-04-02 17:47:37 +02:00
|
|
|
DeviceName: aws.String(bd["device_name"].(string)),
|
|
|
|
EBS: ebs,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("ephemeral_block_device"); ok {
|
|
|
|
vL := v.(*schema.Set).List()
|
|
|
|
for _, v := range vL {
|
|
|
|
bd := v.(map[string]interface{})
|
2015-04-16 14:10:17 +02:00
|
|
|
blockDevices = append(blockDevices, &autoscaling.BlockDeviceMapping{
|
2015-04-02 17:47:37 +02:00
|
|
|
DeviceName: aws.String(bd["device_name"].(string)),
|
|
|
|
VirtualName: aws.String(bd["virtual_name"].(string)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("root_block_device"); ok {
|
|
|
|
vL := v.(*schema.Set).List()
|
|
|
|
if len(vL) > 1 {
|
|
|
|
return fmt.Errorf("Cannot specify more than one root_block_device.")
|
|
|
|
}
|
|
|
|
for _, v := range vL {
|
|
|
|
bd := v.(map[string]interface{})
|
|
|
|
ebs := &autoscaling.EBS{
|
|
|
|
DeleteOnTermination: aws.Boolean(bd["delete_on_termination"].(bool)),
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := bd["volume_size"].(int); ok && v != 0 {
|
2015-04-16 14:10:17 +02:00
|
|
|
ebs.VolumeSize = aws.Long(int64(v))
|
2015-04-02 17:47:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := bd["volume_type"].(string); ok && v != "" {
|
|
|
|
ebs.VolumeType = aws.String(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := bd["iops"].(int); ok && v > 0 {
|
2015-04-16 14:10:17 +02:00
|
|
|
ebs.IOPS = aws.Long(int64(v))
|
2015-04-02 17:47:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if dn, err := fetchRootDeviceName(d.Get("image_id").(string), ec2conn); err == nil {
|
2015-04-16 14:10:17 +02:00
|
|
|
blockDevices = append(blockDevices, &autoscaling.BlockDeviceMapping{
|
2015-04-02 17:47:37 +02:00
|
|
|
DeviceName: dn,
|
|
|
|
EBS: ebs,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(blockDevices) > 0 {
|
|
|
|
createLaunchConfigurationOpts.BlockDeviceMappings = blockDevices
|
2014-07-29 23:30:24 +02:00
|
|
|
}
|
|
|
|
|
2015-04-22 19:53:47 +02:00
|
|
|
var lcName string
|
2015-02-27 19:15:12 +01:00
|
|
|
if v, ok := d.GetOk("name"); ok {
|
2015-04-22 19:53:47 +02:00
|
|
|
lcName = v.(string)
|
2015-02-27 19:15:12 +01:00
|
|
|
} else {
|
2015-04-22 19:53:47 +02:00
|
|
|
lcName = resource.UniqueId()
|
2015-02-27 19:15:12 +01:00
|
|
|
}
|
2015-04-22 19:53:47 +02:00
|
|
|
createLaunchConfigurationOpts.LaunchConfigurationName = aws.String(lcName)
|
2015-02-27 19:15:12 +01:00
|
|
|
|
2015-04-14 02:03:13 +02:00
|
|
|
log.Printf(
|
|
|
|
"[DEBUG] autoscaling create launch configuration: %#v", createLaunchConfigurationOpts)
|
2015-06-24 04:13:25 +02:00
|
|
|
|
|
|
|
// IAM profiles can take ~10 seconds to propagate in AWS:
|
|
|
|
// http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html#launch-instance-with-role-console
|
|
|
|
err := resource.Retry(30*time.Second, func() error {
|
|
|
|
_, err := autoscalingconn.CreateLaunchConfiguration(&createLaunchConfigurationOpts)
|
2015-06-24 23:11:46 +02:00
|
|
|
if err != nil {
|
|
|
|
if awsErr, ok := err.(awserr.Error); ok {
|
|
|
|
if awsErr.Message() == "Invalid IamInstanceProfile" {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &resource.RetryError{
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2015-06-24 04:13:25 +02:00
|
|
|
})
|
|
|
|
|
2014-07-10 20:15:58 +02:00
|
|
|
if err != nil {
|
2014-10-10 23:50:23 +02:00
|
|
|
return fmt.Errorf("Error creating launch configuration: %s", err)
|
2014-07-10 20:15:58 +02:00
|
|
|
}
|
|
|
|
|
2015-04-22 19:53:47 +02:00
|
|
|
d.SetId(lcName)
|
2014-10-10 23:50:23 +02:00
|
|
|
log.Printf("[INFO] launch configuration ID: %s", d.Id())
|
2014-07-10 20:15:58 +02:00
|
|
|
|
2014-10-11 21:27:23 +02:00
|
|
|
// We put a Retry here since sometimes eventual consistency bites
|
|
|
|
// us and we need to retry a few times to get the LC to load properly
|
2014-10-11 21:57:06 +02:00
|
|
|
return resource.Retry(30*time.Second, func() error {
|
2014-10-11 21:27:23 +02:00
|
|
|
return resourceAwsLaunchConfigurationRead(d, meta)
|
|
|
|
})
|
2014-07-10 20:15:58 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 23:50:23 +02:00
|
|
|
func resourceAwsLaunchConfigurationRead(d *schema.ResourceData, meta interface{}) error {
|
2014-11-21 17:58:34 +01:00
|
|
|
autoscalingconn := meta.(*AWSClient).autoscalingconn
|
2015-04-16 22:05:55 +02:00
|
|
|
ec2conn := meta.(*AWSClient).ec2conn
|
2014-07-10 20:15:58 +02:00
|
|
|
|
2015-04-16 14:10:17 +02:00
|
|
|
describeOpts := autoscaling.DescribeLaunchConfigurationsInput{
|
|
|
|
LaunchConfigurationNames: []*string{aws.String(d.Id())},
|
2014-07-10 20:15:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] launch configuration describe configuration: %#v", describeOpts)
|
|
|
|
describConfs, err := autoscalingconn.DescribeLaunchConfigurations(&describeOpts)
|
|
|
|
if err != nil {
|
2014-10-10 23:50:23 +02:00
|
|
|
return fmt.Errorf("Error retrieving launch configuration: %s", err)
|
2014-07-10 20:15:58 +02:00
|
|
|
}
|
2014-10-18 05:21:18 +02:00
|
|
|
if len(describConfs.LaunchConfigurations) == 0 {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
2014-07-10 20:15:58 +02:00
|
|
|
|
|
|
|
// Verify AWS returned our launch configuration
|
2015-02-20 23:26:43 +01:00
|
|
|
if *describConfs.LaunchConfigurations[0].LaunchConfigurationName != d.Id() {
|
2014-10-18 05:21:18 +02:00
|
|
|
return fmt.Errorf(
|
|
|
|
"Unable to find launch configuration: %#v",
|
|
|
|
describConfs.LaunchConfigurations)
|
2014-07-10 20:15:58 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 23:50:23 +02:00
|
|
|
lc := describConfs.LaunchConfigurations[0]
|
2014-07-10 20:15:58 +02:00
|
|
|
|
2015-04-03 02:51:17 +02:00
|
|
|
d.Set("key_name", lc.KeyName)
|
|
|
|
d.Set("image_id", lc.ImageID)
|
|
|
|
d.Set("instance_type", lc.InstanceType)
|
|
|
|
d.Set("name", lc.LaunchConfigurationName)
|
2015-02-20 23:26:43 +01:00
|
|
|
|
2015-04-03 02:51:17 +02:00
|
|
|
d.Set("iam_instance_profile", lc.IAMInstanceProfile)
|
|
|
|
d.Set("ebs_optimized", lc.EBSOptimized)
|
|
|
|
d.Set("spot_price", lc.SpotPrice)
|
2015-06-21 00:24:28 +02:00
|
|
|
d.Set("enable_monitoring", lc.InstanceMonitoring.Enabled)
|
2015-04-03 02:51:17 +02:00
|
|
|
d.Set("security_groups", lc.SecurityGroups)
|
2015-04-02 17:47:37 +02:00
|
|
|
|
2015-04-16 14:10:17 +02:00
|
|
|
if err := readLCBlockDevices(d, lc, ec2conn); err != nil {
|
2015-04-02 17:47:37 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-10-10 23:50:23 +02:00
|
|
|
return nil
|
2014-07-15 18:36:29 +02:00
|
|
|
}
|
2014-11-21 17:58:34 +01:00
|
|
|
|
|
|
|
func resourceAwsLaunchConfigurationDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
autoscalingconn := meta.(*AWSClient).autoscalingconn
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Launch Configuration destroy: %v", d.Id())
|
2015-04-16 14:10:17 +02:00
|
|
|
_, err := autoscalingconn.DeleteLaunchConfiguration(
|
|
|
|
&autoscaling.DeleteLaunchConfigurationInput{
|
|
|
|
LaunchConfigurationName: aws.String(d.Id()),
|
|
|
|
})
|
2014-11-21 17:58:34 +01:00
|
|
|
if err != nil {
|
2015-05-20 13:21:23 +02:00
|
|
|
autoscalingerr, ok := err.(awserr.Error)
|
|
|
|
if ok && autoscalingerr.Code() == "InvalidConfiguration.NotFound" {
|
2014-11-21 17:58:34 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2015-04-02 17:47:37 +02:00
|
|
|
|
|
|
|
func readLCBlockDevices(d *schema.ResourceData, lc *autoscaling.LaunchConfiguration, ec2conn *ec2.EC2) error {
|
|
|
|
ibds, err := readBlockDevicesFromLaunchConfiguration(d, lc, ec2conn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := d.Set("ebs_block_device", ibds["ebs"]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-09 15:36:18 +02:00
|
|
|
if err := d.Set("ephemeral_block_device", ibds["ephemeral"]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-02 17:47:37 +02:00
|
|
|
if ibds["root"] != nil {
|
|
|
|
if err := d.Set("root_block_device", []interface{}{ibds["root"]}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-03 02:51:17 +02:00
|
|
|
} else {
|
|
|
|
d.Set("root_block_device", []interface{}{})
|
2015-04-02 17:47:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readBlockDevicesFromLaunchConfiguration(d *schema.ResourceData, lc *autoscaling.LaunchConfiguration, ec2conn *ec2.EC2) (
|
|
|
|
map[string]interface{}, error) {
|
|
|
|
blockDevices := make(map[string]interface{})
|
|
|
|
blockDevices["ebs"] = make([]map[string]interface{}, 0)
|
2015-04-09 15:36:18 +02:00
|
|
|
blockDevices["ephemeral"] = make([]map[string]interface{}, 0)
|
2015-04-02 17:47:37 +02:00
|
|
|
blockDevices["root"] = nil
|
|
|
|
if len(lc.BlockDeviceMappings) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
rootDeviceName, err := fetchRootDeviceName(d.Get("image_id").(string), ec2conn)
|
2015-04-09 15:36:18 +02:00
|
|
|
if err != nil {
|
2015-04-02 17:47:37 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-14 05:28:36 +02:00
|
|
|
if rootDeviceName == nil {
|
|
|
|
// We do this so the value is empty so we don't have to do nil checks later
|
|
|
|
var blank string
|
|
|
|
rootDeviceName = &blank
|
|
|
|
}
|
2015-04-02 17:47:37 +02:00
|
|
|
for _, bdm := range lc.BlockDeviceMappings {
|
|
|
|
bd := make(map[string]interface{})
|
|
|
|
if bdm.EBS != nil && bdm.EBS.DeleteOnTermination != nil {
|
|
|
|
bd["delete_on_termination"] = *bdm.EBS.DeleteOnTermination
|
|
|
|
}
|
|
|
|
if bdm.EBS != nil && bdm.EBS.VolumeSize != nil {
|
2015-04-09 15:36:18 +02:00
|
|
|
bd["volume_size"] = *bdm.EBS.VolumeSize
|
2015-04-02 17:47:37 +02:00
|
|
|
}
|
|
|
|
if bdm.EBS != nil && bdm.EBS.VolumeType != nil {
|
|
|
|
bd["volume_type"] = *bdm.EBS.VolumeType
|
|
|
|
}
|
|
|
|
if bdm.EBS != nil && bdm.EBS.IOPS != nil {
|
|
|
|
bd["iops"] = *bdm.EBS.IOPS
|
|
|
|
}
|
2015-04-09 15:36:18 +02:00
|
|
|
if bdm.DeviceName != nil && *bdm.DeviceName == *rootDeviceName {
|
2015-04-02 17:47:37 +02:00
|
|
|
blockDevices["root"] = bd
|
|
|
|
} else {
|
|
|
|
if bdm.DeviceName != nil {
|
|
|
|
bd["device_name"] = *bdm.DeviceName
|
|
|
|
}
|
2015-04-09 15:36:18 +02:00
|
|
|
if bdm.VirtualName != nil {
|
|
|
|
bd["virtual_name"] = *bdm.VirtualName
|
|
|
|
blockDevices["ephemeral"] = append(blockDevices["ephemeral"].([]map[string]interface{}), bd)
|
|
|
|
} else {
|
|
|
|
if bdm.EBS != nil && bdm.EBS.SnapshotID != nil {
|
|
|
|
bd["snapshot_id"] = *bdm.EBS.SnapshotID
|
|
|
|
}
|
|
|
|
blockDevices["ebs"] = append(blockDevices["ebs"].([]map[string]interface{}), bd)
|
2015-04-02 17:47:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return blockDevices, nil
|
|
|
|
}
|