2014-06-27 18:47:19 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2014-07-16 18:01:56 +02:00
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/hex"
|
2014-06-27 18:47:19 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2014-07-14 23:16:59 +02:00
|
|
|
"strconv"
|
2014-07-01 19:10:11 +02:00
|
|
|
"time"
|
2014-06-27 18:47:19 +02:00
|
|
|
|
2014-07-15 06:56:37 +02:00
|
|
|
"github.com/hashicorp/terraform/flatmap"
|
2014-06-27 18:47:19 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/diff"
|
2014-07-01 19:10:11 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2014-06-27 18:47:19 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/goamz/ec2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resource_aws_instance_create(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
d *terraform.ResourceDiff,
|
|
|
|
meta interface{}) (*terraform.ResourceState, error) {
|
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
ec2conn := p.ec2conn
|
|
|
|
|
|
|
|
// Merge the diff into the state so that we have all the attributes
|
|
|
|
// properly.
|
|
|
|
rs := s.MergeDiff(d)
|
2014-07-14 23:16:59 +02:00
|
|
|
delete(rs.Attributes, "source_dest_check")
|
2014-06-27 18:47:19 +02:00
|
|
|
|
2014-07-16 18:01:56 +02:00
|
|
|
// Figure out user data
|
|
|
|
userData := ""
|
|
|
|
if v, ok := rs.Attributes["user_data"]; ok {
|
|
|
|
userData = v
|
|
|
|
delete(rs.Attributes, "user_data")
|
|
|
|
}
|
|
|
|
|
|
|
|
if userData != "" {
|
|
|
|
// Set the SHA1 hash of the data as an attribute so we can
|
|
|
|
// compare for diffs.
|
|
|
|
hash := sha1.Sum([]byte(userData))
|
|
|
|
rs.Attributes["user_data_hash"] = hex.EncodeToString(hash[:])
|
|
|
|
}
|
|
|
|
|
2014-07-15 06:56:37 +02:00
|
|
|
// Build the creation struct
|
2014-06-27 18:47:19 +02:00
|
|
|
runOpts := &ec2.RunInstances{
|
|
|
|
ImageId: rs.Attributes["ami"],
|
|
|
|
InstanceType: rs.Attributes["instance_type"],
|
2014-07-15 02:52:29 +02:00
|
|
|
KeyName: rs.Attributes["key_name"],
|
2014-07-14 22:46:32 +02:00
|
|
|
SubnetId: rs.Attributes["subnet_id"],
|
2014-07-16 18:01:56 +02:00
|
|
|
UserData: []byte(userData),
|
2014-06-27 18:47:19 +02:00
|
|
|
}
|
2014-07-15 06:56:37 +02:00
|
|
|
if raw := flatmap.Expand(rs.Attributes, "security_groups"); raw != nil {
|
|
|
|
if sgs, ok := raw.([]interface{}); ok {
|
|
|
|
for _, sg := range sgs {
|
|
|
|
str, ok := sg.(string)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var g ec2.SecurityGroup
|
|
|
|
if runOpts.SubnetId != "" {
|
|
|
|
g.Id = str
|
|
|
|
} else {
|
|
|
|
g.Name = str
|
|
|
|
}
|
|
|
|
|
|
|
|
runOpts.SecurityGroups = append(runOpts.SecurityGroups, g)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
return nil, fmt.Errorf("Error launching source instance: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
instance := &runResp.Instances[0]
|
|
|
|
log.Printf("[INFO] Instance ID: %s", instance.InstanceId)
|
|
|
|
|
|
|
|
// Store the resulting ID so we can look this up later
|
|
|
|
rs.ID = instance.InstanceId
|
|
|
|
|
|
|
|
// 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-06-27 18:47:19 +02:00
|
|
|
Pending: []string{"pending"},
|
|
|
|
Target: "running",
|
|
|
|
Refresh: InstanceStateRefreshFunc(ec2conn, instance.InstanceId),
|
2014-07-01 19:10:11 +02:00
|
|
|
Timeout: 10 * time.Minute,
|
|
|
|
}
|
|
|
|
|
|
|
|
instanceRaw, err := stateConf.WaitForState()
|
|
|
|
|
2014-06-27 18:47:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return rs, fmt.Errorf(
|
|
|
|
"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-07-15 20:30:21 +02:00
|
|
|
rs.ConnInfo["type"] = "ssh"
|
|
|
|
rs.ConnInfo["host"] = instance.PublicIpAddress
|
2014-07-15 02:24:10 +02:00
|
|
|
|
2014-06-27 18:47:19 +02:00
|
|
|
// Set our attributes
|
2014-07-14 23:16:59 +02:00
|
|
|
rs, err = resource_aws_instance_update_state(rs, instance)
|
|
|
|
if err != nil {
|
|
|
|
return rs, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update if we need to
|
|
|
|
return resource_aws_instance_update(rs, d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_instance_update(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
d *terraform.ResourceDiff,
|
|
|
|
meta interface{}) (*terraform.ResourceState, error) {
|
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
ec2conn := p.ec2conn
|
|
|
|
rs := s.MergeDiff(d)
|
|
|
|
|
|
|
|
modify := false
|
|
|
|
opts := new(ec2.ModifyInstance)
|
|
|
|
|
|
|
|
if attr, ok := d.Attributes["source_dest_check"]; ok {
|
|
|
|
modify = true
|
|
|
|
opts.SourceDestCheck = attr.New != "" && attr.New != "false"
|
2014-07-15 02:38:39 +02:00
|
|
|
opts.SetSourceDestCheck = true
|
2014-07-14 23:16:59 +02:00
|
|
|
rs.Attributes["source_dest_check"] = strconv.FormatBool(
|
|
|
|
opts.SourceDestCheck)
|
|
|
|
}
|
|
|
|
|
|
|
|
if modify {
|
|
|
|
log.Printf("[INFO] Modifing instance %s: %#v", s.ID, opts)
|
|
|
|
if _, err := ec2conn.ModifyInstance(s.ID, opts); err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
2014-07-15 02:38:39 +02:00
|
|
|
|
|
|
|
// TODO(mitchellh): wait for the attributes we modified to
|
|
|
|
// persist the change...
|
2014-07-14 23:16:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return rs, nil
|
2014-06-27 18:47:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_instance_destroy(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
meta interface{}) error {
|
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
ec2conn := p.ec2conn
|
|
|
|
|
|
|
|
log.Printf("[INFO] Terminating instance: %s", s.ID)
|
|
|
|
if _, err := ec2conn.TerminateInstances([]string{s.ID}); err != nil {
|
|
|
|
return fmt.Errorf("Error terminating instance: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf(
|
|
|
|
"[DEBUG] Waiting for instance (%s) to become terminated",
|
|
|
|
s.ID)
|
2014-07-01 19:10:11 +02:00
|
|
|
|
|
|
|
stateConf := &resource.StateChangeConf{
|
2014-06-27 18:47:19 +02:00
|
|
|
Pending: []string{"pending", "running", "shutting-down", "stopped", "stopping"},
|
|
|
|
Target: "terminated",
|
|
|
|
Refresh: InstanceStateRefreshFunc(ec2conn, s.ID),
|
2014-07-01 19:11:11 +02:00
|
|
|
Timeout: 10 * time.Minute,
|
2014-07-01 19:10:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := stateConf.WaitForState()
|
|
|
|
|
2014-06-27 18:47:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Error waiting for instance (%s) to terminate: %s",
|
|
|
|
s.ID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_instance_diff(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
c *terraform.ResourceConfig,
|
|
|
|
meta interface{}) (*terraform.ResourceDiff, error) {
|
|
|
|
b := &diff.ResourceBuilder{
|
2014-07-02 01:22:24 +02:00
|
|
|
Attrs: map[string]diff.AttrType{
|
|
|
|
"ami": diff.AttrTypeCreate,
|
|
|
|
"availability_zone": diff.AttrTypeCreate,
|
|
|
|
"instance_type": diff.AttrTypeCreate,
|
2014-07-15 02:52:29 +02:00
|
|
|
"key_name": diff.AttrTypeCreate,
|
2014-07-15 06:56:37 +02:00
|
|
|
"security_groups": diff.AttrTypeCreate,
|
2014-07-14 22:46:32 +02:00
|
|
|
"subnet_id": diff.AttrTypeCreate,
|
2014-07-14 23:16:59 +02:00
|
|
|
"source_dest_check": diff.AttrTypeUpdate,
|
2014-07-16 18:01:56 +02:00
|
|
|
"user_data": diff.AttrTypeCreate,
|
2014-07-02 01:22:24 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
ComputedAttrs: []string{
|
2014-07-14 22:30:01 +02:00
|
|
|
"availability_zone",
|
2014-06-27 18:47:19 +02:00
|
|
|
"public_dns",
|
|
|
|
"public_ip",
|
|
|
|
"private_dns",
|
|
|
|
"private_ip",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.Diff(s, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_instance_refresh(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
meta interface{}) (*terraform.ResourceState, error) {
|
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
ec2conn := p.ec2conn
|
|
|
|
|
|
|
|
resp, err := ec2conn.Instances([]string{s.ID}, ec2.NewFilter())
|
|
|
|
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" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some other error, report it
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If nothing was found, then return no state
|
|
|
|
if len(resp.Reservations) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
instance := &resp.Reservations[0].Instances[0]
|
|
|
|
|
|
|
|
// If the instance is terminated, then it is gone
|
|
|
|
if instance.State.Name == "terminated" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return resource_aws_instance_update_state(s, instance)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resource_aws_instance_update_state(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
instance *ec2.Instance) (*terraform.ResourceState, error) {
|
2014-07-14 22:30:01 +02:00
|
|
|
s.Attributes["availability_zone"] = instance.AvailZone
|
2014-07-15 02:52:29 +02:00
|
|
|
s.Attributes["key_name"] = instance.KeyName
|
2014-06-27 18:47:19 +02:00
|
|
|
s.Attributes["public_dns"] = instance.DNSName
|
|
|
|
s.Attributes["public_ip"] = instance.PublicIpAddress
|
|
|
|
s.Attributes["private_dns"] = instance.PrivateDNSName
|
|
|
|
s.Attributes["private_ip"] = instance.PrivateIpAddress
|
2014-07-14 22:46:32 +02:00
|
|
|
s.Attributes["subnet_id"] = instance.SubnetId
|
|
|
|
s.Dependencies = nil
|
|
|
|
|
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 {
|
|
|
|
if instance.SubnetId != "" {
|
|
|
|
sgs[i] = sg.Id
|
|
|
|
} else {
|
|
|
|
sgs[i] = sg.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Dependencies = append(s.Dependencies,
|
|
|
|
terraform.ResourceDependency{ID: sg.Id},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
flatmap.Map(s.Attributes).Merge(flatmap.Flatten(map[string]interface{}{
|
|
|
|
"security_groups": sgs,
|
|
|
|
}))
|
|
|
|
|
2014-07-14 22:46:32 +02:00
|
|
|
if instance.SubnetId != "" {
|
|
|
|
s.Dependencies = append(s.Dependencies,
|
|
|
|
terraform.ResourceDependency{ID: instance.SubnetId},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2014-06-27 18:47:19 +02:00
|
|
|
return s, nil
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
}
|