2014-06-27 18:47:19 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2014-10-17 18:12:45 +02:00
|
|
|
"bytes"
|
2014-08-22 17:46:48 +02:00
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/hex"
|
2014-06-27 18:47:19 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2014-12-25 18:21:05 +01:00
|
|
|
"strconv"
|
2014-07-28 18:47:40 +02:00
|
|
|
"strings"
|
2014-07-01 19:10:11 +02:00
|
|
|
"time"
|
2014-06-27 18:47:19 +02:00
|
|
|
|
2014-08-22 03:38:43 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/hashcode"
|
2014-07-01 19:10:11 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2014-08-22 03:38:43 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2014-06-27 18:47:19 +02:00
|
|
|
"github.com/mitchellh/goamz/ec2"
|
|
|
|
)
|
|
|
|
|
2014-08-22 03:38:43 +02:00
|
|
|
func resourceAwsInstance() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsInstanceCreate,
|
|
|
|
Read: resourceAwsInstanceRead,
|
|
|
|
Update: resourceAwsInstanceUpdate,
|
|
|
|
Delete: resourceAwsInstanceDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"ami": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"associate_public_ip_address": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"availability_zone": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"instance_type": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"key_name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
2014-08-22 21:20:06 +02:00
|
|
|
Optional: true,
|
2014-08-22 03:38:43 +02:00
|
|
|
ForceNew: true,
|
2014-08-22 21:20:06 +02:00
|
|
|
Computed: true,
|
2014-08-22 03:38:43 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"subnet_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"private_ip": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2014-09-18 22:26:49 +02:00
|
|
|
ForceNew: true,
|
2014-08-22 03:38:43 +02:00
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"source_dest_check": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"user_data": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2014-08-22 17:46:48 +02:00
|
|
|
StateFunc: func(v interface{}) string {
|
2014-08-23 03:11:06 +02:00
|
|
|
switch v.(type) {
|
|
|
|
case string:
|
|
|
|
hash := sha1.Sum([]byte(v.(string)))
|
|
|
|
return hex.EncodeToString(hash[:])
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
2014-08-22 17:46:48 +02:00
|
|
|
},
|
2014-08-22 03:38:43 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"security_groups": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
2014-08-28 00:26:15 +02:00
|
|
|
Computed: true,
|
2014-10-11 02:14:35 +02:00
|
|
|
ForceNew: true,
|
2014-08-22 03:38:43 +02:00
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Set: func(v interface{}) int {
|
|
|
|
return hashcode.String(v.(string))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"public_dns": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"public_ip": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"private_dns": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2014-09-03 12:18:40 +02:00
|
|
|
|
|
|
|
"ebs_optimized": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2014-10-09 01:43:13 +02:00
|
|
|
|
2014-09-23 20:06:30 +02:00
|
|
|
"iam_instance_profile": &schema.Schema{
|
2014-09-28 20:51:49 +02:00
|
|
|
Type: schema.TypeString,
|
2014-09-23 20:06:30 +02:00
|
|
|
ForceNew: true,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2014-11-04 12:08:30 +01:00
|
|
|
"tenancy": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2014-10-13 22:55:59 +02:00
|
|
|
"tags": tagsSchema(),
|
2014-10-17 18:12:45 +02:00
|
|
|
|
|
|
|
"block_device": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
2014-12-25 21:58:26 +01:00
|
|
|
Computed: true,
|
2014-10-17 18:12:45 +02:00
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"device_name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2014-11-22 10:50:22 +01:00
|
|
|
"virtual_name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2014-10-17 18:12:45 +02:00
|
|
|
"snapshot_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2014-12-25 18:21:05 +01:00
|
|
|
Computed: true,
|
2014-10-17 18:12:45 +02:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"volume_type": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2014-12-25 18:21:05 +01:00
|
|
|
Computed: true,
|
2014-10-17 18:12:45 +02:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"volume_size": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
2014-12-25 18:21:05 +01:00
|
|
|
Computed: true,
|
2014-10-17 18:12:45 +02:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"delete_on_termination": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"encrypted": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
2014-12-25 18:21:05 +01:00
|
|
|
Computed: true,
|
2014-10-17 18:12:45 +02:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Set: resourceAwsInstanceBlockDevicesHash,
|
|
|
|
},
|
2014-08-22 03:38:43 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsInstanceCreate(d *schema.ResourceData, meta interface{}) error {
|
2014-11-21 17:58:34 +01:00
|
|
|
ec2conn := meta.(*AWSClient).ec2conn
|
2014-06-27 18:47:19 +02:00
|
|
|
|
2014-07-16 18:01:56 +02:00
|
|
|
// Figure out user data
|
|
|
|
userData := ""
|
2014-08-22 03:38:43 +02:00
|
|
|
if v := d.Get("user_data"); v != nil {
|
|
|
|
userData = v.(string)
|
2014-07-16 18:01:56 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 14:06:53 +02:00
|
|
|
associatePublicIPAddress := false
|
2014-09-03 13:54:36 +02:00
|
|
|
if v := d.Get("associate_public_ip_address"); v != nil {
|
2014-08-22 03:38:43 +02:00
|
|
|
associatePublicIPAddress = v.(bool)
|
2014-07-29 14:06:53 +02:00
|
|
|
}
|
|
|
|
|
2014-07-15 06:56:37 +02:00
|
|
|
// Build the creation struct
|
2014-06-27 18:47:19 +02:00
|
|
|
runOpts := &ec2.RunInstances{
|
2014-08-22 03:38:43 +02:00
|
|
|
ImageId: d.Get("ami").(string),
|
|
|
|
AvailZone: d.Get("availability_zone").(string),
|
|
|
|
InstanceType: d.Get("instance_type").(string),
|
|
|
|
KeyName: d.Get("key_name").(string),
|
|
|
|
SubnetId: d.Get("subnet_id").(string),
|
|
|
|
PrivateIPAddress: d.Get("private_ip").(string),
|
2014-07-29 14:06:53 +02:00
|
|
|
AssociatePublicIpAddress: associatePublicIPAddress,
|
|
|
|
UserData: []byte(userData),
|
2014-09-03 12:18:40 +02:00
|
|
|
EbsOptimized: d.Get("ebs_optimized").(bool),
|
2014-09-23 20:06:30 +02:00
|
|
|
IamInstanceProfile: d.Get("iam_instance_profile").(string),
|
2014-11-04 12:08:30 +01:00
|
|
|
Tenancy: d.Get("tenancy").(string),
|
2014-06-27 18:47:19 +02:00
|
|
|
}
|
2014-08-22 03:38:43 +02:00
|
|
|
|
|
|
|
if v := d.Get("security_groups"); v != nil {
|
2014-08-22 21:20:06 +02:00
|
|
|
for _, v := range v.(*schema.Set).List() {
|
2014-08-22 03:38:43 +02:00
|
|
|
str := v.(string)
|
|
|
|
|
|
|
|
var g ec2.SecurityGroup
|
|
|
|
if runOpts.SubnetId != "" {
|
|
|
|
g.Id = str
|
|
|
|
} else {
|
|
|
|
g.Name = str
|
2014-07-15 06:56:37 +02:00
|
|
|
}
|
2014-08-22 03:38:43 +02:00
|
|
|
|
|
|
|
runOpts.SecurityGroups = append(runOpts.SecurityGroups, g)
|
2014-07-15 06:56:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-17 18:12:45 +02:00
|
|
|
if v := d.Get("block_device"); v != nil {
|
|
|
|
vs := v.(*schema.Set).List()
|
|
|
|
if len(vs) > 0 {
|
|
|
|
runOpts.BlockDevices = make([]ec2.BlockDeviceMapping, len(vs))
|
|
|
|
for i, v := range vs {
|
|
|
|
bd := v.(map[string]interface{})
|
|
|
|
runOpts.BlockDevices[i].DeviceName = bd["device_name"].(string)
|
2014-11-22 10:50:22 +01:00
|
|
|
runOpts.BlockDevices[i].VirtualName = bd["virtual_name"].(string)
|
2014-10-17 18:12:45 +02:00
|
|
|
runOpts.BlockDevices[i].SnapshotId = bd["snapshot_id"].(string)
|
|
|
|
runOpts.BlockDevices[i].VolumeType = bd["volume_type"].(string)
|
|
|
|
runOpts.BlockDevices[i].VolumeSize = int64(bd["volume_size"].(int))
|
|
|
|
runOpts.BlockDevices[i].DeleteOnTermination = bd["delete_on_termination"].(bool)
|
|
|
|
runOpts.BlockDevices[i].Encrypted = bd["encrypted"].(bool)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-15 06:56:37 +02:00
|
|
|
// Create the instance
|
2014-06-27 18:47:19 +02:00
|
|
|
log.Printf("[DEBUG] Run configuration: %#v", runOpts)
|
|
|
|
runResp, err := ec2conn.RunInstances(runOpts)
|
|
|
|
if err != nil {
|
2014-08-22 03:38:43 +02:00
|
|
|
return fmt.Errorf("Error launching source instance: %s", err)
|
2014-06-27 18:47:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
instance := &runResp.Instances[0]
|
|
|
|
log.Printf("[INFO] Instance ID: %s", instance.InstanceId)
|
|
|
|
|
|
|
|
// Store the resulting ID so we can look this up later
|
2014-08-22 03:38:43 +02:00
|
|
|
d.SetId(instance.InstanceId)
|
2014-06-27 18:47:19 +02:00
|
|
|
|
|
|
|
// Wait for the instance to become running so we can get some attributes
|
|
|
|
// that aren't available until later.
|
|
|
|
log.Printf(
|
|
|
|
"[DEBUG] Waiting for instance (%s) to become running",
|
|
|
|
instance.InstanceId)
|
2014-07-01 19:10:11 +02:00
|
|
|
|
|
|
|
stateConf := &resource.StateChangeConf{
|
2014-07-28 18:10:28 +02:00
|
|
|
Pending: []string{"pending"},
|
|
|
|
Target: "running",
|
|
|
|
Refresh: InstanceStateRefreshFunc(ec2conn, instance.InstanceId),
|
|
|
|
Timeout: 10 * time.Minute,
|
|
|
|
Delay: 10 * time.Second,
|
|
|
|
MinTimeout: 3 * time.Second,
|
2014-07-01 19:10:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
instanceRaw, err := stateConf.WaitForState()
|
2014-06-27 18:47:19 +02:00
|
|
|
if err != nil {
|
2014-08-22 03:38:43 +02:00
|
|
|
return fmt.Errorf(
|
2014-06-27 18:47:19 +02:00
|
|
|
"Error waiting for instance (%s) to become ready: %s",
|
|
|
|
instance.InstanceId, err)
|
|
|
|
}
|
2014-07-01 19:10:11 +02:00
|
|
|
|
2014-06-27 18:47:19 +02:00
|
|
|
instance = instanceRaw.(*ec2.Instance)
|
|
|
|
|
2014-07-15 02:24:10 +02:00
|
|
|
// Initialize the connection info
|
2014-08-22 08:03:04 +02:00
|
|
|
d.SetConnInfo(map[string]string{
|
|
|
|
"type": "ssh",
|
|
|
|
"host": instance.PublicIpAddress,
|
|
|
|
})
|
2014-07-15 02:24:10 +02:00
|
|
|
|
2014-06-27 18:47:19 +02:00
|
|
|
// Set our attributes
|
2014-08-22 03:38:43 +02:00
|
|
|
if err := resourceAwsInstanceRead(d, meta); err != nil {
|
|
|
|
return err
|
2014-07-14 23:16:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update if we need to
|
2014-08-22 03:38:43 +02:00
|
|
|
return resourceAwsInstanceUpdate(d, meta)
|
2014-07-14 23:16:59 +02:00
|
|
|
}
|
|
|
|
|
2014-08-22 03:38:43 +02:00
|
|
|
func resourceAwsInstanceRead(d *schema.ResourceData, meta interface{}) error {
|
2014-11-21 17:58:34 +01:00
|
|
|
ec2conn := meta.(*AWSClient).ec2conn
|
2014-06-27 18:47:19 +02:00
|
|
|
|
2014-08-22 03:38:43 +02:00
|
|
|
resp, err := ec2conn.Instances([]string{d.Id()}, ec2.NewFilter())
|
2014-06-27 18:47:19 +02:00
|
|
|
if err != nil {
|
|
|
|
// If the instance was not found, return nil so that we can show
|
|
|
|
// that the instance is gone.
|
|
|
|
if ec2err, ok := err.(*ec2.Error); ok && ec2err.Code == "InvalidInstanceID.NotFound" {
|
2014-08-22 03:38:43 +02:00
|
|
|
d.SetId("")
|
|
|
|
return nil
|
2014-06-27 18:47:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Some other error, report it
|
2014-08-22 03:38:43 +02:00
|
|
|
return err
|
2014-06-27 18:47:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If nothing was found, then return no state
|
|
|
|
if len(resp.Reservations) == 0 {
|
2014-08-22 03:38:43 +02:00
|
|
|
d.SetId("")
|
|
|
|
return nil
|
2014-06-27 18:47:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
instance := &resp.Reservations[0].Instances[0]
|
|
|
|
|
|
|
|
// If the instance is terminated, then it is gone
|
|
|
|
if instance.State.Name == "terminated" {
|
2014-08-22 03:38:43 +02:00
|
|
|
d.SetId("")
|
|
|
|
return nil
|
2014-06-27 18:47:19 +02:00
|
|
|
}
|
|
|
|
|
2014-08-22 03:38:43 +02:00
|
|
|
d.Set("availability_zone", instance.AvailZone)
|
|
|
|
d.Set("key_name", instance.KeyName)
|
|
|
|
d.Set("public_dns", instance.DNSName)
|
|
|
|
d.Set("public_ip", instance.PublicIpAddress)
|
|
|
|
d.Set("private_dns", instance.PrivateDNSName)
|
|
|
|
d.Set("private_ip", instance.PrivateIpAddress)
|
|
|
|
d.Set("subnet_id", instance.SubnetId)
|
2014-09-03 12:18:40 +02:00
|
|
|
d.Set("ebs_optimized", instance.EbsOptimized)
|
2014-10-13 22:55:59 +02:00
|
|
|
d.Set("tags", tagsToMap(instance.Tags))
|
2014-11-04 12:08:30 +01:00
|
|
|
d.Set("tenancy", instance.Tenancy)
|
2014-08-22 03:38:43 +02:00
|
|
|
|
|
|
|
// Determine whether we're referring to security groups with
|
|
|
|
// IDs or names. We use a heuristic to figure this out. By default,
|
|
|
|
// we use IDs if we're in a VPC. However, if we previously had an
|
|
|
|
// all-name list of security groups, we use names. Or, if we had any
|
|
|
|
// IDs, we use IDs.
|
|
|
|
useID := instance.SubnetId != ""
|
|
|
|
if v := d.Get("security_groups"); v != nil {
|
|
|
|
match := false
|
2014-08-22 21:20:06 +02:00
|
|
|
for _, v := range v.(*schema.Set).List() {
|
2014-08-22 03:38:43 +02:00
|
|
|
if strings.HasPrefix(v.(string), "sg-") {
|
|
|
|
match = true
|
|
|
|
break
|
2014-07-28 18:47:40 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-22 03:38:43 +02:00
|
|
|
|
|
|
|
useID = match
|
2014-07-28 18:47:40 +02:00
|
|
|
}
|
|
|
|
|
2014-07-15 06:56:37 +02:00
|
|
|
// Build up the security groups
|
|
|
|
sgs := make([]string, len(instance.SecurityGroups))
|
|
|
|
for i, sg := range instance.SecurityGroups {
|
2014-08-22 03:38:43 +02:00
|
|
|
if useID {
|
2014-07-15 06:56:37 +02:00
|
|
|
sgs[i] = sg.Id
|
|
|
|
} else {
|
|
|
|
sgs[i] = sg.Name
|
|
|
|
}
|
|
|
|
}
|
2014-08-22 03:38:43 +02:00
|
|
|
d.Set("security_groups", sgs)
|
2014-07-15 06:56:37 +02:00
|
|
|
|
2015-01-28 12:00:05 +01:00
|
|
|
blockDevices := make(map[string]ec2.BlockDevice)
|
|
|
|
for _, bd := range instance.BlockDevices {
|
|
|
|
// Skip root device; AWS attaches it automatically and terraform does not
|
|
|
|
// manage it
|
|
|
|
if bd.DeviceName == instance.RootDeviceName {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
blockDevices[bd.VolumeId] = bd
|
|
|
|
}
|
|
|
|
|
|
|
|
volIDs := make([]string, 0, len(blockDevices))
|
|
|
|
for volID := range blockDevices {
|
|
|
|
volIDs = append(volIDs, volID)
|
2014-10-17 18:12:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
volResp, err := ec2conn.Volumes(volIDs, ec2.NewFilter())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-30 15:24:23 +01:00
|
|
|
bds := make([]map[string]interface{}, len(volResp.Volumes))
|
2014-10-17 18:12:45 +02:00
|
|
|
for i, vol := range volResp.Volumes {
|
2014-12-25 18:21:05 +01:00
|
|
|
volSize, err := strconv.Atoi(vol.Size)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-10-17 18:12:45 +02:00
|
|
|
bds[i] = make(map[string]interface{})
|
2015-01-28 12:00:05 +01:00
|
|
|
bds[i]["device_name"] = blockDevices[vol.VolumeId].DeviceName
|
2014-10-17 18:12:45 +02:00
|
|
|
bds[i]["snapshot_id"] = vol.SnapshotId
|
|
|
|
bds[i]["volume_type"] = vol.VolumeType
|
2014-12-25 18:21:05 +01:00
|
|
|
bds[i]["volume_size"] = volSize
|
2015-01-28 12:00:05 +01:00
|
|
|
bds[i]["delete_on_termination"] = blockDevices[vol.VolumeId].DeleteOnTermination
|
2014-10-17 18:12:45 +02:00
|
|
|
bds[i]["encrypted"] = vol.Encrypted
|
|
|
|
}
|
|
|
|
d.Set("block_device", bds)
|
|
|
|
|
2014-08-22 03:38:43 +02:00
|
|
|
return nil
|
2014-06-27 18:47:19 +02:00
|
|
|
}
|
2014-07-01 19:10:11 +02:00
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
func resourceAwsInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
ec2conn := meta.(*AWSClient).ec2conn
|
|
|
|
|
|
|
|
modify := false
|
|
|
|
opts := new(ec2.ModifyInstance)
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("source_dest_check"); ok {
|
|
|
|
opts.SourceDestCheck = v.(bool)
|
|
|
|
opts.SetSourceDestCheck = true
|
|
|
|
modify = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if modify {
|
|
|
|
log.Printf("[INFO] Modifing instance %s: %#v", d.Id(), opts)
|
|
|
|
if _, err := ec2conn.ModifyInstance(d.Id(), opts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(mitchellh): wait for the attributes we modified to
|
|
|
|
// persist the change...
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := setTags(ec2conn, d); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
d.SetPartial("tags")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsInstanceDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
ec2conn := meta.(*AWSClient).ec2conn
|
|
|
|
|
|
|
|
log.Printf("[INFO] Terminating instance: %s", d.Id())
|
|
|
|
if _, err := ec2conn.TerminateInstances([]string{d.Id()}); err != nil {
|
|
|
|
return fmt.Errorf("Error terminating instance: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf(
|
|
|
|
"[DEBUG] Waiting for instance (%s) to become terminated",
|
|
|
|
d.Id())
|
|
|
|
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"pending", "running", "shutting-down", "stopped", "stopping"},
|
|
|
|
Target: "terminated",
|
|
|
|
Refresh: InstanceStateRefreshFunc(ec2conn, d.Id()),
|
|
|
|
Timeout: 10 * time.Minute,
|
|
|
|
Delay: 10 * time.Second,
|
|
|
|
MinTimeout: 3 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := stateConf.WaitForState()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for instance (%s) to terminate: %s",
|
|
|
|
d.Id(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
2014-08-22 03:38:43 +02:00
|
|
|
return nil
|
2014-06-27 18:47:19 +02:00
|
|
|
}
|
2014-07-01 19:10:11 +02:00
|
|
|
|
|
|
|
// InstanceStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
|
|
|
|
// an EC2 instance.
|
|
|
|
func InstanceStateRefreshFunc(conn *ec2.EC2, instanceID string) resource.StateRefreshFunc {
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
resp, err := conn.Instances([]string{instanceID}, ec2.NewFilter())
|
|
|
|
if err != nil {
|
|
|
|
if ec2err, ok := err.(*ec2.Error); ok && ec2err.Code == "InvalidInstanceID.NotFound" {
|
|
|
|
// Set this to nil as if we didn't find anything.
|
|
|
|
resp = nil
|
|
|
|
} else {
|
|
|
|
log.Printf("Error on InstanceStateRefresh: %s", err)
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp == nil || len(resp.Reservations) == 0 || len(resp.Reservations[0].Instances) == 0 {
|
|
|
|
// Sometimes AWS just has consistency issues and doesn't see
|
|
|
|
// our instance yet. Return an empty state.
|
|
|
|
return nil, "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
i := &resp.Reservations[0].Instances[0]
|
|
|
|
return i, i.State.Name, nil
|
|
|
|
}
|
|
|
|
}
|
2014-10-17 18:12:45 +02:00
|
|
|
|
|
|
|
func resourceAwsInstanceBlockDevicesHash(v interface{}) int {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
m := v.(map[string]interface{})
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["device_name"].(string)))
|
2014-12-25 18:21:05 +01:00
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["virtual_name"].(string)))
|
2014-10-17 18:12:45 +02:00
|
|
|
buf.WriteString(fmt.Sprintf("%t-", m["delete_on_termination"].(bool)))
|
|
|
|
return hashcode.String(buf.String())
|
|
|
|
}
|