github.com/acm1/terraform@v0.6.2-0.20150729164239-1f314444f45c/builtin/providers/aws/resource_aws_spot_instance_request.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/aws/awserr"
    10  	"github.com/aws/aws-sdk-go/service/ec2"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/helper/schema"
    13  )
    14  
    15  func resourceAwsSpotInstanceRequest() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceAwsSpotInstanceRequestCreate,
    18  		Read:   resourceAwsSpotInstanceRequestRead,
    19  		Delete: resourceAwsSpotInstanceRequestDelete,
    20  		Update: resourceAwsSpotInstanceRequestUpdate,
    21  
    22  		Schema: func() map[string]*schema.Schema {
    23  			// The Spot Instance Request Schema is based on the AWS Instance schema.
    24  			s := resourceAwsInstance().Schema
    25  
    26  			// Everything on a spot instance is ForceNew except tags
    27  			for k, v := range s {
    28  				if k == "tags" {
    29  					continue
    30  				}
    31  				v.ForceNew = true
    32  			}
    33  
    34  			s["spot_price"] = &schema.Schema{
    35  				Type:     schema.TypeString,
    36  				Required: true,
    37  				ForceNew: true,
    38  			}
    39  			s["wait_for_fulfillment"] = &schema.Schema{
    40  				Type:     schema.TypeBool,
    41  				Optional: true,
    42  				Default:  false,
    43  			}
    44  			s["spot_bid_status"] = &schema.Schema{
    45  				Type:     schema.TypeString,
    46  				Computed: true,
    47  			}
    48  			s["spot_request_state"] = &schema.Schema{
    49  				Type:     schema.TypeString,
    50  				Computed: true,
    51  			}
    52  			s["spot_instance_id"] = &schema.Schema{
    53  				Type:     schema.TypeString,
    54  				Computed: true,
    55  			}
    56  
    57  			return s
    58  		}(),
    59  	}
    60  }
    61  
    62  func resourceAwsSpotInstanceRequestCreate(d *schema.ResourceData, meta interface{}) error {
    63  	conn := meta.(*AWSClient).ec2conn
    64  
    65  	instanceOpts, err := buildAwsInstanceOpts(d, meta)
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    70  	spotOpts := &ec2.RequestSpotInstancesInput{
    71  		SpotPrice: aws.String(d.Get("spot_price").(string)),
    72  
    73  		// We always set the type to "persistent", since the imperative-like
    74  		// behavior of "one-time" does not map well to TF's declarative domain.
    75  		Type: aws.String("persistent"),
    76  
    77  		// Though the AWS API supports creating spot instance requests for multiple
    78  		// instances, for TF purposes we fix this to one instance per request.
    79  		// Users can get equivalent behavior out of TF's "count" meta-parameter.
    80  		InstanceCount: aws.Int64(1),
    81  
    82  		LaunchSpecification: &ec2.RequestSpotLaunchSpecification{
    83  			BlockDeviceMappings: instanceOpts.BlockDeviceMappings,
    84  			EBSOptimized:        instanceOpts.EBSOptimized,
    85  			Monitoring:          instanceOpts.Monitoring,
    86  			IAMInstanceProfile:  instanceOpts.IAMInstanceProfile,
    87  			ImageID:             instanceOpts.ImageID,
    88  			InstanceType:        instanceOpts.InstanceType,
    89  			Placement:           instanceOpts.SpotPlacement,
    90  			SecurityGroupIDs:    instanceOpts.SecurityGroupIDs,
    91  			SecurityGroups:      instanceOpts.SecurityGroups,
    92  			UserData:            instanceOpts.UserData64,
    93  		},
    94  	}
    95  
    96  	// Make the spot instance request
    97  	log.Printf("[DEBUG] Requesting spot bid opts: %s", spotOpts)
    98  	resp, err := conn.RequestSpotInstances(spotOpts)
    99  	if err != nil {
   100  		return fmt.Errorf("Error requesting spot instances: %s", err)
   101  	}
   102  	if len(resp.SpotInstanceRequests) != 1 {
   103  		return fmt.Errorf(
   104  			"Expected response with length 1, got: %s", resp)
   105  	}
   106  
   107  	sir := *resp.SpotInstanceRequests[0]
   108  	d.SetId(*sir.SpotInstanceRequestID)
   109  
   110  	if d.Get("wait_for_fulfillment").(bool) {
   111  		spotStateConf := &resource.StateChangeConf{
   112  			// http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-bid-status.html
   113  			Pending:    []string{"start", "pending-evaluation", "pending-fulfillment"},
   114  			Target:     "fulfilled",
   115  			Refresh:    SpotInstanceStateRefreshFunc(conn, sir),
   116  			Timeout:    10 * time.Minute,
   117  			Delay:      10 * time.Second,
   118  			MinTimeout: 3 * time.Second,
   119  		}
   120  
   121  		log.Printf("[DEBUG] waiting for spot bid to resolve... this may take several minutes.")
   122  		_, err = spotStateConf.WaitForState()
   123  
   124  		if err != nil {
   125  			return fmt.Errorf("Error while waiting for spot request (%s) to resolve: %s", sir, err)
   126  		}
   127  	}
   128  
   129  	return resourceAwsSpotInstanceRequestUpdate(d, meta)
   130  }
   131  
   132  // Update spot state, etc
   133  func resourceAwsSpotInstanceRequestRead(d *schema.ResourceData, meta interface{}) error {
   134  	conn := meta.(*AWSClient).ec2conn
   135  
   136  	req := &ec2.DescribeSpotInstanceRequestsInput{
   137  		SpotInstanceRequestIDs: []*string{aws.String(d.Id())},
   138  	}
   139  	resp, err := conn.DescribeSpotInstanceRequests(req)
   140  
   141  	if err != nil {
   142  		// If the spot request was not found, return nil so that we can show
   143  		// that it is gone.
   144  		if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidSpotInstanceRequestID.NotFound" {
   145  			d.SetId("")
   146  			return nil
   147  		}
   148  
   149  		// Some other error, report it
   150  		return err
   151  	}
   152  
   153  	// If nothing was found, then return no state
   154  	if len(resp.SpotInstanceRequests) == 0 {
   155  		d.SetId("")
   156  		return nil
   157  	}
   158  
   159  	request := resp.SpotInstanceRequests[0]
   160  
   161  	// if the request is cancelled, then it is gone
   162  	if *request.State == "cancelled" {
   163  		d.SetId("")
   164  		return nil
   165  	}
   166  
   167  	d.Set("spot_bid_status", *request.Status.Code)
   168  	// Instance ID is not set if the request is still pending
   169  	if request.InstanceID != nil {
   170  		d.Set("spot_instance_id", *request.InstanceID)
   171  	}
   172  	d.Set("spot_request_state", *request.State)
   173  	d.Set("tags", tagsToMap(request.Tags))
   174  
   175  	return nil
   176  }
   177  
   178  func resourceAwsSpotInstanceRequestUpdate(d *schema.ResourceData, meta interface{}) error {
   179  	conn := meta.(*AWSClient).ec2conn
   180  
   181  	d.Partial(true)
   182  	if err := setTags(conn, d); err != nil {
   183  		return err
   184  	} else {
   185  		d.SetPartial("tags")
   186  	}
   187  
   188  	d.Partial(false)
   189  
   190  	return resourceAwsSpotInstanceRequestRead(d, meta)
   191  }
   192  
   193  func resourceAwsSpotInstanceRequestDelete(d *schema.ResourceData, meta interface{}) error {
   194  	conn := meta.(*AWSClient).ec2conn
   195  
   196  	log.Printf("[INFO] Cancelling spot request: %s", d.Id())
   197  	_, err := conn.CancelSpotInstanceRequests(&ec2.CancelSpotInstanceRequestsInput{
   198  		SpotInstanceRequestIDs: []*string{aws.String(d.Id())},
   199  	})
   200  
   201  	if err != nil {
   202  		return fmt.Errorf("Error cancelling spot request (%s): %s", d.Id(), err)
   203  	}
   204  
   205  	if instanceId := d.Get("spot_instance_id").(string); instanceId != "" {
   206  		log.Printf("[INFO] Terminating instance: %s", instanceId)
   207  		if err := awsTerminateInstance(conn, instanceId); err != nil {
   208  			return fmt.Errorf("Error terminating spot instance: %s", err)
   209  		}
   210  	}
   211  
   212  	return nil
   213  }
   214  
   215  // SpotInstanceStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
   216  // an EC2 spot instance request
   217  func SpotInstanceStateRefreshFunc(
   218  	conn *ec2.EC2, sir ec2.SpotInstanceRequest) resource.StateRefreshFunc {
   219  
   220  	return func() (interface{}, string, error) {
   221  		resp, err := conn.DescribeSpotInstanceRequests(&ec2.DescribeSpotInstanceRequestsInput{
   222  			SpotInstanceRequestIDs: []*string{sir.SpotInstanceRequestID},
   223  		})
   224  
   225  		if err != nil {
   226  			if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidSpotInstanceRequestID.NotFound" {
   227  				// Set this to nil as if we didn't find anything.
   228  				resp = nil
   229  			} else {
   230  				log.Printf("Error on StateRefresh: %s", err)
   231  				return nil, "", err
   232  			}
   233  		}
   234  
   235  		if resp == nil || len(resp.SpotInstanceRequests) == 0 {
   236  			// Sometimes AWS just has consistency issues and doesn't see
   237  			// our request yet. Return an empty state.
   238  			return nil, "", nil
   239  		}
   240  
   241  		req := resp.SpotInstanceRequests[0]
   242  		return req, *req.Status.Code, nil
   243  	}
   244  }